Skip to content

Commit

Permalink
add option to generate a new fingerprint file or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
Duppils authored and filip-debricked committed Aug 20, 2024
1 parent 04e5703 commit 2df8b39
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 17 additions & 1 deletion internal/cmd/fingerprint/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fingerprint
import (
"fmt"
"path/filepath"
"time"

"github.com/debricked/cli/internal/fingerprint"
"github.com/spf13/cobra"
Expand All @@ -14,13 +15,15 @@ var inclusions []string
var shouldFingerprintCompressedContent bool
var outputDir string
var minFingerprintContentLength int
var shouldRegenerateFingerprintFile bool

const (
ExclusionFlag = "exclusion"
InclusionFlag = "inclusion"
FingerprintCompressedContent = "fingerprint-compressed-content"
OutputDirFlag = "output-dir"
MinFingerprintContentLengthFlag = "min-fingerprint-content-length"
RegenerateFingerprintFile = "regenerate"
)

func NewFingerprintCmd(fingerprinter fingerprint.IFingerprint) *cobra.Command {
Expand Down Expand Up @@ -60,6 +63,7 @@ $ debricked scan . --include '**/node_modules/**'`)
cmd.Flags().BoolVar(&shouldFingerprintCompressedContent, FingerprintCompressedContent, false, `Fingerprint the contents of compressed files by unpacking them in memory, Supported files: `+fmt.Sprintf("%v", fingerprint.ZIP_FILE_ENDINGS))
cmd.Flags().StringVar(&outputDir, OutputDirFlag, ".", "The directory to write the output file to")
cmd.Flags().IntVar(&minFingerprintContentLength, MinFingerprintContentLengthFlag, 45, "Set minimum content length (in bytes) for files to fingerprint. Defaults to 45 bytes.")
cmd.Flags().BoolVar(&shouldRegenerateFingerprintFile, RegenerateFingerprintFile, true, `If generated fingerprint file should be overwritten on subequent scans. Defaults to true`)

viper.MustBindEnv(ExclusionFlag)

Expand All @@ -84,7 +88,19 @@ func RunE(f fingerprint.IFingerprint) func(_ *cobra.Command, args []string) erro
if err != nil {
return err
}
outputFilePath := filepath.Join(outputDir, fingerprint.OutputFileNameFingerprints)
var outputFilePath string
// TODO: decide if we should only create a date-suffix file if a file without date-suffix already exists.
// Depending on decision, check for existence of debricked.fingerprints.txt here.
if shouldRegenerateFingerprintFile {
outputFilePath = filepath.Join(outputDir, fingerprint.OutputFileNameFingerprints)
} else {
// TODO: decide on if date suffix is what we want or if an incrementing integer should be used.
timestamp := time.Now().Format("2006-01-02_15-04-05") // reference date, do not change the date, only format
ext := filepath.Ext(fingerprint.OutputFileNameFingerprints)
ouputFileName := fmt.Sprintf("%s_%s%s", fingerprint.OutputFileNameFingerprints, timestamp, ext)
outputFilePath = filepath.Join(outputDir, ouputFileName)
}

err = output.ToFile(outputFilePath)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/fingerprint/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ func TestNewFingerprintCmd(t *testing.T) {

func TestRunE(t *testing.T) {
defer func() {
os.Remove(fingerprint.OutputFileNameFingerprints)
os.Remove(fingerprint.OutputFileNameFingerprints) // TODO: make sure it will remove all generated fingerprint files (e.g. with date suffix)
}()
fingerprintMock := testdata.NewFingerprintMock()
runE := RunE(fingerprintMock)

err := runE(nil, []string{"."})

assert.NoError(t, err)
// TODO: Run command again, first with regenerate=true (default) to ensure no extra file is generated (check only one exists)
// Run command a third time, without regenerate,
// asserting that a date-stamped debricked fingerprint file is created when regenerate=false

}

0 comments on commit 2df8b39

Please sign in to comment.