Skip to content

Commit 60bfe5c

Browse files
committed
feat: improve the error handling to bubble up
1 parent 8698fce commit 60bfe5c

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

internal/app/gitops-commit/cmd/run.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ func newRunCommand() *cobra.Command {
3737

3838
defer c()
3939

40-
err = gitops.DeployVersionHandler(gitops.DeployVersionCommand{
40+
return gitops.DeployVersionHandler(gitops.DeployVersionCommand{
4141
GitOptions: *options,
4242
Repository: repo,
4343
Notation: notation,
4444
File: file,
4545
Version: newVersion,
4646
})
47-
48-
return err
4947
},
5048
}
5149

internal/pkg/gitops/git.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ func NewGitOptions(keys *ssh.PublicKeys) (*GitOptions, func(), error) {
3838
}, nil
3939
}
4040

41-
func PushVersion(r *git.Repository, options *GitOptions, file string, message string) {
41+
func PushVersion(r *git.Repository, options *GitOptions, file string, message string) error {
4242
tree, err := r.Worktree()
4343
if err != nil {
44-
panic(err)
44+
return err
4545
}
4646

4747
_, err = tree.Add(file)
4848

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

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

6161
if err != nil {
62-
panic(err)
62+
return fmt.Errorf("failed to commit: %w", err)
6363
}
6464

6565
_, err = r.CommitObject(commit)
6666

6767
if err != nil {
68-
panic(err)
68+
return fmt.Errorf("failed to commit: %w", err)
6969
}
7070

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

7575
if err != nil {
76-
panic(err)
76+
return fmt.Errorf("failed to push change to the repo: %w", err)
7777
}
78+
79+
return nil
7880
}
7981

8082
func GetPasswordlessKey(key string) (*ssh.PublicKeys, error) {

internal/pkg/gitops/handler.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,5 @@ func DeployVersionHandler(c DeployVersionCommand) error {
3838
return fmt.Errorf("cannot write new version: %w", err)
3939
}
4040

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

0 commit comments

Comments
 (0)