Skip to content

Commit a15c0da

Browse files
committed
Correctly handle potential errors during CLI initialization
Signed-off-by: Noah Stride <noah.stride@goteleport.com>
1 parent d9e6fed commit a15c0da

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

cmd/main.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"log/slog"
67
"os"
78
"time"
89

@@ -17,23 +18,36 @@ var (
1718
)
1819

1920
func main() {
21+
rootCmd, err := newRootCmd()
22+
if err != nil {
23+
slog.Error("Failed to initialize CLI", "error", err)
24+
os.Exit(1)
25+
}
26+
27+
if err := rootCmd.Execute(); err != nil {
28+
slog.Error("Encountered a fatal error during execution", "error", err)
29+
os.Exit(1)
30+
}
31+
}
32+
33+
func newRootCmd() (*cobra.Command, error) {
2034
rootCmd := &cobra.Command{
2135
Use: "aws-spiffe-workload-helper",
2236
Short: "TODO", // TODO(strideynet): Helpful, short description.
2337
Long: `TODO`, // TODO(strideynet): Helpful, long description.
2438
Version: version,
2539
}
2640

27-
x509CredentialProcessCmd := newX509CredentialProcessCmd()
41+
x509CredentialProcessCmd, err := newX509CredentialProcessCmd()
42+
if err != nil {
43+
return nil, fmt.Errorf("initializing x509-credential-process command: %w", err)
44+
}
2845
rootCmd.AddCommand(x509CredentialProcessCmd)
2946

30-
if err := rootCmd.Execute(); err != nil {
31-
fmt.Println(err.Error())
32-
os.Exit(1)
33-
}
47+
return rootCmd, nil
3448
}
3549

36-
func newX509CredentialProcessCmd() *cobra.Command {
50+
func newX509CredentialProcessCmd() (*cobra.Command, error) {
3751
var (
3852
roleARN string
3953
region string
@@ -91,13 +105,19 @@ func newX509CredentialProcessCmd() *cobra.Command {
91105
},
92106
}
93107
cmd.Flags().StringVar(&roleARN, "role-arn", "", "The ARN of the role to assume. Required.")
94-
cmd.MarkFlagRequired("role-arn")
108+
if err := cmd.MarkFlagRequired("role-arn"); err != nil {
109+
return nil, fmt.Errorf("marking role-arn flag as required: %w", err)
110+
}
95111
cmd.Flags().StringVar(&region, "region", "", "The AWS region to use. Optional.")
96112
cmd.Flags().StringVar(&profileARN, "profile-arn", "", "The ARN of the Roles Anywhere profile to use. Required.")
97-
cmd.MarkFlagRequired("profile-arn")
113+
if err := cmd.MarkFlagRequired("profile-arn"); err != nil {
114+
return nil, fmt.Errorf("marking profile-arn flag as required: %w", err)
115+
}
98116
cmd.Flags().DurationVar(&sessionDuration, "session-duration", 0, "The duration of the resulting session. Optional. Can range from 15m to 12h.")
99117
cmd.Flags().StringVar(&trustAnchorARN, "trust-anchor-arn", "", "The ARN of the Roles Anywhere trust anchor to use. Required.")
100-
cmd.MarkFlagRequired("trust-anchor-arn")
118+
if err := cmd.MarkFlagRequired("trust-anchor-arn"); err != nil {
119+
return nil, fmt.Errorf("marking trust-anchor-arn flag as required: %w", err)
120+
}
101121
cmd.Flags().StringVar(&roleSessionName, "role-session-name", "", "The identifier for the role session. Optional.")
102-
return cmd
122+
return cmd, nil
103123
}

0 commit comments

Comments
 (0)