Skip to content

Commit

Permalink
🐛 Moving clean-up of resource to after the resources are created. (#258)
Browse files Browse the repository at this point in the history
Moving clean-up of resource to after the resources are created.

Also removing duplicate input validation code.

Signed-off-by: Shawn Hurley <[email protected]>
  • Loading branch information
shawn-hurley authored Jun 13, 2024
1 parent a89ffa3 commit ef49bed
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,6 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
analyzeCmd.cleanup = !val
}
if analyzeCmd.overrideProviderSettings == "" {
// defer cleaning created resources here instead of PostRun
// if Run returns an error, PostRun does not run
defer func() {
if err := analyzeCmd.CleanAnalysisResources(cmd.Context()); err != nil {
log.Error(err, "failed to clean temporary directories")
}
}()
if analyzeCmd.listSources || analyzeCmd.listTargets {
err := analyzeCmd.ListLabels(cmd.Context())
if err != nil {
Expand Down Expand Up @@ -177,6 +170,13 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
log.Error(err, "failed to create container volume")
return err
}
// defer cleaning created resources here instead of PostRun
// if Run returns an error, PostRun does not run
defer func() {
if err := analyzeCmd.CleanAnalysisResources(cmd.Context()); err != nil {
log.Error(err, "failed to clean temporary directories")
}
}()
// allow for 5 retries of running provider in the case of port in use
providerPorts, err := analyzeCmd.RunProviders(cmd.Context(), containerNetworkName, containerVolName, foundProviders, 5)
if err != nil {
Expand Down Expand Up @@ -335,19 +335,6 @@ func (a *analyzeCommand) Validate(ctx context.Context) error {
if stat != nil && !stat.IsDir() {
return fmt.Errorf("output path %s is not a directory", a.output)
}
stat, err = os.Stat(a.input)
if err != nil {
return fmt.Errorf("%w failed to stat input path %s", err, a.input)
}
// when input isn't a dir, it's pointing to a binary
// we need abs path to mount the file correctly
if !stat.Mode().IsDir() {
a.input, err = filepath.Abs(a.input)
if err != nil {
return fmt.Errorf("%w failed to get absolute path for input file %s", err, a.input)
}
a.isFileInput = true
}
if len(a.depFolders) != 0 {
for i := range a.depFolders {
stat, err := os.Stat(a.depFolders[i])
Expand Down

0 comments on commit ef49bed

Please sign in to comment.