Skip to content
This repository was archived by the owner on Feb 12, 2026. It is now read-only.

Commit c5ac8c4

Browse files
authored
fix: centralize error handling in main commands (#5)
1 parent 5576b1a commit c5ac8c4

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

  • cmd/github-actions-digest-pinner

cmd/github-actions-digest-pinner/main.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ var (
2222
date = "unknown"
2323
)
2424

25+
// Centralized error handling function
26+
func handleError(err error, message string, exit bool) {
27+
if err != nil {
28+
log.Printf("%s: %v", message, err)
29+
if exit {
30+
os.Exit(1)
31+
}
32+
}
33+
}
34+
2535
func main() {
2636
rootCmd := &cobra.Command{
2737
Use: "github-actions-digest-pinner",
@@ -30,9 +40,7 @@ func main() {
3040
GitHub Actions to specific digests for better security and reliability.`,
3141
Run: func(cmd *cobra.Command, args []string) {
3242
err := cmd.Help()
33-
if err != nil {
34-
log.Fatalf("Failed to display help: %v", err)
35-
}
43+
handleError(err, "Failed to display help", true)
3644
},
3745
}
3846

@@ -65,10 +73,7 @@ GitHub Actions to specific digests for better security and reliability.`,
6573
}
6674

6775
files, err := finder.FindWorkflowFiles(fsys)
68-
69-
if err != nil {
70-
log.Fatalf("Failed to find workflow files: %v", err)
71-
}
76+
handleError(err, "Failed to find workflow files", true)
7277

7378
if verbose {
7479
log.Printf("Found %d workflow files", len(files))
@@ -81,19 +86,13 @@ GitHub Actions to specific digests for better security and reliability.`,
8186
}
8287

8388
content, err := fsys.Open(file)
84-
if err != nil {
85-
log.Fatalf("Failed to read file %s: %v", file, err)
86-
}
89+
handleError(err, fmt.Sprintf("Failed to read file %s", file), true)
8790

8891
fileContent, err := io.ReadAll(content)
89-
if err != nil {
90-
log.Fatalf("Failed to read content of file %s: %v", file, err)
91-
}
92+
handleError(err, fmt.Sprintf("Failed to read content of file %s", file), true)
9293

9394
actions, err := parser.ParseWorkflowActions(fileContent)
94-
if err != nil {
95-
log.Fatalf("Failed to parse actions in file %s: %v", file, err)
96-
}
95+
handleError(err, fmt.Sprintf("Failed to parse actions in file %s", file), true)
9796

9897
log.Printf("Found %d actions in file %s", len(actions), file)
9998

@@ -136,19 +135,14 @@ GitHub Actions to specific digests for better security and reliability.`,
136135
}
137136

138137
files, err := finder.FindWorkflowFiles(fsys)
139-
140-
if err != nil {
141-
log.Fatalf("Failed to find workflow files: %v", err)
142-
}
138+
handleError(err, "Failed to find workflow files", true)
143139

144140
if verbose {
145141
log.Printf("Found %d workflow files", len(files))
146142
}
147143

148144
totalUpdates, err := workflowUpdater.UpdateWorkflows(ctx, fsys)
149-
if err != nil {
150-
log.Fatalf("Failed to update workflows: %v", err)
151-
}
145+
handleError(err, "Failed to update workflows", true)
152146

153147
log.Printf("Updated %d action references in %v", totalUpdates, time.Since(start).Round(time.Millisecond))
154148

0 commit comments

Comments
 (0)