Skip to content
Merged
26 changes: 25 additions & 1 deletion cmd/account_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -11,7 +12,10 @@ import (
// prefers an explicit --account-id flag, falls back to the configured
// profile via viper, and returns an error if neither is set.
func resolveAccountID(cmd *cobra.Command) (string, error) {
accountID, _ := cmd.Flags().GetString("account-id")
accountID, err := cmd.Flags().GetString("account-id")
if err != nil {
accountID = ""
}
if accountID == "" {
accountID = viper.GetString("account-id")
}
Comment thread
blue4209211 marked this conversation as resolved.
Expand All @@ -20,3 +24,23 @@ func resolveAccountID(cmd *cobra.Command) (string, error) {
}
return accountID, nil
}

// resolveAccountIDWithPositional returns the account-id prioritizing an explicit
// --account-id flag (if set), followed by a positional argument, and falling
// back to profile config via viper.
func resolveAccountIDWithPositional(cmd *cobra.Command, args []string) (string, error) {
if cmd.Flags().Changed("account-id") {
flagVal, err := cmd.Flags().GetString("account-id")
if err == nil && strings.TrimSpace(flagVal) != "" {
return strings.TrimSpace(flagVal), nil
}
}
Comment thread
blue4209211 marked this conversation as resolved.
if len(args) > 0 && strings.TrimSpace(args[0]) != "" {
return strings.TrimSpace(args[0]), nil
}
accountID, err := resolveAccountID(cmd)
if err != nil {
return "", fmt.Errorf("resolving account ID: %w", err)
}
return accountID, nil
}
Loading