Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MTA-1297: add overwrite option #103

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ type analyzeCommand struct {
skipStaticReport bool
analyzeKnownLibraries bool
jsonOutput bool
overwrite bool
mavenSettingsFile string
sources []string
targets []string
@@ -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
@@ -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) {
@@ -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