Skip to content

Commit 7ccf09f

Browse files
authored
chore: Update CLI to use errors and Cobra's RunE (#200)
This improves the control flow. When RunE is used, if the error is non- nil, the error will be shown to the user. Signed-off-by: James Alseth <[email protected]>
1 parent 6079358 commit 7ccf09f

File tree

7 files changed

+41
-50
lines changed

7 files changed

+41
-50
lines changed

sourcetool/cmd/audit.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"log"
1110

1211
"github.com/spf13/cobra"
1312

@@ -87,11 +86,8 @@ Future:
8786
* Check the provenance to validate the verifiedLevels in the VSA match expectations
8887
(i.e. that the VSA was issued correctly)
8988
`,
90-
Run: func(cmd *cobra.Command, args []string) {
91-
err := doAudit(auditArgs)
92-
if err != nil {
93-
log.Fatal(err)
94-
}
89+
RunE: func(cmd *cobra.Command, args []string) error {
90+
return doAudit(auditArgs)
9591
},
9692
}
9793
)

sourcetool/cmd/checklevel.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"log"
1110
"os"
1211

1312
"github.com/spf13/cobra"
@@ -39,56 +38,56 @@ var (
3938
Long: `Determines the SLSA Source Level of the repo.
4039
4140
This is meant to be run within the corresponding GitHub Actions workflow.`,
42-
Run: func(cmd *cobra.Command, args []string) {
43-
doCheckLevel(&checkLevelArgs)
41+
RunE: func(cmd *cobra.Command, args []string) error {
42+
return doCheckLevel(&checkLevelArgs)
4443
},
4544
}
4645
)
4746

48-
func doCheckLevel(cla *CheckLevelArgs) {
47+
func doCheckLevel(cla *CheckLevelArgs) error {
4948
if err := cla.Validate(); err != nil {
50-
log.Fatalf("Error: %v", err)
49+
return err
5150
}
5251

53-
ghconnection := ghcontrol.NewGhConnection(
54-
cla.owner, cla.repo, ghcontrol.BranchToFullRef(cla.branch),
55-
).WithAuthToken(githubToken)
52+
ghconnection := ghcontrol.NewGhConnection(cla.owner, cla.repo, ghcontrol.BranchToFullRef(cla.branch)).WithAuthToken(githubToken)
5653
ghconnection.Options.AllowMergeCommits = cla.allowMergeCommits
5754

5855
ctx := context.Background()
5956
controlStatus, err := ghconnection.GetBranchControls(ctx, cla.commit, ghconnection.GetFullRef())
6057
if err != nil {
61-
log.Fatal(err)
58+
return err
6259
}
6360
pe := policy.NewPolicyEvaluator()
6461
pe.UseLocalPolicy = checkLevelProvArgs.useLocalPolicy
6562
verifiedLevels, policyPath, err := pe.EvaluateControl(ctx, ghconnection, controlStatus)
6663
if err != nil {
67-
log.Fatal(err)
64+
return err
6865
}
6966
fmt.Print(verifiedLevels)
7067

7168
unsignedVsa, err := attest.CreateUnsignedSourceVsa(ghconnection.GetRepoUri(), ghconnection.GetFullRef(), cla.commit, verifiedLevels, policyPath)
7269
if err != nil {
73-
log.Fatal(err)
70+
return err
7471
}
7572
if cla.outputUnsignedVsa != "" {
76-
if err = os.WriteFile(cla.outputUnsignedVsa, []byte(unsignedVsa), 0o644); err != nil { //nolint:gosec
77-
log.Fatal(err)
73+
if err := os.WriteFile(cla.outputUnsignedVsa, []byte(unsignedVsa), 0o644); err != nil { //nolint:gosec
74+
return err
7875
}
7976
}
8077

8178
if cla.outputVsa != "" {
8279
// This will output in the sigstore bundle format.
8380
signedVsa, err := attest.Sign(unsignedVsa)
8481
if err != nil {
85-
log.Fatal(err)
82+
return err
8683
}
8784
err = os.WriteFile(cla.outputVsa, []byte(signedVsa), 0o644) //nolint:gosec
8885
if err != nil {
89-
log.Fatal(err)
86+
return err
9087
}
9188
}
89+
90+
return nil
9291
}
9392

9493
func init() {

sourcetool/cmd/checklevelprov.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ var (
3939
checklevelprovCmd = &cobra.Command{
4040
Use: "checklevelprov",
4141
Short: "Checks the given commit against policy using & creating provenance",
42-
Run: func(cmd *cobra.Command, args []string) {
43-
if err := doCheckLevelProv(checkLevelProvArgs); err != nil {
44-
log.Fatal(err)
45-
}
42+
RunE: func(cmd *cobra.Command, args []string) error {
43+
return doCheckLevelProv(checkLevelProvArgs)
4644
},
4745
}
4846
)

sourcetool/cmd/checktag.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ var (
3434
checktagCmd = &cobra.Command{
3535
Use: "checktag",
3636
Short: "Checks to see if the tag operation should be allowed and issues a VSA",
37-
Run: func(cmd *cobra.Command, args []string) {
38-
if err := doCheckTag(checkTagArgs); err != nil {
39-
log.Fatal(err)
40-
}
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
return doCheckTag(checkTagArgs)
4139
},
4240
}
4341
)

sourcetool/cmd/createpolicy.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"context"
88
"fmt"
9-
"log"
109

1110
"github.com/spf13/cobra"
1211

@@ -28,20 +27,21 @@ var (
2827
Long: `Creates a SLSA source policy in a local copy of slsa-source-poc.
2928
3029
The created policy should then be sent as a PR to slsa-framework/slsa-source-poc.`,
31-
Run: func(cmd *cobra.Command, args []string) {
32-
doCreatePolicy(createPolicyArgs.policyRepoPath, createPolicyArgs.owner, createPolicyArgs.repo, createPolicyArgs.branch)
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
return doCreatePolicy(createPolicyArgs.policyRepoPath, createPolicyArgs.owner, createPolicyArgs.repo, createPolicyArgs.branch)
3332
},
3433
}
3534
)
3635

37-
func doCreatePolicy(policyRepoPath, owner, repo, branch string) {
36+
func doCreatePolicy(policyRepoPath, owner, repo, branch string) error {
3837
ghconnection := ghcontrol.NewGhConnection(owner, repo, ghcontrol.BranchToFullRef(branch)).WithAuthToken(githubToken)
3938
ctx := context.Background()
4039
outpath, err := policy.CreateLocalPolicy(ctx, ghconnection, policyRepoPath)
4140
if err != nil {
42-
log.Fatal(err)
41+
return err
4342
}
4443
fmt.Printf("Wrote policy to %s\n", outpath)
44+
return nil
4545
}
4646

4747
func init() {

sourcetool/cmd/prov.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"context"
88
"fmt"
9-
"log"
109

1110
"github.com/spf13/cobra"
1211
"google.golang.org/protobuf/encoding/protojson"
@@ -25,25 +24,26 @@ var (
2524
provCmd = &cobra.Command{
2625
Use: "prov",
2726
Short: "Creates provenance for the given commit, but does not check policy.",
28-
Run: func(cmd *cobra.Command, args []string) {
29-
doProv(provArgs.prevAttPath, provArgs.commit, provArgs.prevCommit, provArgs.owner, provArgs.repo, provArgs.branch)
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
return doProv(provArgs.prevAttPath, provArgs.commit, provArgs.prevCommit, provArgs.owner, provArgs.repo, provArgs.branch)
3029
},
3130
}
3231
)
3332

34-
func doProv(prevAttPath, commit, prevCommit, owner, repo, branch string) {
33+
func doProv(prevAttPath, commit, prevCommit, owner, repo, branch string) error {
3534
ghconnection := ghcontrol.NewGhConnection(owner, repo, ghcontrol.BranchToFullRef(branch)).WithAuthToken(githubToken)
3635
ctx := context.Background()
3736
pa := attest.NewProvenanceAttestor(ghconnection, getVerifier())
3837
newProv, err := pa.CreateSourceProvenance(ctx, prevAttPath, commit, prevCommit, ghconnection.GetFullRef())
3938
if err != nil {
40-
log.Fatal(err)
39+
return err
4140
}
4241
provStr, err := protojson.Marshal(newProv)
4342
if err != nil {
44-
log.Fatal(err)
43+
return err
4544
}
46-
fmt.Printf("%s\n", string(provStr))
45+
fmt.Println(string(provStr))
46+
return nil
4747
}
4848

4949
func init() {

sourcetool/cmd/verifycommit.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"context"
88
"fmt"
9-
"log"
109

1110
"github.com/spf13/cobra"
1211

@@ -24,40 +23,41 @@ var (
2423
verifycommitCmd = &cobra.Command{
2524
Use: "verifycommit",
2625
Short: "Verifies the specified commit is valid",
27-
Run: func(cmd *cobra.Command, args []string) {
28-
doVerifyCommit(verifyCommitArgs.commit, verifyCommitArgs.owner, verifyCommitArgs.repo, verifyCommitArgs.branch, verifyCommitArgs.tag)
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
return doVerifyCommit(verifyCommitArgs.commit, verifyCommitArgs.owner, verifyCommitArgs.repo, verifyCommitArgs.branch, verifyCommitArgs.tag)
2928
},
3029
}
3130
)
3231

33-
func doVerifyCommit(commit, owner, repo, branch, tag string) {
32+
func doVerifyCommit(commit, owner, repo, branch, tag string) error {
3433
if commit == "" || owner == "" || repo == "" {
35-
log.Fatal("Must set commit, owner and repo.")
34+
return fmt.Errorf("must set commit, owner and repo")
3635
}
3736

38-
ref := ""
37+
var ref string
3938
switch {
4039
case branch != "":
4140
ref = ghcontrol.BranchToFullRef(branch)
4241
case tag != "":
4342
ref = ghcontrol.TagToFullRef(tag)
4443
default:
45-
log.Fatal("Must specify either branch or tag.")
44+
return fmt.Errorf("must specify either branch or tag")
4645
}
4746

4847
ghconnection := ghcontrol.NewGhConnection(owner, repo, ref).WithAuthToken(githubToken)
4948
ctx := context.Background()
5049

5150
_, vsaPred, err := attest.GetVsa(ctx, ghconnection, getVerifier(), commit, ghconnection.GetFullRef())
5251
if err != nil {
53-
log.Fatal(err)
52+
return err
5453
}
5554
if vsaPred == nil {
5655
fmt.Printf("FAILED: no VSA matching commit '%s' on branch '%s' found in github.com/%s/%s\n", commit, branch, owner, repo)
57-
return
56+
return nil
5857
}
5958

6059
fmt.Printf("SUCCESS: commit %s verified with %v\n", commit, vsaPred.GetVerifiedLevels())
60+
return nil
6161
}
6262

6363
func init() {

0 commit comments

Comments
 (0)