diff --git a/cmd/root.go b/cmd/root.go index e2d7292..231c4be 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,6 +36,7 @@ const ( classifierFlag = "classifier" uploadToDTrackFlag = "upload-to-dependency-track" purgeCacheFlag = "purge-cache" + softExitFlag = "soft-exit" orgFlag = "organization" ) @@ -129,6 +130,7 @@ func init() { tagsUsage = "tags to use when SBOMs are uploaded to Dependency Track (optional)" purgeCacheUsage = "whether to purge gradle and go caches after a successful run (default: false)" orgFlagUsage = "used when using organization github app" + softExitUsage = "used on cleanup to exit soft without crashing" ) const classifierUsageTemplate = "classifier to use when uploading to Dependency Track. Valid values are: %s" @@ -144,6 +146,7 @@ func init() { rootCmd.PersistentFlags().BoolP(uploadToDTrackFlag, "u", false, uploadToDependencyTrackUsage) rootCmd.PersistentFlags().BoolP(purgeCacheFlag, "p", false, purgeCacheUsage) + rootCmd.PersistentFlags().BoolP(softExitFlag, "s", false, softExitUsage) rootCmd.PersistentFlags().StringP(orgFlag, "g", "", orgFlagUsage) } diff --git a/cmd/util.go b/cmd/util.go index 5e01de0..bcafaaa 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -46,6 +46,14 @@ func createAppFromCLI(cmd *cobra.Command, verbose bool) (*app.App, error) { options = append(options, app.WithCachePurge()) } + softExit, err := cmd.Flags().GetBool(softExitFlag) + if err != nil { + return nil, fmt.Errorf(errTemplate, softExitFlag) + } + if softExit { + options = append(options, app.WithSoftExit()) + } + if uploadToDependencyTrack { classifier, err := cmd.Flags().GetString(classifierFlag) if err != nil { diff --git a/internal/app/app.go b/internal/app/app.go index 5223cc1..fb616e0 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -29,7 +29,7 @@ type App struct { tags []string githubUsername, githubAPIToken, organization string // TODO Move later on to a separate GitHub client dependencyTrackClient *dtrack.DependencyTrackClient - purgeCache bool + purgeCache, softExit bool } type SBOMsFromFilesystemConfig struct { @@ -42,7 +42,7 @@ type options struct { tags []string githubUsername, githubAPIToken, organization string // TODO Move later on to a separate GitHub client dependencyTrackClient *dtrack.DependencyTrackClient - purgeCache bool + purgeCache, softExit bool } type Option func(options *options) error @@ -96,6 +96,13 @@ func WithCachePurge() Option { } } +func WithSoftExit() Option { + return func(options *options) error { + options.softExit = true + return nil + } +} + func WithTags(tags []string) Option { return func(options *options) error { options.tags = tags @@ -130,6 +137,7 @@ func New(outputFile string, opts ...Option) (*App, error) { app.tags = options.tags app.purgeCache = options.purgeCache + app.softExit = options.softExit app.dependencyTrackClient = options.dependencyTrackClient app.organization = options.organization @@ -440,8 +448,11 @@ func (a App) cleanup() { removeDirectory := func(directoryPath string) { if _, err := os.Stat(directoryPath); !os.IsNotExist(err) { if err = os.RemoveAll(directoryPath); err != nil { - exitCode = 2 // ENOENT - + if a.softExit { + exitCode = 0 + } else { + exitCode = 2 // ENOENT + } log.WithError(err).Errorf("can't remove %s", directoryPath) } }