| 29 | } |
| 30 | |
| 31 | func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command { |
| 32 | opts := &DeleteOptions{ |
| 33 | IO: f.IOStreams, |
| 34 | Config: f.Config, |
| 35 | HttpClient: f.HttpClient, |
| 36 | } |
| 37 | |
| 38 | cmd := &cobra.Command{ |
| 39 | Use: "delete <secret-name>", |
| 40 | Short: "Delete secrets", |
| 41 | Long: heredoc.Doc(` |
| 42 | Delete a secret on one of the following levels: |
| 43 | - repository (default): available to GitHub Actions runs, Agents sessions, or Dependabot in a repository |
| 44 | - environment: available to GitHub Actions runs for a deployment environment in a repository |
| 45 | - organization: available to GitHub Actions runs, Agents sessions, Dependabot, or Codespaces within an organization |
| 46 | - user: available to Codespaces for your user |
| 47 | `), |
| 48 | Args: cobra.ExactArgs(1), |
| 49 | RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | // If the user specified a repo directly, then we're using the OverrideBaseRepoFunc set by EnableRepoOverride |
| 51 | // So there's no reason to use the specialised BaseRepoFunc that requires remote disambiguation. |
| 52 | opts.BaseRepo = f.BaseRepo |
| 53 | isRepoUserProvided := cmd.Flags().Changed("repo") || os.Getenv("GH_REPO") != "" |
| 54 | if !isRepoUserProvided { |
| 55 | // If they haven't specified a repo directly, then we will wrap the BaseRepoFunc in one that errors if |
| 56 | // there might be multiple valid remotes. |
| 57 | opts.BaseRepo = shared.RequireNoAmbiguityBaseRepoFunc(opts.BaseRepo, f.Remotes) |
| 58 | // But if we are able to prompt, then we will wrap that up in a BaseRepoFunc that can prompt the user to |
| 59 | // resolve the ambiguity. |
| 60 | if opts.IO.CanPrompt() { |
| 61 | opts.BaseRepo = shared.PromptWhenAmbiguousBaseRepoFunc(opts.BaseRepo, f.IOStreams, f.Prompter) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if err := cmdutil.MutuallyExclusive("specify only one of `--org`, `--env`, or `--user`", opts.OrgName != "", opts.EnvName != "", opts.UserSecrets); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | opts.SecretName = args[0] |
| 70 | |
| 71 | if runF != nil { |
| 72 | return runF(opts) |
| 73 | } |
| 74 | |
| 75 | return removeRun(opts) |
| 76 | }, |
| 77 | Aliases: []string{ |
| 78 | "remove", |
| 79 | }, |
| 80 | } |
| 81 | cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Delete a secret for an organization") |
| 82 | cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Delete a secret for an environment") |
| 83 | cmd.Flags().BoolVarP(&opts.UserSecrets, "user", "u", false, "Delete a secret for your user") |
| 84 | cmdutil.StringEnumFlag(cmd, &opts.Application, "app", "a", "", []string{shared.Actions, shared.Agents, shared.Codespaces, shared.Dependabot}, "Delete a secret for a specific application") |
| 85 | |
| 86 | return cmd |
| 87 | } |
| 88 | |