Skip to content

Commit

Permalink
Add soft-exit
Browse files Browse the repository at this point in the history
  • Loading branch information
ysebyy committed Jul 30, 2024
1 parent 2ed8ed8 commit 1766438
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
classifierFlag = "classifier"
uploadToDTrackFlag = "upload-to-dependency-track"
purgeCacheFlag = "purge-cache"
softExitFlag = "soft-exit"
orgFlag = "organization"
)

Expand Down Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 1766438

Please sign in to comment.