|
7 | 7 | "context" |
8 | 8 | "errors" |
9 | 9 | "fmt" |
10 | | - "log" |
11 | 10 | "os" |
12 | 11 |
|
13 | 12 | "github.com/spf13/cobra" |
@@ -39,56 +38,56 @@ var ( |
39 | 38 | Long: `Determines the SLSA Source Level of the repo. |
40 | 39 |
|
41 | 40 | 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) |
44 | 43 | }, |
45 | 44 | } |
46 | 45 | ) |
47 | 46 |
|
48 | | -func doCheckLevel(cla *CheckLevelArgs) { |
| 47 | +func doCheckLevel(cla *CheckLevelArgs) error { |
49 | 48 | if err := cla.Validate(); err != nil { |
50 | | - log.Fatalf("Error: %v", err) |
| 49 | + return err |
51 | 50 | } |
52 | 51 |
|
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) |
56 | 53 | ghconnection.Options.AllowMergeCommits = cla.allowMergeCommits |
57 | 54 |
|
58 | 55 | ctx := context.Background() |
59 | 56 | controlStatus, err := ghconnection.GetBranchControls(ctx, cla.commit, ghconnection.GetFullRef()) |
60 | 57 | if err != nil { |
61 | | - log.Fatal(err) |
| 58 | + return err |
62 | 59 | } |
63 | 60 | pe := policy.NewPolicyEvaluator() |
64 | 61 | pe.UseLocalPolicy = checkLevelProvArgs.useLocalPolicy |
65 | 62 | verifiedLevels, policyPath, err := pe.EvaluateControl(ctx, ghconnection, controlStatus) |
66 | 63 | if err != nil { |
67 | | - log.Fatal(err) |
| 64 | + return err |
68 | 65 | } |
69 | 66 | fmt.Print(verifiedLevels) |
70 | 67 |
|
71 | 68 | unsignedVsa, err := attest.CreateUnsignedSourceVsa(ghconnection.GetRepoUri(), ghconnection.GetFullRef(), cla.commit, verifiedLevels, policyPath) |
72 | 69 | if err != nil { |
73 | | - log.Fatal(err) |
| 70 | + return err |
74 | 71 | } |
75 | 72 | 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 |
78 | 75 | } |
79 | 76 | } |
80 | 77 |
|
81 | 78 | if cla.outputVsa != "" { |
82 | 79 | // This will output in the sigstore bundle format. |
83 | 80 | signedVsa, err := attest.Sign(unsignedVsa) |
84 | 81 | if err != nil { |
85 | | - log.Fatal(err) |
| 82 | + return err |
86 | 83 | } |
87 | 84 | err = os.WriteFile(cla.outputVsa, []byte(signedVsa), 0o644) //nolint:gosec |
88 | 85 | if err != nil { |
89 | | - log.Fatal(err) |
| 86 | + return err |
90 | 87 | } |
91 | 88 | } |
| 89 | + |
| 90 | + return nil |
92 | 91 | } |
93 | 92 |
|
94 | 93 | func init() { |
|
0 commit comments