Search in sources :

Example 1 with CommandData

use of aQute.jpm.lib.CommandData in project bnd by bndtools.

the class Main method _update.

@Description("Perform updates for installed commands and services")
public void _update(UpdateOptions opts) throws Exception {
    if (!jpm.hasAccess()) {
        error("No write acces, might require administrator or root privileges (sudo in *nix)");
        return;
    }
    ArrayList<String> refs = new ArrayList<String>();
    for (CommandData data : jpm.getCommands()) {
        refs.add(data.name);
    }
    for (ServiceData data : jpm.getServices()) {
        refs.add(data.name);
    }
    ArrayList<UpdateMemo> notFound = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
    ArrayList<UpdateMemo> upToDate = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
    ArrayList<UpdateMemo> toUpdate = new ArrayList<JustAnotherPackageManager.UpdateMemo>();
    ArrayList<CommandData> datas = new ArrayList<CommandData>();
    if (opts._arguments().size() == 0) {
        datas.addAll(jpm.getCommands());
        datas.addAll(jpm.getServices());
    } else {
        for (String pattern : opts._arguments()) {
            Glob glob = new Glob(pattern);
            for (String name : refs) {
                if (glob.matcher(name).matches()) {
                    CommandData data = jpm.getCommand(name);
                    if (data == null) {
                        Service service = jpm.getService(name);
                        if (service != null) {
                            data = service.getServiceData();
                        }
                    }
                    if (data != null) {
                        datas.add(data);
                    }
                }
            }
        }
    }
    for (CommandData data : datas) {
        jpm.listUpdates(notFound, upToDate, toUpdate, data, opts.staged());
    }
    if (opts.all() || opts._arguments().size() > 0) {
        for (UpdateMemo memo : toUpdate) {
            jpm.update(memo);
        }
        out.format("%d command(s) updated%n", toUpdate.size());
    } else {
        Justif justif = new Justif(100, 20, 50);
        StringBuilder sb = new StringBuilder();
        Formatter f = new Formatter(sb);
        if (upToDate.size() > 0) {
            f.format("Up to date:%n");
            for (UpdateMemo memo : upToDate) {
                if (memo.current instanceof ServiceData) {
                    f.format(" - %s (service) \t0- %s%n", memo.current.name, memo.current.version);
                } else {
                    f.format(" - %s \t0- %s%n", memo.current.name, memo.current.version);
                }
            }
            f.format("%n");
        }
        if (toUpdate.size() > 0) {
            f.format("Update available:%n");
            for (UpdateMemo memo : toUpdate) {
                if (memo.current instanceof ServiceData) {
                    f.format(" - %s (service) \t0- %s \t1-> %s%n", memo.current.name, memo.current.version, memo.best.version);
                } else {
                    f.format(" - %s \t0- %s \t1-> %s%n", memo.current.name, memo.current.version, memo.best.version);
                }
            }
            f.format("%n");
        }
        if (notFound.size() > 0) {
            if (opts.staged()) {
                f.format("Information not found (local install ?):%n");
            } else {
                f.format("Information not found (try including staging versions with the --staged (-s) flag)%n");
            }
            for (UpdateMemo memo : notFound) {
                if (memo.current instanceof ServiceData) {
                    f.format(" - %s (service)%n", memo.current.name);
                } else {
                    f.format(" - %s%n", memo.current.name);
                }
            }
        }
        if (toUpdate.size() > 0) {
            f.format("%nIn order to apply all possible updates, run jpm update again with the --all (or -a) flag.%n");
        }
        f.flush();
        justif.wrap(sb);
        out.println(sb.toString());
        f.close();
    }
}
Also used : JustAnotherPackageManager(aQute.jpm.lib.JustAnotherPackageManager) Justif(aQute.lib.justif.Justif) Formatter(java.util.Formatter) ArrayList(java.util.ArrayList) UpdateMemo(aQute.jpm.lib.JustAnotherPackageManager.UpdateMemo) Service(aQute.jpm.lib.Service) ServiceData(aQute.jpm.lib.ServiceData) Glob(aQute.libg.glob.Glob) CommandData(aQute.jpm.lib.CommandData) Description(aQute.lib.getopt.Description)

Example 2 with CommandData

use of aQute.jpm.lib.CommandData in project bnd by bndtools.

the class Main method _service.

@Description("Manage the jpm4j services")
public void _service(ServiceOptions opts) throws Exception {
    if (opts._arguments().isEmpty()) {
        for (ServiceData sd : jpm.getServices()) print(sd);
        return;
    }
    List<String> cmdline = opts._arguments();
    String name = cmdline.remove(0);
    Service s = jpm.getService(name);
    if (opts.remove()) {
        if (!jpm.hasAccess()) {
            error("No write access to remove service %s", name);
            return;
        }
        if (s == null) {
            error("No such service %s to remove", name);
            return;
        }
        s.stop();
        s.remove();
        return;
    }
    if (opts.create() != null) {
        logger.debug("create service");
        if (s != null) {
            error("Service already exists, cannot be created: %s. Update or remove it first", name);
            return;
        }
        ArtifactData target = jpm.getCandidate(opts.create());
        if (target == null) {
            error("Cannot find candidate for coordinates", opts.create());
            return;
        }
        ServiceData data = new ServiceData();
        CommandData cmd = jpm.parseCommandData(target);
        for (Field f : cmd.getClass().getFields()) {
            f.set(data, f.get(cmd));
        }
        logger.debug("service data {}", cmd);
        data.name = name;
        updateServiceData(data, opts);
        logger.debug("update service data");
        String result = jpm.createService(data, false);
        if (result != null)
            error("Create service failed: %s", result);
        return;
    }
    if (s == null) {
        error("No such service: %s", name);
        return;
    }
    ServiceData data = s.getServiceData();
    if (updateServiceData(data, opts) || opts.coordinates() != null || opts.update()) {
        if (!jpm.hasAccess()) {
            error("No write access to update service %s", name);
            return;
        }
        if (opts.coordinates() != null || opts.update()) {
            String coordinates = opts.coordinates();
            if (coordinates == null) {
                error("No coordinate found in old service record");
                return;
            }
            int n = coordinates.indexOf('@');
            if (n > 0)
                coordinates = coordinates.substring(0, n);
            logger.debug("Updating from coordinate: {}", coordinates);
            ArtifactData target = jpm.getCandidate(coordinates);
            if (target == null) {
                error("No candidates found for %s (%s)", coordinates, opts.staged() ? "staged" : "only masters");
                return;
            }
            CommandData cmd = jpm.parseCommandData(target);
            for (Field f : cmd.getClass().getFields()) {
                f.set(data, f.get(cmd));
            }
            data.name = name;
        }
        String result = jpm.createService(data, true);
        if (result != null)
            error("Update service failed: %s", result);
        else if (s.isRunning())
            warning("Changes will not affect the currently running process");
    }
    Data.details(data, out);
}
Also used : Field(java.lang.reflect.Field) Service(aQute.jpm.lib.Service) CommandData(aQute.jpm.lib.CommandData) ServiceData(aQute.jpm.lib.ServiceData) ArtifactData(aQute.jpm.lib.ArtifactData) Description(aQute.lib.getopt.Description)

Example 3 with CommandData

use of aQute.jpm.lib.CommandData in project bnd by bndtools.

the class Main method print.

private void print(List<CommandData> commands) {
    Justif j = new Justif(width, tabs);
    Formatter f = j.formatter();
    for (CommandData command : commands) {
        f.format("%s\t1%s%n", command.name, Strings.display(command.description, command.title));
    }
    out.append(j.wrap());
}
Also used : Justif(aQute.lib.justif.Justif) Formatter(java.util.Formatter) CommandData(aQute.jpm.lib.CommandData)

Example 4 with CommandData

use of aQute.jpm.lib.CommandData in project bnd by bndtools.

the class Main method _command.

@Description("Manage the jpm4j commands")
public void _command(CommandOptions opts) throws Exception {
    if (opts.remove()) {
        Instructions instrs = new Instructions(opts._arguments());
        for (CommandData cmd : jpm.getCommands()) {
            if (instrs.matches(cmd.name)) {
                jpm.deleteCommand(cmd.name);
            }
        }
        return;
    }
    if (opts._arguments().isEmpty()) {
        print(jpm.getCommands());
        return;
    }
    String cmd = opts._arguments().get(0);
    CommandData data = jpm.getCommand(cmd);
    if (data == null) {
        error("Not found: %s", cmd);
    } else {
        CommandData newer = new CommandData();
        JustAnotherPackageManager.xcopy(data, newer);
        if (updateCommandData(newer, opts)) {
            jpm.deleteCommand(data.name);
            String result = jpm.createCommand(newer, true);
            if (result != null)
                error("Failed to update command %s: %s", cmd, result);
        }
        print(newer);
    }
}
Also used : Instructions(aQute.bnd.osgi.Instructions) CommandData(aQute.jpm.lib.CommandData) Description(aQute.lib.getopt.Description)

Example 5 with CommandData

use of aQute.jpm.lib.CommandData in project bnd by bndtools.

the class Main method _install.

/**
	 * A better way to install
	 */
@Description("Install an artifact from a url, file, or http://www.jpm4j.org")
public void _install(installOptions opts) throws Exception {
    if (!this.options.user() && !jpm.hasAccess()) {
        error("No write acces, might require administrator or root privileges (sudo in *nix)");
        return;
    }
    for (String coordinate : opts._arguments()) {
        logger.debug("install {}", coordinate);
        File file = IO.getFile(base, coordinate);
        if (file.isFile()) {
            coordinate = file.toURI().toString();
            logger.debug("is existing file: {}", coordinate);
        }
        ArtifactData artifact = jpm.getCandidate(coordinate);
        logger.debug("candidate {}", artifact);
        if (artifact == null) {
            if (jpm.isWildcard(coordinate))
                error("no candidate found for %s", coordinate);
            else
                error("no candidate found for %s, you could try %s@* to also see staged and withdrawn revisions", coordinate, coordinate);
        } else {
            if (!opts.ignore()) {
                CommandData cmd = jpm.parseCommandData(artifact);
                updateCommandData(cmd, opts);
                logger.debug("main={}, name={}", cmd.main, cmd.name);
                if (cmd.main != null) {
                    if (cmd.name == null && !artifact.local) {
                        cmd.name = artifact.coordinate.getArtifactId();
                    }
                    List<Error> errors = cmd.validate();
                    if (!errors.isEmpty()) {
                        error("Command not valid");
                        for (Error error : errors) {
                            error("[%s] %s %s %s %s", error.code, error.description, error.path, error.failure, error.value);
                        }
                    } else {
                        String result = jpm.createCommand(cmd, opts.force());
                        if (result != null) {
                            error("[%s] %s", coordinate, result);
                        }
                    }
                } else
                    error("No main class found. Please specify");
            }
        }
    }
}
Also used : Error(aQute.struct.struct.Error) CommandData(aQute.jpm.lib.CommandData) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ArtifactData(aQute.jpm.lib.ArtifactData) Description(aQute.lib.getopt.Description)

Aggregations

CommandData (aQute.jpm.lib.CommandData)6 Description (aQute.lib.getopt.Description)5 Service (aQute.jpm.lib.Service)3 ServiceData (aQute.jpm.lib.ServiceData)3 ArtifactData (aQute.jpm.lib.ArtifactData)2 Justif (aQute.lib.justif.Justif)2 Glob (aQute.libg.glob.Glob)2 ArrayList (java.util.ArrayList)2 Formatter (java.util.Formatter)2 Instructions (aQute.bnd.osgi.Instructions)1 JustAnotherPackageManager (aQute.jpm.lib.JustAnotherPackageManager)1 UpdateMemo (aQute.jpm.lib.JustAnotherPackageManager.UpdateMemo)1 Error (aQute.struct.struct.Error)1 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 Field (java.lang.reflect.Field)1