Skip to content

Commit

Permalink
feat: add dryrun execution mode
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Vernin <[email protected]>
  • Loading branch information
olblak committed Jan 3, 2024
1 parent 1ede12f commit c967d2b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (

"github.com/spf13/cobra"
"github.com/updatecli/releasepost/internal/core/config"
"github.com/updatecli/releasepost/internal/core/dryrun"
"github.com/updatecli/releasepost/internal/core/engine"
"github.com/updatecli/releasepost/internal/core/result"
)

var (
configFile string
e engine.Engine
dryRun bool

rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -22,6 +24,12 @@ var (
fmt.Printf("Failed to initialize releasepost: %v", err)
os.Exit(2)
}

fmt.Println("Running releasepost")
if dryRun {
dryrun.Enabled = true
fmt.Println("Dry run mode enabled, no changelog will be saved to disk")
}
err = e.Run()
if err != nil {
fmt.Printf("Failed to run releasepost: %v", err)
Expand All @@ -42,6 +50,7 @@ and then copy them to locally to a directory of your choice.

func init() {
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Releasepost configuration file")
rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "Dry run mode")
rootCmd.AddCommand(
versionCmd,
)
Expand Down
21 changes: 12 additions & 9 deletions internal/core/changelog/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"

"github.com/updatecli/releasepost/internal/core/dryrun"
"github.com/updatecli/releasepost/internal/core/result"
)

Expand All @@ -26,15 +27,17 @@ func dataToFile(data []byte, filename string) error {
currentChecksum := getChecksumFromFile(filename)
newChecksum := getChecksumFromByte(data)

f, err := os.Create(filename)
if err != nil {
fmt.Printf("creating file %s: %v", filename, err)
return err
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
fmt.Printf("writing file %s: %v", filename, err)
if !dryrun.Enabled {
f, err := os.Create(filename)
if err != nil {
fmt.Printf("creating file %s: %v", filename, err)
return err
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
fmt.Printf("writing file %s: %v", filename, err)
}
}

if currentChecksum == "" && newChecksum != "" {
Expand Down
6 changes: 6 additions & 0 deletions internal/core/dryrun/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dryrun

var (
// Enabled is a global variable to enable or disable dry-run mode
Enabled bool
)

0 comments on commit c967d2b

Please sign in to comment.