| 24 | } |
| 25 | |
| 26 | func addCommand( |
| 27 | cfg storage, |
| 28 | promptProfileName func(io.Reader, io.Writer) (string, error), |
| 29 | editProfileFields func(ui.ProfileFormData, io.Reader, io.Writer) (ui.ProfileFormData, error), |
| 30 | ) *cobra.Command { |
| 31 | return &cobra.Command{ |
| 32 | Use: "add [profile] [key] [value]", |
| 33 | Aliases: []string{"set"}, |
| 34 | Short: "Add or update profile entries", |
| 35 | Long: "Add a key to a profile or update an existing profile.", |
| 36 | Example: multiline( |
| 37 | `git-profile add`, |
| 38 | `git-profile add my-profile user.email work@example.com`, |
| 39 | `git-profile add my-profile user.name "John Doe"`, |
| 40 | `git-profile add my-profile user.signingkey AAAAAAAA`, |
| 41 | ), |
| 42 | Args: func(_ *cobra.Command, args []string) error { |
| 43 | if len(args) == 0 || len(args) == 3 { |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | return fmt.Errorf("accepts either no arguments for interactive mode or exactly 3 arguments") |
| 48 | }, |
| 49 | Run: func(cmd *cobra.Command, args []string) { |
| 50 | filename, _ := cmd.Flags().GetString("config") |
| 51 | |
| 52 | switch len(args) { |
| 53 | case 3: |
| 54 | err := profileUpdateEntry(cmd, cfg, filename, args[0], args[1], args[2]) |
| 55 | if err != nil { |
| 56 | ui.PrintErrln(cmd, ui.ErrorStyle, "Unable to save config file: %s", err) |
| 57 | os.Exit(1) |
| 58 | } |
| 59 | |
| 60 | return |
| 61 | case 0: |
| 62 | err := profileUpdateEntries(cmd, cfg, filename, promptProfileName, editProfileFields) |
| 63 | if err != nil { |
| 64 | if ui.IsAborted(err) { |
| 65 | ui.PrintErrln(cmd, ui.ErrorStyle, "Interactive add cancelled.") |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | ui.PrintErrln(cmd, ui.ErrorStyle, "Unable to save config file: %s", err) |
| 70 | os.Exit(1) |
| 71 | } |
| 72 | } |
| 73 | }, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func profileUpdateEntry( |
| 78 | cmd *cobra.Command, |