Skip to content

Commit

Permalink
Better usage of cli library for exit handler
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Apr 20, 2023
1 parent 4594c82 commit 82d4228
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/sfreleaser/bashism.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func runSilent(inputs ...string) (output string, err error) {
output, info, err := internalMaybeRun(inputs, true)
if err != nil {
zlog.Debug("run command failed", zap.Stringer("cmd", info), zap.Error(err), zap.String("output", output))
osExit(1)
cli.Exit(1)
}

return output, nil
Expand Down
11 changes: 0 additions & 11 deletions cmd/sfreleaser/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
"os"
"strings"
"unicode"

Expand Down Expand Up @@ -44,13 +43,3 @@ func mapEachReaderLine[T any](reader io.Reader, fn func(line string) T) (out []T
func dedent(format string, args ...any) string {
return fmt.Sprintf(cli.Dedent(format), args...)
}

var onExits []func()

func osExit(code int) {
for _, onExit := range onExits {
onExit()
}

os.Exit(code)
}
2 changes: 1 addition & 1 deletion cmd/sfreleaser/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func ensureGitNotDirty() {
if isGitDirty() {
fmt.Println("Your git repository is dirty, refusing to release (use --allow-dirty to releaser while being Git dirty)")
run("git status")
osExit(1)
cli.Exit(1)
}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/sfreleaser/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,17 @@ func deleteExistingRelease(version string) {
run("gh release delete --yes", version)
}

func publishReleaseNow(version string, published *bool) {
func publishReleaseNow(version string) {
fmt.Println("Publishing release right now")
runSilent("gh release edit", version, "--draft=false")

// We re-fetch the releaseURL here because it changed from before publish
fmt.Printf("Release published at %s\n", releaseURL(version))

cli.ExitHandler(deleteTagExitHandlerID, nil)

zlog.Debug("refreshing git tags now that release happened")
runSilent(fmt.Sprintf(`git fetch origin +refs/tags/%s:refs/tags/%s`, version, version))

*published = true
}

func reviewRelease(releaseURL string) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/sfreleaser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"

"github.com/spf13/pflag"
"github.com/streamingfast/cli"
. "github.com/streamingfast/cli"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -61,6 +62,6 @@ func verifyCommand(command string, onErrorText string) {
fmt.Println()
fmt.Println(onErrorText)

osExit(1)
cli.Exit(1)
}
}
38 changes: 18 additions & 20 deletions cmd/sfreleaser/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"go.uber.org/zap"
)

const deleteTagExitHandlerID = "delete-tag"

var ReleaseCmd = Command(release,
"release [<version>]",
"Perform the actual release",
Expand Down Expand Up @@ -60,24 +62,23 @@ var ReleaseCmd = Command(release,

go func() {
<-sigs
osExit(1)
cli.Exit(1)
}()

cli.OnQuit = func(message string) {
if message != "" {
fmt.Println(message)
}
osExit(1)
}

if err := release(cmd, args); err != nil {
fmt.Println("Error", err.Error())
osExit(1)
// Let the normal flow happen, we trap error and exit properly
return err
}

osExit(0)
// Forces our exit handler (if any) to run
cli.Exit(0)
return nil
}),
OnCommandError(func(err error) {
fmt.Println()
fmt.Println("Error:", err.Error())
cli.Exit(1)
}),
)

func release(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -152,12 +153,9 @@ func release(cmd *cobra.Command, args []string) error {
fmt.Println("Creating temporary tag so that goreleaser can work properly")
run("git tag", version)

publishedNowSuccesfully := false
onExits = append(onExits, func() {
if !publishedNowSuccesfully {
zlog.Debug("Deleting local temporary tag")
runSilent("git tag -d", version)
}
cli.ExitHandler(deleteTagExitHandlerID, func(_ int) {
zlog.Debug("Deleting local temporary tag")
runSilent("git tag -d", version)
})

if !devSkipGoreleaser {
Expand All @@ -169,7 +167,7 @@ func release(cmd *cobra.Command, args []string) error {
"run",
"--rm",
"-e CGO_ENABLED=1",
"--env-file", "build/.env.release",
"--env-file", "build/.env.r2elease",
"-v /var/run/docker.sock:/var/run/docker.sock",
"-v", cli.WorkingDirectory() + ":/go/src/work",
"-w /go/src/work",
Expand Down Expand Up @@ -197,7 +195,7 @@ func release(cmd *cobra.Command, args []string) error {
releaseURL := releaseURL(version)

if publishNow {
publishReleaseNow(version, &publishedNowSuccesfully)
publishReleaseNow(version)
} else {
fmt.Println()
fmt.Println(dedent(`
Expand Down Expand Up @@ -225,7 +223,7 @@ func release(cmd *cobra.Command, args []string) error {

fmt.Println()
if yes, _ := cli.PromptConfirm("Publish release right now?"); yes {
publishReleaseNow(version, &publishedNowSuccesfully)
publishReleaseNow(version)
}

fmt.Println("Completed")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/streamingfast/cli v0.0.4-0.20230420050158-aee4f9637f40
github.com/streamingfast/cli v0.0.4-0.20230420210357-3bebdb9f2fbf
github.com/streamingfast/logging v0.0.0-20221209193439-bff11742bf4c
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.21.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU=
github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA=
github.com/streamingfast/cli v0.0.4-0.20230420050158-aee4f9637f40 h1:FJnFmDJKxVneqrV+cqQft+te8GAI4w8JWGqCvWv5rlA=
github.com/streamingfast/cli v0.0.4-0.20230420050158-aee4f9637f40/go.mod h1:Z31J79/Litacp7iS0//7E8IQLh2JcLOYvPVsWWiHjEE=
github.com/streamingfast/cli v0.0.4-0.20230420210357-3bebdb9f2fbf h1:RFWFe6f4CdyzUW1rMuEg21eD2JZ59MKdco7luk1qzkw=
github.com/streamingfast/cli v0.0.4-0.20230420210357-3bebdb9f2fbf/go.mod h1:OzwQE+cSMmLwO1U2rcwQz4UBMEW3lDHA2UeDg0PMCwY=
github.com/streamingfast/logging v0.0.0-20221209193439-bff11742bf4c h1:dV1ye/S2PiW9uIWvLtMrxWoTLcZS+yhjZDSKEV102Ho=
github.com/streamingfast/logging v0.0.0-20221209193439-bff11742bf4c/go.mod h1:VlduQ80JcGJSargkRU4Sg9Xo63wZD/l8A5NC/Uo1/uU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit 82d4228

Please sign in to comment.