Search in sources :

Example 1 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class Util method explode.

/**
 * Explodes filepattern found in baseDir returnin list of relative Path names.
 *
 * TODO: this really should return some kind of abstraction of paths that allow
 * it be portable for urls as wells as files...or have a filesystem for each...
 *
 * @param source
 * @param baseDir
 * @param filepattern
 * @return
 */
public static List<String> explode(String source, Path baseDir, String filepattern) {
    List<String> results = new ArrayList<>();
    if (source != null && Util.isURL(source)) {
        // TODO: technically this is really where it should get resolved!
        if (isPattern(filepattern)) {
            Util.warnMsg("Pattern " + filepattern + " used while using URL to run; this could result in errors.");
            return results;
        } else {
            results.add(filepattern);
        }
    } else if (Util.isURL(filepattern)) {
        results.add(filepattern);
    } else if (!filepattern.contains("?") && !filepattern.contains("*")) {
        // not a pattern thus just as well return path directly
        results.add(filepattern);
    } else {
        // it is a non-url letls try locate it
        PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + filepattern);
        FileVisitor<Path> matcherVisitor = new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attribs) {
                Path relpath = baseDir.relativize(file);
                if (matcher.matches(relpath)) {
                    // to avoid windows fail.
                    if (file.toFile().exists()) {
                        results.add(relpath.toString().replace("\\", "/"));
                    } else {
                        Util.verboseMsg("Warning: " + relpath + " matches but does not exist!");
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        };
        try {
            Files.walkFileTree(baseDir, matcherVisitor);
        } catch (IOException e) {
            throw new ExitException(BaseCommand.EXIT_INTERNAL_ERROR, "Problem looking for " + filepattern + " in " + baseDir.toString(), e);
        }
    }
    return results;
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) PathMatcher(java.nio.file.PathMatcher) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class RefTarget method copy.

public void copy(Path destroot) {
    Path from = source.getFile().toPath();
    Path to = to(destroot);
    Util.verboseMsg("Copying " + from + " to " + to);
    try {
        if (!to.toFile().getParentFile().exists()) {
            to.toFile().getParentFile().mkdirs();
        }
        Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException ioe) {
        throw new ExitException(EXIT_UNEXPECTED_STATE, "Could not copy " + from + " to " + to, ioe);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException)

Example 3 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class ClasspathResourceResolver method getClasspathResource.

private static ResourceRef getClasspathResource(String cpResource) {
    String ref = cpResource.substring(11);
    Util.verboseMsg("Duplicating classpath resource " + ref);
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null) {
        cl = ResourceRef.class.getClassLoader();
    }
    URL url = cl.getResource(ref);
    if (url == null) {
        throw new ExitException(BaseCommand.EXIT_INVALID_INPUT, "Resource not found on class path: " + ref);
    }
    try {
        File f = new File(url.toURI());
        if (f.canRead()) {
            return ResourceRef.forCachedResource(cpResource, f);
        }
    } catch (URISyntaxException | IllegalArgumentException e) {
    // Ignore
    }
    // We couldn't read the file directly from the class path so let's make a copy
    try (InputStream is = url.openStream()) {
        Path to = Util.getUrlCache(cpResource);
        Files.createDirectories(to.getParent());
        Files.copy(is, to, StandardCopyOption.REPLACE_EXISTING);
        return ResourceRef.forCachedResource(cpResource, to.toFile());
    } catch (IOException e) {
        throw new ExitException(BaseCommand.EXIT_GENERIC_ERROR, "Resource could not be copied from class path: " + ref, e);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ResourceRef(dev.jbang.source.ResourceRef) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException) File(java.io.File) URL(java.net.URL)

Example 4 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class RemoteResourceResolver method fetchScriptFromUntrustedURL.

public static ResourceRef fetchScriptFromUntrustedURL(String scriptURL) {
    try {
        java.net.URI uri = new java.net.URI(scriptURL);
        if (!TrustedSources.instance().isURLTrusted(uri)) {
            String question = scriptURL + " is not from a trusted source thus not running it automatically.\n" + "\n" + "If you trust the url to be safe to run you can do one of the following";
            String trustUrl = goodTrustURL(scriptURL);
            String trustOrgUrl = orgURL(trustUrl);
            List<String> options = new ArrayList<>();
            options.add("Trust once: Add no trust, just run this time");
            options.add("Trust limited url in future: " + trustUrl);
            if (trustOrgUrl != null) {
                options.add("Trust organization url in future: " + trustOrgUrl);
            }
            int result = Util.askInput(question, 30, 0, options.toArray(new String[] {}));
            TrustedSources ts = TrustedSources.instance();
            if (result == 2) {
                ts.add(trustUrl, Settings.getTrustedSourcesFile().toFile());
            } else if (result == 3) {
                ts.add(trustOrgUrl, Settings.getTrustedSourcesFile().toFile());
            } else if (result <= 0) {
                String exmsg = scriptURL + " is not from a trusted source and user did not confirm trust thus aborting.\n" + "If you trust the url to be safe to run are here a few suggestions:\n" + "Limited trust:\n     jbang trust add " + trustUrl + "\n";
                if (trustOrgUrl != null) {
                    exmsg += "Organization trust:\n     jbang trust add " + trustOrgUrl + "\n";
                }
                exmsg += "Trust all subdomains:\n    jbang trust add *." + uri.getAuthority() + "\n" + "Trust all sources (WARNING! disables url protection):\n    jbang trust add *" + "\n" + "\nFor more control edit ~/.jbang/trusted-sources.json" + "\n";
                throw new ExitException(10, exmsg);
            }
        }
        scriptURL = swizzleURL(scriptURL);
        Path path = Util.swizzleContent(scriptURL, Util.downloadAndCacheFile(scriptURL));
        return ResourceRef.forCachedResource(scriptURL, path.toFile());
    } catch (IOException | URISyntaxException e) {
        throw new ExitException(BaseCommand.EXIT_INVALID_INPUT, "Could not download " + scriptURL, e);
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TrustedSources(dev.jbang.net.TrustedSources) ExitException(dev.jbang.cli.ExitException)

Example 5 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class RemoteResourceResolver method fetchFromURL.

public static ResourceRef fetchFromURL(String scriptURL) {
    try {
        String url = swizzleURL(scriptURL);
        Path path = Util.downloadAndCacheFile(url);
        return ResourceRef.forCachedResource(scriptURL, path.toFile());
    } catch (IOException e) {
        throw new ExitException(BaseCommand.EXIT_INVALID_INPUT, "Could not download " + scriptURL, e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException)

Aggregations

ExitException (dev.jbang.cli.ExitException)25 IOException (java.io.IOException)17 Path (java.nio.file.Path)16 File (java.io.File)4 ArrayList (java.util.ArrayList)4 ResourceRef (dev.jbang.source.ResourceRef)3 Util (dev.jbang.util.Util)3 Collectors (java.util.stream.Collectors)3 Template (io.quarkus.qute.Template)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Files (java.nio.file.Files)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 JsonParseException (com.google.gson.JsonParseException)1