| 27 | } |
| 28 | |
| 29 | func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command { |
| 30 | opts := &SetOptions{ |
| 31 | IO: f.IOStreams, |
| 32 | Config: f.Config, |
| 33 | } |
| 34 | |
| 35 | cmd := &cobra.Command{ |
| 36 | Use: "set <alias> <expansion>", |
| 37 | Short: "Create a shortcut for a gh command", |
| 38 | Long: heredoc.Docf(` |
| 39 | Define a word that will expand to a full gh command when invoked. |
| 40 | |
| 41 | The expansion may specify additional arguments and flags. If the expansion includes |
| 42 | positional placeholders such as %[1]s$1%[1]s, extra arguments that follow the alias will be |
| 43 | inserted appropriately. Otherwise, extra arguments will be appended to the expanded |
| 44 | command. |
| 45 | |
| 46 | Use %[1]s-%[1]s as expansion argument to read the expansion string from standard input. This |
| 47 | is useful to avoid quoting issues when defining expansions. |
| 48 | |
| 49 | If the expansion starts with %[1]s!%[1]s or if %[1]s--shell%[1]s was given, the expansion is a shell |
| 50 | expression that will be evaluated through the %[1]ssh%[1]s interpreter when the alias is |
| 51 | invoked. This allows for chaining multiple commands via piping and redirection. |
| 52 | `, "`"), |
| 53 | Example: heredoc.Doc(` |
| 54 | # Note: Command Prompt on Windows requires using double quotes for arguments |
| 55 | $ gh alias set pv 'pr view' |
| 56 | $ gh pv -w 123 #=> gh pr view -w 123 |
| 57 | |
| 58 | $ gh alias set bugs 'issue list --label=bugs' |
| 59 | $ gh bugs |
| 60 | |
| 61 | $ gh alias set homework 'issue list --assignee @me' |
| 62 | $ gh homework |
| 63 | |
| 64 | $ gh alias set 'issue mine' 'issue list --mention @me' |
| 65 | $ gh issue mine |
| 66 | |
| 67 | $ gh alias set epicsBy 'issue list --author="$1" --label="epic"' |
| 68 | $ gh epicsBy vilmibm #=> gh issue list --author="vilmibm" --label="epic" |
| 69 | |
| 70 | $ gh alias set --shell igrep 'gh issue list --label="$1" | grep "$2"' |
| 71 | $ gh igrep epic foo #=> gh issue list --label="epic" | grep "foo" |
| 72 | `), |
| 73 | Args: cobra.ExactArgs(2), |
| 74 | RunE: func(cmd *cobra.Command, args []string) error { |
| 75 | opts.Name = args[0] |
| 76 | opts.Expansion = args[1] |
| 77 | |
| 78 | opts.validAliasName = shared.ValidAliasNameFunc(cmd) |
| 79 | opts.validAliasExpansion = shared.ValidAliasExpansionFunc(cmd) |
| 80 | |
| 81 | if runF != nil { |
| 82 | return runF(opts) |
| 83 | } |
| 84 | |
| 85 | return setRun(opts) |
| 86 | }, |