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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations