nolint:funlen
( cfg storage, prompt func([]string, io.Reader, io.Writer) (string, error), )
| 17 | |
| 18 | //nolint:funlen |
| 19 | func delCommand( |
| 20 | cfg storage, |
| 21 | prompt func([]string, io.Reader, io.Writer) (string, error), |
| 22 | ) *cobra.Command { |
| 23 | return &cobra.Command{ |
| 24 | Use: "del [profile] [key]", |
| 25 | Aliases: []string{"rm"}, |
| 26 | Short: "Delete a profile or a profile entry", |
| 27 | Long: multiline( |
| 28 | `Delete an entry from a profile or an entire profile.`, |
| 29 | `Enter the "key" argument to remove the exact key from the profile.`, |
| 30 | ), |
| 31 | Example: multiline( |
| 32 | `git-profile del`, |
| 33 | `git-profile del my-profile (delete the entire profile)`, |
| 34 | `git-profile del my-profile user.name (deleting only a certain key)`, |
| 35 | ), |
| 36 | Args: func(_ *cobra.Command, args []string) error { |
| 37 | if len(args) <= 2 { //nolint:mnd |
| 38 | return nil |
| 39 | } |
| 40 | |
| 41 | return fmt.Errorf("accepts at most 2 arguments") |
| 42 | }, |
| 43 | Run: func(cmd *cobra.Command, args []string) { |
| 44 | filename, _ := cmd.Flags().GetString("config") |
| 45 | |
| 46 | switch len(args) { |
| 47 | case 0: |
| 48 | profile, err := prompt(cfg.Names(), cmd.InOrStdin(), cmd.OutOrStdout()) |
| 49 | if err != nil { |
| 50 | if ui.IsAborted(err) { |
| 51 | ui.PrintErrln(cmd, ui.ErrorStyle, "Interactive delete cancelled.") |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | ui.PrintErrln(cmd, ui.ErrorStyle, "%s", err) |
| 56 | os.Exit(1) |
| 57 | } |
| 58 | |
| 59 | err = profileDelete(cmd, cfg, filename, profile) |
| 60 | if err != nil { |
| 61 | ui.PrintErrln(cmd, ui.ErrorStyle, "Unable to save config file: %s", err) |
| 62 | os.Exit(1) |
| 63 | } |
| 64 | |
| 65 | return |
| 66 | case 1: |
| 67 | profile := args[0] |
| 68 | |
| 69 | err := profileDelete(cmd, cfg, filename, profile) |
| 70 | if err != nil { |
| 71 | ui.PrintErrln(cmd, ui.ErrorStyle, "Unable to save config file: %s", err) |
| 72 | os.Exit(1) |
| 73 | } |
| 74 | case 2: |
| 75 | profile := args[0] |
| 76 | if ok := cfg.Delete(profile, args[1]); !ok { |