Search in sources :

Example 31 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class Main method _settings.

@Description("Manage user settings of jpm (in ~/.jpm)")
public void _settings(settingOptions opts) throws Exception {
    try {
        logger.debug("settings {}", opts.clear());
        List<String> rest = opts._arguments();
        if (opts.clear()) {
            settings.clear();
            logger.debug("clear {}", settings.entrySet());
        }
        if (opts.publicKey()) {
            out.println(tos(opts.hex(), settings.getPublicKey()));
            return;
        }
        if (opts.secretKey()) {
            out.println(tos(opts.hex(), settings.getPrivateKey()));
            return;
        }
        if (opts.id()) {
            out.printf("%s\n", tos(opts.hex(), settings.getPublicKey()));
        }
        if (opts.mac()) {
            for (String s : rest) {
                byte[] data = s.getBytes(UTF_8);
                byte[] signature = settings.sign(data);
                out.printf("%s\n", tos(opts.hex(), signature));
            }
            return;
        }
        if (rest.isEmpty()) {
            list(null, settings);
        } else {
            boolean set = false;
            for (String s : rest) {
                Matcher m = ASSIGNMENT.matcher(s);
                logger.debug("try {}", s);
                if (m.matches()) {
                    logger.debug("matches {} {} {}", s, m.group(1), m.group(2));
                    String key = m.group(1);
                    Instructions instr = new Instructions(key);
                    Collection<String> select = instr.select(settings.keySet(), true);
                    String value = m.group(2);
                    if (value == null) {
                        logger.debug("list wildcard {} {} {}", instr, select, settings.keySet());
                        list(select, settings);
                    } else {
                        logger.debug("assignment 	");
                        settings.put(key, value);
                        set = true;
                    }
                } else {
                    err.printf("Cannot assign %s\n", s);
                }
            }
            if (set) {
                logger.debug("saving");
                settings.save();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Matcher(java.util.regex.Matcher) Instructions(aQute.bnd.osgi.Instructions) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Description(aQute.lib.getopt.Description)

Example 32 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class Main method _init.

@Description("Install jpm on the current system")
public void _init(InitOptions opts) throws Exception {
    if (!jpm.hasAccess()) {
        error("No write acces, might require administrator or root privileges (sudo in *nix)");
        return;
    }
    jpm.getPlatform().init();
    try {
        String s = System.getProperty("java.class.path");
        if (s == null) {
            error("Cannot initialize because not clear what the command jar is from java.class.path: %s", s);
            return;
        }
        String[] parts = s.split(File.pathSeparator);
        s = parts[0];
        try {
            File f = new File(s).getAbsoluteFile();
            if (f.exists()) {
                CommandLine cl = new CommandLine(this);
                String help = cl.execute(this, "install", Arrays.asList("-fl", f.getAbsolutePath()));
                if (help != null) {
                    error(help);
                    return;
                }
                String completionInstallResult = jpm.getPlatform().installCompletion(this);
                if (completionInstallResult != null)
                    logger.debug("{}", completionInstallResult);
                settings.put(JPM_CONFIG_BIN, jpm.getBinDir().getAbsolutePath());
                settings.put(JPM_CONFIG_HOME, jpm.getHomeDir().getAbsolutePath());
                settings.save();
                if (jpm.getPlatform().hasPost()) {
                    Command cmd = new Command();
                    cmd.add(new File(jpm.getBinDir(), "jpm").getAbsolutePath());
                    cmd.add("_postinstall");
                    cmd.setTimeout(5, TimeUnit.SECONDS);
                    StringBuffer stdout = new StringBuffer();
                    StringBuffer stderr = new StringBuffer();
                    System.out.println("post : " + cmd);
                    int result = cmd.execute(stdout, stderr);
                    if (result != 0) {
                        Justif j = new Justif(80, 5, 10, 20);
                        if (stdout.length() != 0)
                            j.formatter().format("stdout: $-%n", stdout);
                        if (stderr.length() != 0)
                            j.formatter().format("stderr: $-%n", stderr);
                        error("Failed to run platform init, exit code %s.%n%s", result, j.wrap());
                    } else
                        out.append(stdout);
                }
                out.println("Home dir      " + jpm.getHomeDir());
                out.println("Bin  dir      " + jpm.getBinDir());
            } else
                error("Cannot find the jpm jar from %s", f);
        } catch (InvocationTargetException e) {
            exception(e.getTargetException(), "Could not install jpm, %s", e.getTargetException());
            if (isExceptions())
                e.printStackTrace();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : CommandLine(aQute.lib.getopt.CommandLine) Justif(aQute.lib.justif.Justif) Command(aQute.libg.command.Command) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) Description(aQute.lib.getopt.Description)

Example 33 with Description

use of aQute.lib.getopt.Description 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)

Example 34 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class Main method _trace.

/**
	 * Trace a service.
	 * 
	 * @param options
	 * @throws Exception
	 */
@Description("Trace a service")
public void _trace(traceOptions options) throws Exception {
    List<String> args = options._arguments();
    String s = args.remove(0);
    boolean on = args.isEmpty() || !"off".equalsIgnoreCase(args.remove(0));
    Service service = jpm.getService(s);
    if (service == null)
        error("Non existent service %s", s);
    else {
        try {
            if (!service.isRunning())
                error("First start the service to trace it");
            else {
                String result = service.trace(on);
                if (result != null)
                    error("Failed to trace: %s", result);
            }
        } catch (Exception e) {
            exception(e, "Could not trace service %s due to %s", s, e);
        }
    }
}
Also used : Service(aQute.jpm.lib.Service) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Description(aQute.lib.getopt.Description)

Example 35 with Description

use of aQute.lib.getopt.Description in project bnd by bndtools.

the class Main method _start.

/**
	 * Start a service.
	 * 
	 * @param options
	 * @throws Exception
	 */
@Description("Start a service")
public void _start(startOptions options) throws Exception {
    if (!jpm.hasAccess()) {
        error("No write acces, might require administrator or root privileges (sudo in *nix)");
        return;
    }
    for (String s : options._arguments()) {
        Service service = jpm.getService(s);
        if (service == null)
            error("Non existent service %s", s);
        else {
            if (!service.isRunning()) {
                try {
                    ServiceData d = service.getServiceData();
                    logger.debug("starting {} as user {}, lock={}, log={}", d.name, d.user, d.lock, d.log);
                    if (options.clean())
                        service.clear();
                    String result = service.start();
                    if (result != null)
                        error("Failed to start: %s", result);
                } catch (Exception e) {
                    exception(e, "Could not start service %s due to %s", s, e);
                }
            } else
                warning("Service %s already running", s);
        }
    }
}
Also used : Service(aQute.jpm.lib.Service) ServiceData(aQute.jpm.lib.ServiceData) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Description(aQute.lib.getopt.Description)

Aggregations

Description (aQute.lib.getopt.Description)63 File (java.io.File)31 Jar (aQute.bnd.osgi.Jar)14 Project (aQute.bnd.build.Project)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 Instructions (aQute.bnd.osgi.Instructions)10 CommandLine (aQute.lib.getopt.CommandLine)10 Justif (aQute.lib.justif.Justif)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 Workspace (aQute.bnd.build.Workspace)7 PomFromManifest (aQute.bnd.maven.PomFromManifest)7 Service (aQute.jpm.lib.Service)7 Manifest (java.util.jar.Manifest)7 FileResource (aQute.bnd.osgi.FileResource)6 Resource (aQute.bnd.osgi.Resource)6 Analyzer (aQute.bnd.osgi.Analyzer)5 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)5 CommandData (aQute.jpm.lib.CommandData)5 ServiceData (aQute.jpm.lib.ServiceData)5