Skip to content

Commit

Permalink
chore: Rename/add commands & options, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Jan 20, 2024
1 parent b1f2848 commit fed1ee8
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ private String getTable(String[] fields, String[][] data) {

private String getHeader(String fieldName) {
String header = getConfig().getMessageResolver().getMessageString("output.header."+fieldName);
return header!=null ? header : PropertyPathFormatter.humanReadable(fieldName);
return header!=null ? header : PropertyPathFormatter.humanReadable(getNormalizedFieldName(fieldName));
}

private String getNormalizedFieldName(String fieldName) {
return fieldName.replaceAll("String$", "");
}

private String[] getFields(ObjectNode firstObjectNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.http.proxy.helper.ProxyHelper;
import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand;
import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.common.rest.unirest.GenericUnirestFactory;
import com.fortify.cli.common.util.FileUtils;
import com.fortify.cli.common.util.StringUtils;
import com.fortify.cli.tool._common.helper.OsAndArchHelper;
Expand All @@ -45,7 +43,6 @@
import com.fortify.cli.tool._common.helper.ToolInstallationDescriptor;
import com.fortify.cli.tool._common.helper.ToolOutputDescriptor;

import kong.unirest.UnirestInstance;
import lombok.SneakyThrows;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -205,16 +202,10 @@ private static final void warnIfDifferentInstallPath(ToolInstallationDescriptor
private static final File download(ToolDefinitionArtifactDescriptor artifactDescriptor) throws IOException {
File tempDownloadFile = File.createTempFile("fcli-tool-download", null);
tempDownloadFile.deleteOnExit();
download(artifactDescriptor.getDownloadUrl(), tempDownloadFile);
ToolHelper.download(artifactDescriptor.getDownloadUrl(), tempDownloadFile);
return tempDownloadFile;
}

private static final void download(String downloadUrl, File destFile) {
UnirestInstance unirest = GenericUnirestFactory.getUnirestInstance("tool",
u->ProxyHelper.configureProxy(u, "tool", downloadUrl));
unirest.get(downloadUrl).asFile(destFile.getAbsolutePath(), StandardCopyOption.REPLACE_EXISTING).getBody();
}

private static final void updateBinPermissions(Path binPath) throws IOException {
try (Stream<Path> walk = Files.walk(binPath)) {
walk.forEach(AbstractToolInstallCommand::updateFilePermissions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.fortify.cli.tool._common.cli.mixin;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Date;

import com.fortify.cli.common.util.DateTimePeriodHelper;
import com.fortify.cli.common.util.DateTimePeriodHelper.Period;
import com.fortify.cli.common.util.FcliDataHelper;
import com.fortify.cli.tool._common.helper.ToolDefinitionsStateDescriptor;
import com.fortify.cli.tool._common.helper.ToolHelper;

import lombok.Getter;
import picocli.CommandLine.Option;

public class ToolDefinitionsUpdateMixin {
// When updating this URL, please also update the URL in the resource bundle
private static final String DEFAULT_URL = "https://github.com/fortify-ps/tool-definitions/raw/main/v1/tool-definitions.yaml.zip";
private static final DateTimePeriodHelper periodHelper = DateTimePeriodHelper.byRange(Period.SECONDS, Period.DAYS);
private static final Path DESCRIPTOR_PATH = ToolHelper.DEFINITIONS_STATE_DIR.resolve("state.json");
@Getter @Option(names={"-s", "--definitions-source"}, required = false, descriptionKey="fcli.tool.definitions.update.definitions-source")
private String source = DEFAULT_URL;
@Getter @Option(names={"-a", "--max-definitions-age"}, required = false, defaultValue = "1h", descriptionKey="fcli.tool.definitions.update.max-definitions-age")
private String maxAge;

public final ToolDefinitionsStateDescriptor updateToolDefinitions() throws IOException {
createDefinitionsStateDir(ToolHelper.DEFINITIONS_STATE_DIR);
var zip = ToolHelper.DEFINITIONS_STATE_ZIP;
ToolDefinitionsStateDescriptor descriptor = FcliDataHelper.readFile(DESCRIPTOR_PATH, ToolDefinitionsStateDescriptor.class, false);
if ( descriptor==null || !Files.exists(zip) || isMaxAgeExpired(descriptor, maxAge) ) {
update(source, zip);
descriptor = new ToolDefinitionsStateDescriptor(source, new Date(getModifiedTime(zip).toMillis()), "UPDATED");
} else {
descriptor = new ToolDefinitionsStateDescriptor(descriptor.getSource(), descriptor.getLastUpdate(), "SKIPPED");
}
FcliDataHelper.saveFile(DESCRIPTOR_PATH, descriptor, true);
return descriptor;
}

private static final void createDefinitionsStateDir(Path dir) throws IOException {
if( !Files.exists(dir) ) {
Files.createDirectories(dir);
}
}

private static final boolean isMaxAgeExpired(ToolDefinitionsStateDescriptor descriptor, String maxAge) {
var lastUpdate = descriptor.getLastUpdate().toInstant();
return periodHelper.getCurrentOffsetDateTimeMinusPeriod(maxAge).toInstant().compareTo(lastUpdate)>0;
}

private static FileTime getModifiedTime(Path path) throws IOException {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
return attr.lastModifiedTime();
}

private static final void update(String source, Path dest) throws IOException {
try {
ToolHelper.download(new URL(source).toString(), dest.toFile());
} catch ( MalformedURLException e ) {
Files.copy(Path.of(source), dest, StandardCopyOption.REPLACE_EXISTING);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2023 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors (“Open Text”) are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.tool._common.helper;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.formkiq.graalvm.annotations.Reflectable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Reflectable @NoArgsConstructor @AllArgsConstructor
@Data
public final class ToolDefinitionsStateDescriptor{
private String source;
private Date lastUpdate;
private String __action__;

public final String getLastUpdateString() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(getLastUpdate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@
package com.fortify.cli.tool._common.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fortify.cli.common.http.proxy.helper.ProxyHelper;
import com.fortify.cli.common.rest.unirest.GenericUnirestFactory;
import com.fortify.cli.common.util.FcliDataHelper;
import com.fortify.cli.common.util.FileUtils;

public final class ToolHelper {
public static final Path DEFINITIONS_STATE_DIR = FcliDataHelper.getFcliStatePath().resolve("tool");
public static final Path DEFINITIONS_STATE_ZIP = DEFINITIONS_STATE_DIR.resolve("tool-definitions.yaml.zip");
private static final String DEFINITIONS_INTERNAL_ZIP = "com/fortify/cli/tool/config/tool-definitions.yaml.zip";
private static final ObjectMapper yamlObjectMapper = new ObjectMapper(new YAMLFactory());
private static final String internalToolDefinitionsLocation = "com/fortify/cli/tool/config/tool-definitions.yaml.zip";
private static final File configuredToolDefinitionsFile = FcliDataHelper.getFcliConfigPath().resolve("tool/tool-definitions.yaml.zip").toFile();

public static final ToolDefinitionRootDescriptor getToolDefinitionRootDescriptor(String toolName) {
String yamlFileName = toolName + ".yaml";
Expand Down Expand Up @@ -66,48 +69,19 @@ public static final String getResourceFile(String toolName, String fileName) {
return String.format("%s/%s", getResourceDir(toolName.replace('-', '_')), fileName);
}

private static final Path getInstallDescriptorPath(String toolName, String version) {
return FcliDataHelper.getFcliStatePath().resolve("tools").resolve(toolName).resolve(version);
}

private static final InputStream getToolDefinitionsInputStream() throws FileNotFoundException {
return configuredToolDefinitionsFile.exists()
? new FileInputStream(configuredToolDefinitionsFile)
: FileUtils.getResourceInputStream(internalToolDefinitionsLocation);
public static final File download(String url, File dest) {
GenericUnirestFactory.getUnirestInstance("tool", u->ProxyHelper.configureProxy(u, "tool", url))
.get(url).asFile(dest.getAbsolutePath(), StandardCopyOption.REPLACE_EXISTING).getBody();
return dest;
}

/*
private static final void initializeZipBundle() throws IOException {
if(!toolDefinitions.toFile().exists()) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String resourceFile = "com/fortify/cli/tool/config/tool-definitions.yaml.zip";
try ( InputStream stream = classLoader.getResourceAsStream(resourceFile) ) {
if(!FcliDataHelper.getFcliConfigPath().resolve("tool").toFile().exists()) {
Files.createDirectories(FcliDataHelper.getFcliConfigPath().resolve("tool"));
}
Files.copy(stream, toolDefinitions, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw e;
}
}
private static final Path getInstallDescriptorPath(String toolName, String version) {
return FcliDataHelper.getFcliStatePath().resolve("tools").resolve(toolName).resolve(version);
}

private static final ToolDefinitionRootDescriptor loadDescriptorFromZipBundle(String toolName) throws IOException {
ZipFile bundle = new ZipFile(toolDefinitions.toString());
Enumeration<? extends ZipEntry> entries = bundle.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.getName().equals(String.format("%s.yaml", toolName))) {
try (InputStream file = bundle.getInputStream(entry)) {
return yamlObjectMapper.readValue(file, ToolDefinitionRootDescriptor.class);
} catch (IOException e) {
throw e;
}
}
}
throw new FileNotFoundException(String.format("%s.yaml", toolName));
private static final InputStream getToolDefinitionsInputStream() throws IOException {
return Files.exists(DEFINITIONS_STATE_ZIP)
? Files.newInputStream(DEFINITIONS_STATE_ZIP)
: FileUtils.getResourceInputStream(DEFINITIONS_INTERNAL_ZIP);
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import com.fortify.cli.common.cli.cmd.AbstractContainerCommand;
import com.fortify.cli.tool.bugtracker_utility.cli.cmd.ToolBugTrackerUtilityCommands;
import com.fortify.cli.tool.config.cli.cmd.ToolConfigCommands;
import com.fortify.cli.tool.debricked_cli.cli.cmd.ToolDebrickedCliCommands;
import com.fortify.cli.tool.definitions.cli.cmd.ToolDefinitionsCommands;
import com.fortify.cli.tool.fcli.cli.cmd.ToolFcliCommands;
import com.fortify.cli.tool.fod_uploader.cli.cmd.ToolFoDUploaderCommands;
import com.fortify.cli.tool.sc_client.cli.cmd.ToolSCClientCommands;
Expand All @@ -33,7 +33,7 @@
ToolFoDUploaderCommands.class,
ToolSCClientCommands.class,
ToolVulnExporterCommands.class,
ToolConfigCommands.class
ToolDefinitionsCommands.class
}
)
public class ToolCommands extends AbstractContainerCommand {}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit fed1ee8

Please sign in to comment.