Skip to content

Commit

Permalink
feat: add detailed exit code flag
Browse files Browse the repository at this point in the history
  • Loading branch information
olblak committed Jan 14, 2025
1 parent 303acfe commit 18c8688
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
24 changes: 18 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ import (
)

var (
configFile string
e engine.Engine
dryRun bool
cleanRun bool
configFile string
e engine.Engine
dryRun bool
cleanRun bool
detailedExitCode bool

detailedExitCodeCmdDescription string = `Returns a detailed exit code when the command exits.
When provided, this argument changes the exit codes and their meanings
to provide more granular information about what the resulting plan contains:
0 = Succeeded with empty diff (no changes)
1 = Error
2 = Succeeded with non-empty diff (changes present)
`

rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -43,10 +53,10 @@ var (
}
if err != nil {
fmt.Printf("Failed to run releasepost: %v", err)
os.Exit(2)
os.Exit(1)
}

os.Exit(result.ChangelogResult.ExitCode())
os.Exit(result.ChangelogResult.ExitCode(detailedExitCode))
},
Use: "releasepost",
Short: "releasepost is a release note town crier",
Expand All @@ -64,7 +74,9 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "releasepost configuration file")
rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "Dry run mode")
rootCmd.PersistentFlags().BoolVar(&cleanRun, "clean", false, "Clean run, removes files from changelog directories not created by releasepost.")
rootCmd.PersistentFlags().BoolVar(&detailedExitCode, "detailed-exit-code", false, detailedExitCodeCmdDescription)
rootCmd.AddCommand(

versionCmd,
)
}
Expand Down
9 changes: 7 additions & 2 deletions internal/core/result/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ type Changelog struct {
UnTracked []string `json:"untracked"`
}

func (c Changelog) ExitCode() int {
func (c Changelog) ExitCode(detailedExitCode bool) int {

if !detailedExitCode {
return 0
}

if len(c.Created) == 0 && len(c.Modified) == 0 {
return 0
}
return 1
return 2
}

// UpdateResult update the result with the current changelog information
Expand Down

0 comments on commit 18c8688

Please sign in to comment.