Skip to content

Commit

Permalink
clear stale static report on skip report option
Browse files Browse the repository at this point in the history
Signed-off-by: Emily McMullan <[email protected]>

Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed Oct 30, 2023
1 parent 6c8e58d commit fbb6f74
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type analyzeCommand struct {
skipStaticReport bool
analyzeKnownLibraries bool
jsonOutput bool
overwrite bool
mavenSettingsFile string
sources []string
targets []string
Expand Down Expand Up @@ -154,6 +155,7 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
analyzeCommand.Flags().StringVar(&analyzeCmd.mavenSettingsFile, "maven-settings", "", "path to a custom maven settings file to use")
analyzeCommand.Flags().StringVarP(&analyzeCmd.mode, "mode", "m", string(provider.FullAnalysisMode), "analysis mode. Must be one of 'full' or 'source-only'")
analyzeCommand.Flags().BoolVar(&analyzeCmd.jsonOutput, "json-output", false, "create analysis and dependency output as json")
analyzeCommand.Flags().BoolVar(&analyzeCmd.overwrite, "overwrite", false, "overwrite output directory")
analyzeCommand.Flags().StringVar(&analyzeCmd.jaegerEndpoint, "jaeger-endpoint", "", "jaeger endpoint to collect traces")

return analyzeCommand
Expand All @@ -166,6 +168,10 @@ func (a *analyzeCommand) Validate() error {
if a.labelSelector != "" && (len(a.sources) > 0 || len(a.targets) > 0) {
return fmt.Errorf("must not specify label-selector and sources or targets")
}
err := a.CheckOverwriteOutput()
if err != nil {
return err
}
stat, err := os.Stat(a.output)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -215,6 +221,26 @@ func (a *analyzeCommand) Validate() error {
return nil
}

func (a *analyzeCommand) CheckOverwriteOutput() error {
// default overwrite to false so check for already existing output dir
stat, err := os.Stat(a.output)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
}
if !a.overwrite && stat != nil {
return fmt.Errorf("output dir %v already exists and --overwrite not set", a.output)
}
if a.overwrite && stat != nil {
err := os.RemoveAll(a.output)
if err != nil {
return err
}
}
return nil
}

func (a *analyzeCommand) ListLabels(ctx context.Context) error {
// reserved labels
sourceLabel := outputv1.SourceTechnologyLabel
Expand Down

0 comments on commit fbb6f74

Please sign in to comment.