allow suppression of npm management logging

This commit is contained in:
Grahame Grieve 2022-05-11 06:58:46 +10:00
parent 4b1b99a61c
commit 9c20a28710
3 changed files with 41 additions and 12 deletions

View File

@ -18,6 +18,7 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
private static final Logger ourLog = LoggerFactory.getLogger(BasePackageCacheManager.class);
private List<String> myPackageServers = new ArrayList<>();
private Function<String, PackageClient> myClientFactory = address -> new CachingPackageClient(address);
protected boolean silent;
/**
* Constructor
@ -77,7 +78,9 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
String url = packageClient.url(id, version);
return new InputStreamWithSrc(stream, url, version);
} catch (IOException e) {
ourLog.info("Failed to resolve package {}#{} from server: {} ({})", id, version, nextPackageServer, e.getMessage());
if (!silent) {
ourLog.info("Failed to resolve package {}#{} from server: {} ({})", id, version, nextPackageServer, e.getMessage());
}
}
}
}
@ -177,4 +180,9 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
return loadPackage(idAndVer, null);
}
public void setSilent(boolean silent) {
this.silent = silent;
}
}

View File

@ -344,15 +344,15 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
public NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException {
checkValidVersionString(version, id);
if (progress) {
System.out.println("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache");
System.out.print(" Fetching:");
log("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache");
log(" Fetching:");
}
NpmPackage npm = NpmPackage.fromPackage(packageTgzInputStream, sourceDesc, true);
if (progress) {
System.out.println();
System.out.print(" Installing: ");
log("");
logn(" Installing: ");
}
if (!suppressErrors && npm.name() == null || id == null || !id.equalsIgnoreCase(npm.name())) {
@ -374,7 +374,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
try {
Utilities.clearDirectory(packRoot);
} catch (Throwable t) {
System.out.println("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later");
log("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later");
}
int i = 0;
@ -392,10 +392,10 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
i++;
if (progress && i % 50 == 0) {
c++;
System.out.print(".");
logn(".");
if (c == 120) {
System.out.println("");
System.out.print(" ");
log("");
logn(" ");
c = 2;
}
}
@ -409,7 +409,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
ini.setIntegerProperty("package-sizes", id + "#" + v, size, null);
ini.save();
if (progress)
System.out.println(" done.");
log(" done.");
}
pck = loadPackageInfo(packRoot);
if (!id.equals(JSONUtil.str(npm.getNpm(), "name")) || !v.equals(JSONUtil.str(npm.getNpm(), "version"))) {
@ -428,7 +428,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} catch (Exception e) {
try {
// don't leave a half extracted package behind
System.out.println("Clean up package " + packRoot + " because installation failed: " + e.getMessage());
log("Clean up package " + packRoot + " because installation failed: " + e.getMessage());
e.printStackTrace();
Utilities.clearDirectory(packRoot);
new File(packRoot).delete();
@ -441,6 +441,18 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
});
}
private void log(String s) {
if (!silent) {
System.out.println(s);
}
}
private void logn(String s) {
if (!silent) {
System.out.print(s);
}
}
@Override
public String getPackageUrl(String packageId) throws IOException {
String result = super.getPackageUrl(packageId);
@ -646,7 +658,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
try {
loadFromBuildServer();
} catch (Exception e) {
System.out.println("Error connecting to build server - running without build (" + e.getMessage() + ")");
log("Error connecting to build server - running without build (" + e.getMessage() + ")");
e.printStackTrace();
}
return false;

View File

@ -63,8 +63,10 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.json.JSONUtil;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import org.hl7.fhir.utilities.npm.NpmPackage.ITransformingLoader;
@ -1160,5 +1162,12 @@ public class NpmPackage {
}
}
public static NpmPackage fromUrl(String source) throws IOException {
SimpleHTTPClient fetcher = new SimpleHTTPClient();
HTTPResult res = fetcher.get(source+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return fromPackage(new ByteArrayInputStream(res.getContent()));
}
}