| 24 | } |
| 25 | |
| 26 | func NewCmdAdd(f *cmdutil.Factory, runF func(*AddOptions) error) *cobra.Command { |
| 27 | opts := &AddOptions{ |
| 28 | HTTPClient: f.HttpClient, |
| 29 | IO: f.IOStreams, |
| 30 | } |
| 31 | |
| 32 | cmd := &cobra.Command{ |
| 33 | Use: "add <key-file>", |
| 34 | Short: "Add a deploy key to a GitHub repository", |
| 35 | Long: heredoc.Doc(` |
| 36 | Add a deploy key to a GitHub repository. |
| 37 | |
| 38 | Note that any key added by gh will be associated with the current authentication token. |
| 39 | If you de-authorize the GitHub CLI app or authentication token from your account, any |
| 40 | deploy keys added by GitHub CLI will be removed as well. |
| 41 | `), |
| 42 | Example: heredoc.Doc(` |
| 43 | # Generate a passwordless SSH key and add it as a deploy key to a repository |
| 44 | $ ssh-keygen -t ed25519 -C "my description" -N "" -f ~/.ssh/gh-test |
| 45 | $ gh repo deploy-key add ~/.ssh/gh-test.pub |
| 46 | `), |
| 47 | Args: cobra.ExactArgs(1), |
| 48 | RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | opts.BaseRepo = f.BaseRepo |
| 50 | opts.KeyFile = args[0] |
| 51 | |
| 52 | if runF != nil { |
| 53 | return runF(opts) |
| 54 | } |
| 55 | return addRun(opts) |
| 56 | }, |
| 57 | } |
| 58 | |
| 59 | cmd.Flags().StringVarP(&opts.Title, "title", "t", "", "Title of the new key") |
| 60 | cmd.Flags().BoolVarP(&opts.AllowWrite, "allow-write", "w", false, "Allow write access for the key") |
| 61 | return cmd |
| 62 | } |
| 63 | |
| 64 | func addRun(opts *AddOptions) error { |
| 65 | httpClient, err := opts.HTTPClient() |