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

Add soft-exit #139

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
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
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
Loading