|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/AlecAivazis/survey/v2" |
| 7 | + "github.com/ccxdev/opssh/internal/config" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var addCmd = &cobra.Command{ |
| 12 | + Use: "add <name>", |
| 13 | + Short: "Add a new SSH key profile", |
| 14 | + Long: `Add a new SSH key profile to your config file. |
| 15 | +You'll be prompted to enter the account, vault, and item details.`, |
| 16 | + Args: cobra.ExactArgs(1), |
| 17 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 18 | + profileName := args[0] |
| 19 | + |
| 20 | + cfg, err := config.LoadConfig() |
| 21 | + if err != nil { |
| 22 | + return fmt.Errorf("failed to load config: %w", err) |
| 23 | + } |
| 24 | + |
| 25 | + if _, err := config.GetProfile(cfg, profileName); err == nil { |
| 26 | + return fmt.Errorf("profile '%s' already exists", profileName) |
| 27 | + } |
| 28 | + |
| 29 | + var profile *config.Profile |
| 30 | + |
| 31 | + profile, err = promptManualProfile() |
| 32 | + |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + if err := config.AddProfile(cfg, profileName, *profile); err != nil { |
| 38 | + return fmt.Errorf("failed to add profile: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + if err := config.SaveConfig(cfg); err != nil { |
| 42 | + return fmt.Errorf("failed to save config: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + fmt.Printf("✓ Added profile '%s'\n", profileName) |
| 46 | + fmt.Printf(" Account: %s\n", profile.Account) |
| 47 | + fmt.Printf(" Vault: %s\n", profile.Vault) |
| 48 | + fmt.Printf(" Item: %s\n", profile.Item) |
| 49 | + |
| 50 | + var switchNow bool |
| 51 | + switchPrompt := &survey.Confirm{ |
| 52 | + Message: "Switch to this profile now?", |
| 53 | + Default: false, |
| 54 | + } |
| 55 | + if err := survey.AskOne(switchPrompt, &switchNow); err == nil && switchNow { |
| 56 | + fmt.Println("Use 'opssh switch " + profileName + "' to switch to this profile.") |
| 57 | + } |
| 58 | + |
| 59 | + return nil |
| 60 | + }, |
| 61 | +} |
| 62 | + |
| 63 | +func promptManualProfile() (*config.Profile, error) { |
| 64 | + var account, vault, item string |
| 65 | + |
| 66 | + accountPrompt := &survey.Input{ |
| 67 | + Message: "Account:", |
| 68 | + } |
| 69 | + if err := survey.AskOne(accountPrompt, &account); err != nil { |
| 70 | + return nil, fmt.Errorf("failed to get account: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + vaultPrompt := &survey.Input{ |
| 74 | + Message: "Vault:", |
| 75 | + } |
| 76 | + if err := survey.AskOne(vaultPrompt, &vault); err != nil { |
| 77 | + return nil, fmt.Errorf("failed to get vault: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + itemPrompt := &survey.Input{ |
| 81 | + Message: "Item:", |
| 82 | + } |
| 83 | + if err := survey.AskOne(itemPrompt, &item); err != nil { |
| 84 | + return nil, fmt.Errorf("failed to get item: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return &config.Profile{ |
| 88 | + Account: account, |
| 89 | + Vault: vault, |
| 90 | + Item: item, |
| 91 | + }, nil |
| 92 | +} |
| 93 | + |
| 94 | +func init() { |
| 95 | + rootCmd.AddCommand(addCmd) |
| 96 | +} |
0 commit comments