-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Rename/add commands & options, refactoring
- Loading branch information
Showing
12 changed files
with
221 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...tool/src/main/java/com/fortify/cli/tool/_common/cli/mixin/ToolDefinitionsUpdateMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ool/src/main/java/com/fortify/cli/tool/_common/helper/ToolDefinitionsStateDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...-core/fcli-tool/src/main/java/com/fortify/cli/tool/config/cli/cmd/ToolConfigCommands.java
This file was deleted.
Oops, something went wrong.
97 changes: 0 additions & 97 deletions
97
.../fcli-tool/src/main/java/com/fortify/cli/tool/config/cli/cmd/ToolConfigUpdateCommand.java
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
.../fcli-tool/src/main/java/com/fortify/cli/tool/config/cli/mixin/ToolConfigSourceMixin.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.