Skip to content

Commit

Permalink
adding exit silently
Browse files Browse the repository at this point in the history
  • Loading branch information
ohcnivek committed May 23, 2023
1 parent 8aa0ebe commit fbd2a0c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cmd/av/commit_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func init() {
_, _ = fmt.Fprint(os.Stderr,
"\n", colors.Failure("Failed to create commit."), "\n",
)
return errExitSilently{1}
return actions.ErrExitSilently{ExitCode: 1}
}

state, err := actions.ReadStackSyncState(repo)
Expand Down
16 changes: 3 additions & 13 deletions cmd/av/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"emperror.dev/errors"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/gh"
"github.com/fatih/color"
Expand Down Expand Up @@ -94,18 +95,7 @@ func init() {
)
}

// errExitSilently is an error type that indicates that program should exit
// without printing any additional information with the given exit code.
// This is meant for cases where the running commands wants to manage its own
// error output but still needs to return a non-zero exit code (since returning
// nil from RunE would cause a exit with a zero code).
type errExitSilently struct {
exitCode int
}

func (e errExitSilently) Error() string {
return "<exit silently>"
}

func main() {
// Note: this doesn't include whatever time is spent in initializing the
Expand All @@ -114,9 +104,9 @@ func main() {
err := rootCmd.Execute()
logrus.WithField("duration", time.Since(startTime)).Debug("command exited")
checkCliVersion()
var exitSilently errExitSilently
var exitSilently actions.ErrExitSilently
if errors.As(err, &exitSilently) {
os.Exit(exitSilently.exitCode)
os.Exit(exitSilently.ExitCode)
}
if err != nil {
// In debug mode, show more detailed information about the error
Expand Down
8 changes: 5 additions & 3 deletions cmd/av/stack_branchcommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"fmt"
"github.com/aviator-co/av/internal/utils/cleanup"
"github.com/aviator-co/av/internal/utils/colors"
"os"
"regexp"
"strings"

"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/utils/cleanup"
"github.com/aviator-co/av/internal/utils/colors"

"emperror.dev/errors"
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
Expand Down Expand Up @@ -173,7 +175,7 @@ var stackBranchCommitCmd = &cobra.Command{
_, _ = fmt.Fprint(os.Stderr,
"\n", colors.Failure("Failed to create commit."), "\n",
)
return errExitSilently{1}
return actions.ErrExitSilently{ExitCode: 1}
}

// Cancel the cleanup **after** the commit is successful (so that we
Expand Down
41 changes: 3 additions & 38 deletions cmd/av/stack_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,47 +239,12 @@ base branch.
if err != nil {
return err
}
for i, currentBranch := range branchesToSync {
if i > 0 {
// Add spacing in the output between each branch sync
_, _ = fmt.Fprint(os.Stderr, "\n\n")
}
state.CurrentBranch = currentBranch
cont, err := actions.SyncBranch(ctx, repo, client, tx, actions.SyncBranchOpts{
Branch: currentBranch,
Fetch: !state.Config.NoFetch,
Push: !state.Config.NoPush,
Continuation: state.Continuation,
ToTrunk: state.Config.Trunk,
Skip: syncFlags.Skip,
})
if err != nil {
return err
}
if cont != nil {
state.Continuation = cont
if err := actions.WriteStackSyncState(repo, &state); err != nil {
return errors.Wrap(err, "failed to write stack sync state")
}
if err := tx.Commit(); err != nil {
return err
}
return errExitSilently{1}
}

state.Continuation = nil
}

// Return to the original branch
if _, err := repo.CheckoutBranch(&git.CheckoutBranch{Name: state.OriginalBranch}); err != nil {
return err
}
if err := actions.WriteStackSyncState(repo, nil); err != nil {
return errors.Wrap(err, "failed to write stack sync state")
}
if err := tx.Commit(); err != nil {
err = actions.SyncStack(ctx, repo, client, tx, branchesToSync, state, syncFlags)
if err != nil {
return err
}

return nil
},
}
Expand Down
14 changes: 14 additions & 0 deletions internal/actions/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@ package actions
import "emperror.dev/errors"

var ErrRepoNotInitialized = errors.Sentinel("this repository is not initialized; please run `av init`")


// errExitSilently is an error type that indicates that program should exit
// without printing any additional information with the given exit code.
// This is meant for cases where the running commands wants to manage its own
// error output but still needs to return a non-zero exit code (since returning
// nil from RunE would cause a exit with a zero code).
type ErrExitSilently struct {
ExitCode int
}

func (e ErrExitSilently) Error() string {
return "<exit silently>"
}
2 changes: 1 addition & 1 deletion internal/actions/sync_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func SyncStack( ctx context.Context,
if err := tx.Commit(); err != nil {
return err
}
// TODO: Exit silently here.
return ErrExitSilently{ExitCode: 1}
}
state.Continuation = nil
}
Expand Down

0 comments on commit fbd2a0c

Please sign in to comment.