Skip to content

Commit

Permalink
Correctly handle potential errors during CLI initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Noah Stride <[email protected]>
  • Loading branch information
strideynet committed Oct 18, 2024
1 parent d9e6fed commit a15c0da
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"time"

Expand All @@ -17,23 +18,36 @@ var (
)

func main() {
rootCmd, err := newRootCmd()
if err != nil {
slog.Error("Failed to initialize CLI", "error", err)
os.Exit(1)
}

if err := rootCmd.Execute(); err != nil {
slog.Error("Encountered a fatal error during execution", "error", err)
os.Exit(1)
}
}

func newRootCmd() (*cobra.Command, error) {
rootCmd := &cobra.Command{
Use: "aws-spiffe-workload-helper",
Short: "TODO", // TODO(strideynet): Helpful, short description.
Long: `TODO`, // TODO(strideynet): Helpful, long description.
Version: version,
}

x509CredentialProcessCmd := newX509CredentialProcessCmd()
x509CredentialProcessCmd, err := newX509CredentialProcessCmd()
if err != nil {
return nil, fmt.Errorf("initializing x509-credential-process command: %w", err)
}
rootCmd.AddCommand(x509CredentialProcessCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return rootCmd, nil
}

func newX509CredentialProcessCmd() *cobra.Command {
func newX509CredentialProcessCmd() (*cobra.Command, error) {
var (
roleARN string
region string
Expand Down Expand Up @@ -91,13 +105,19 @@ func newX509CredentialProcessCmd() *cobra.Command {
},
}
cmd.Flags().StringVar(&roleARN, "role-arn", "", "The ARN of the role to assume. Required.")
cmd.MarkFlagRequired("role-arn")
if err := cmd.MarkFlagRequired("role-arn"); err != nil {
return nil, fmt.Errorf("marking role-arn flag as required: %w", err)
}
cmd.Flags().StringVar(&region, "region", "", "The AWS region to use. Optional.")
cmd.Flags().StringVar(&profileARN, "profile-arn", "", "The ARN of the Roles Anywhere profile to use. Required.")
cmd.MarkFlagRequired("profile-arn")
if err := cmd.MarkFlagRequired("profile-arn"); err != nil {
return nil, fmt.Errorf("marking profile-arn flag as required: %w", err)
}
cmd.Flags().DurationVar(&sessionDuration, "session-duration", 0, "The duration of the resulting session. Optional. Can range from 15m to 12h.")
cmd.Flags().StringVar(&trustAnchorARN, "trust-anchor-arn", "", "The ARN of the Roles Anywhere trust anchor to use. Required.")
cmd.MarkFlagRequired("trust-anchor-arn")
if err := cmd.MarkFlagRequired("trust-anchor-arn"); err != nil {
return nil, fmt.Errorf("marking trust-anchor-arn flag as required: %w", err)
}
cmd.Flags().StringVar(&roleSessionName, "role-session-name", "", "The identifier for the role session. Optional.")
return cmd
return cmd, nil
}

0 comments on commit a15c0da

Please sign in to comment.