Use - to read from stdin instead

Resolves PR feedback.
This commit is contained in:
Heath Stewart 2021-04-23 17:23:27 -07:00
parent bd2738379b
commit aaa5a9e949
2 changed files with 13 additions and 13 deletions

View file

@ -38,7 +38,8 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
The expansion may specify additional arguments and flags. If the expansion
includes positional placeholders such as '$1', '$2', etc., any extra arguments
that follow the invocation of an alias will be inserted appropriately.
Reads from STDIN if not specified.
Reads from STDIN if '-' is specified as the expansion parameter. This can be useful
for commands with mixed quotes or multiple lines.
If '--shell' is specified, the alias will be run through a shell interpreter (sh). This allows you
to compose commands with "|" or redirect with ">". Note that extra arguments following the alias
@ -48,7 +49,8 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
Platform note: on Windows, shell aliases are executed via "sh" as installed by Git For Windows. If
you have installed git on Windows in some other way, shell aliases may not work for you.
Quotes must always be used when defining a command as in the examples.
Quotes must always be used when defining a command as in the examples unless you pass '-'
as the expansion parameter and pipe your command to 'gh alias set'.
`),
Example: heredoc.Doc(`
$ gh alias set pv 'pr view'
@ -70,19 +72,16 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
#=> gh issue list --label="epic" | grep "foo"
# users.txt contains multiline 'api graphql -F name="$1" ...' with mixed quotes
$ gh alias set users < users.txt
$ gh alias set users - < users.txt
$ gh users octocat
#=> gh api graphql -F name="octocat" ...
`),
Args: cobra.RangeArgs(1, 2),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
opts.RootCmd = cmd.Root()
opts.Name = args[0]
if len(args) > 1 {
opts.Expansion = args[1]
}
opts.Expansion = args[1]
if runF != nil {
return runF(opts)
@ -165,7 +164,7 @@ func validCommand(rootCmd *cobra.Command, expansion string) bool {
}
func getExpansion(opts *SetOptions) (string, error) {
if opts.Expansion == "" {
if opts.Expansion == "-" {
stdin, err := ioutil.ReadAll(opts.IO.In)
if err != nil {
return "", fmt.Errorf("failed to read from STDIN: %w", err)

View file

@ -267,7 +267,7 @@ func TestShellAlias_from_stdin(t *testing.T) {
cfg := config.NewFromString(``)
output, err := runCommand(cfg, true, "users", `api graphql -F name="$1" -f query='
output, err := runCommand(cfg, true, "users -", `api graphql -F name="$1" -f query='
query ($name: String!) {
user(login: $name) {
name
@ -311,9 +311,10 @@ func TestShellAlias_getExpansion(t *testing.T) {
stdin: "api graphql -F name=\"$1\"",
},
{
name: "stdin",
want: "api graphql -F name=\"$1\"",
stdin: "api graphql -F name=\"$1\"",
name: "stdin",
expansionArg: "-",
want: "api graphql -F name=\"$1\"",
stdin: "api graphql -F name=\"$1\"",
},
}