Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: log if already up-to-date
Browse files Browse the repository at this point in the history
seriouspoop committed Sep 4, 2024
1 parent e26c0fe commit e21c8ea
Showing 5 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion internal/handler/facade.go
Original file line number Diff line number Diff line change
@@ -15,6 +15,6 @@ type servicer interface {
SwitchBranchIfExists(branch model.Branch) (bool, error)
CreateBranchAndSwitch(branch model.Branch) error
CheckTestsAndRun() (bool, error)
Push(setUpstreamBranch bool) (output string, err error)
Push(setUpstreamBranch bool) error
SetRemoteSSHAuth() error
}
8 changes: 1 addition & 7 deletions internal/handler/run.go
Original file line number Diff line number Diff line change
@@ -113,11 +113,8 @@ func Run(s servicer) *cobra.Command {

// Push changes
utils.Logger(utils.LOG_INFO, "Pushing changes...")
output, err := s.Push(setUpstreamBranch)
err = s.Push(setUpstreamBranch)
if err != nil {
if output != "" {
fmt.Println(output)
}
if errors.Is(err, svc.ErrAuthNotFound) {
fmt.Println(heredoc.Doc(`
Auth credentials for current remote are missing.
@@ -126,9 +123,6 @@ func Run(s servicer) *cobra.Command {
}
return err
}
if output != "" {
fmt.Println(output)
}
return nil
},
}
5 changes: 3 additions & 2 deletions repo/git/git.go
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ type Errors struct {
InvalidAuthMethod error
InvalidPassphrase error
KeyNotSupported error
AlreadyUpToDate error
}

type Git struct {
@@ -166,7 +167,7 @@ func (g *Git) Pull(remote *model.Remote, branch model.Branch, auth *config.Crede
} else if errors.Is(err, git.ErrNonFastForwardUpdate) {
return g.err.PullFailed
} else if errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil
return g.err.AlreadyUpToDate
}
return err
}
@@ -264,7 +265,7 @@ func (g *Git) Push(remote *model.Remote, branch model.Branch, auth *config.Crede
if err != nil && strings.Contains(err.Error(), "unable to authenticate") {
return g.err.KeyNotSupported
} else if errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil
return g.err.AlreadyUpToDate
}
return err
}
1 change: 1 addition & 0 deletions svc/errors.go
Original file line number Diff line number Diff line change
@@ -20,4 +20,5 @@ var (
ErrInvalidPassphrase = errors.New("invalid passphrase")
ErrKeyNotSupported = errors.New("invalid key")
ErrAuthLoadFailed = errors.New("failed to load auth")
ErrAlreadyUpToDate = errors.New("already up to date")
)
37 changes: 22 additions & 15 deletions svc/repo.go
Original file line number Diff line number Diff line change
@@ -120,6 +120,10 @@ func (s *Svc) Pull(initial bool) error {
message := fmt.Sprintf("copy contents of %s.pub and upload the keys on %s", filepath.Join(os.Getenv("HOME"), gopushDir, keyName), remoteDetails.Provider().String())
utils.Logger(utils.LOG_STRICT_INFO, message)
}
if errors.Is(pullErr, ErrAlreadyUpToDate) {
utils.Logger(utils.LOG_SUCCESS, "already up-to-date")
return nil
}
return pullErr
}

@@ -191,59 +195,58 @@ func (s *Svc) StageChanges() error {
return nil
}

func (s *Svc) Push(setUpstreamBranch bool) (output string, err error) {
func (s *Svc) Push(setUpstreamBranch bool) error {
currBranch, err := s.bash.GetCurrentBranch()
if err != nil {
return "", err
return err
}
if setUpstreamBranch {
output, err = s.bash.Push(currBranch, true)
_, err = s.bash.Push(currBranch, true)
if err != nil {
fmt.Println(output)
return "", err
return err
}
} else {
remoteDetails, err := s.git.GetRemoteDetails()
if err != nil {
return "", err
return err
}

if remoteDetails == nil {
return "", ErrRemoteNotFound
return ErrRemoteNotFound
}

var providerAuth *config.Credentials
if remoteDetails.AuthMode() == model.AuthHTTP {
if s.cfg == nil {
return "", ErrConfigNotLoaded
return ErrConfigNotLoaded
}
providerAuth = s.cfg.ProviderAuth(remoteDetails.Provider())
if providerAuth == nil {
return "", ErrAuthNotFound
return ErrAuthNotFound
}
} else if remoteDetails.AuthMode() == model.AuthSSH {
if !s.passphrase.Valid() {
passphrase, err := utils.Prompt(true, false, "passphrase")
if err != nil {
return "", err
return err
}
s.passphrase = model.Password(passphrase)
}
providerAuth = &config.Credentials{
Token: s.passphrase.String(),
}
} else {
return "", ErrInvalidAuthMethod
return ErrInvalidAuthMethod
}

if providerAuth == nil {
return "", ErrAuthLoadFailed
return ErrAuthLoadFailed
}
pushErr := s.git.Push(remoteDetails, currBranch, providerAuth)
for errors.Is(pushErr, ErrInvalidPassphrase) {
passphrase, err := utils.Prompt(true, false, "invalid passphrase")
if err != nil {
return "", err
return err
}
s.passphrase = model.Password(passphrase)
providerAuth = &config.Credentials{
@@ -256,9 +259,13 @@ func (s *Svc) Push(setUpstreamBranch bool) (output string, err error) {
message := fmt.Sprintf("copy contents of %s.pub and upload the keys on %s", filepath.Join(os.Getenv("HOME"), gopushDir, keyName), remoteDetails.Provider().String())
utils.Logger(utils.LOG_STRICT_INFO, message)
}
return "", pushErr
if errors.Is(pushErr, ErrAlreadyUpToDate) {
utils.Logger(utils.LOG_SUCCESS, "already up-to-date")
return nil
}
return pushErr
}
utils.Logger(utils.LOG_SUCCESS, "push successful")
}
return "", err
return err
}

0 comments on commit e21c8ea

Please sign in to comment.