NewRemoveCmd returns a new instance of RemoveCmd
(f *cmdutil.Factory, runF func(*RemoveOptions) error)
| 26 | |
| 27 | // NewRemoveCmd returns a new instance of RemoveCmd |
| 28 | func NewRemoveCmd(f *cmdutil.Factory, runF func(*RemoveOptions) error) *cobra.Command { |
| 29 | opts := &RemoveOptions{ |
| 30 | IO: f.IOStreams, |
| 31 | config: f.Config, |
| 32 | } |
| 33 | |
| 34 | var confirm bool |
| 35 | |
| 36 | cmd := &cobra.Command{ |
| 37 | Use: "remove <profile>", |
| 38 | Args: validators.ExactArgs(1), |
| 39 | ValidArgsFunction: cmdutil.ConfiguredProfilesCompletionFunc(f), |
| 40 | Short: "Remove the specified profile", |
| 41 | Long: `Remove the specified profile from the configuration.`, |
| 42 | Example: heredoc.Doc(` |
| 43 | # Remove the profile named "my-app" from the configuration |
| 44 | $ algolia profile remove my-app |
| 45 | `), |
| 46 | RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | opts.Profile = args[0] |
| 48 | |
| 49 | if opts.config.Default() != nil { |
| 50 | opts.DefaultProfile = opts.config.Default().Name |
| 51 | } else { |
| 52 | opts.DefaultProfile = "" |
| 53 | } |
| 54 | |
| 55 | if !confirm && opts.Profile == opts.DefaultProfile { |
| 56 | if !opts.IO.CanPrompt() { |
| 57 | return cmdutil.FlagErrorf( |
| 58 | "--confirm required when non-interactive shell is detected", |
| 59 | ) |
| 60 | } |
| 61 | opts.DoConfirm = true |
| 62 | } |
| 63 | |
| 64 | if !opts.config.ProfileExists(opts.Profile) { |
| 65 | return fmt.Errorf("the specified profile does not exist: '%s'", opts.Profile) |
| 66 | } |
| 67 | |
| 68 | if runF != nil { |
| 69 | return runF(opts) |
| 70 | } |
| 71 | |
| 72 | return runRemoveCmd(opts) |
| 73 | }, |
| 74 | } |
| 75 | |
| 76 | cmd.Flags(). |
| 77 | BoolVarP(&confirm, "confirm", "y", false, "Skip the remove profile confirmation prompt") |
| 78 | |
| 79 | return cmd |
| 80 | } |
| 81 | |
| 82 | // runRemoveCmd executes the remove command |
| 83 | func runRemoveCmd(opts *RemoveOptions) error { |