Skip to content

Commit

Permalink
feat: improve the error handling to bubble up
Browse files Browse the repository at this point in the history
  • Loading branch information
gsdevme committed Feb 9, 2022
1 parent 8698fce commit 60bfe5c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 1 addition & 3 deletions internal/app/gitops-commit/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ func newRunCommand() *cobra.Command {

defer c()

err = gitops.DeployVersionHandler(gitops.DeployVersionCommand{
return gitops.DeployVersionHandler(gitops.DeployVersionCommand{
GitOptions: *options,
Repository: repo,
Notation: notation,
File: file,
Version: newVersion,
})

return err
},
}

Expand Down
14 changes: 8 additions & 6 deletions internal/pkg/gitops/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func NewGitOptions(keys *ssh.PublicKeys) (*GitOptions, func(), error) {
}, nil
}

func PushVersion(r *git.Repository, options *GitOptions, file string, message string) {
func PushVersion(r *git.Repository, options *GitOptions, file string, message string) error {
tree, err := r.Worktree()
if err != nil {
panic(err)
return err
}

_, err = tree.Add(file)

if err != nil {
panic(err)
return fmt.Errorf("failed to stage file for comit:%w", err)
}

commit, err := tree.Commit(message, &git.CommitOptions{
Expand All @@ -59,22 +59,24 @@ func PushVersion(r *git.Repository, options *GitOptions, file string, message st
})

if err != nil {
panic(err)
return fmt.Errorf("failed to commit: %w", err)
}

_, err = r.CommitObject(commit)

if err != nil {
panic(err)
return fmt.Errorf("failed to commit: %w", err)
}

err = r.Push(&git.PushOptions{
Auth: options.Keys,
})

if err != nil {
panic(err)
return fmt.Errorf("failed to push change to the repo: %w", err)
}

return nil
}

func GetPasswordlessKey(key string) (*ssh.PublicKeys, error) {
Expand Down
4 changes: 1 addition & 3 deletions internal/pkg/gitops/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ func DeployVersionHandler(c DeployVersionCommand) error {
return fmt.Errorf("cannot write new version: %w", err)
}

PushVersion(r, &c.GitOptions, c.File, fmt.Sprintf("ci: update tag to %s", c.Version))

return nil
return PushVersion(r, &c.GitOptions, c.File, fmt.Sprintf("ci: update tag to %s", c.Version))
}

0 comments on commit 60bfe5c

Please sign in to comment.