Skip to content

Commit aaa5a9e

Browse files
committed
Use - to read from stdin instead
Resolves PR feedback.
1 parent bd27383 commit aaa5a9e

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

pkg/cmd/alias/set/set.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
3838
The expansion may specify additional arguments and flags. If the expansion
3939
includes positional placeholders such as '$1', '$2', etc., any extra arguments
4040
that follow the invocation of an alias will be inserted appropriately.
41-
Reads from STDIN if not specified.
41+
Reads from STDIN if '-' is specified as the expansion parameter. This can be useful
42+
for commands with mixed quotes or multiple lines.
4243
4344
If '--shell' is specified, the alias will be run through a shell interpreter (sh). This allows you
4445
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
4849
Platform note: on Windows, shell aliases are executed via "sh" as installed by Git For Windows. If
4950
you have installed git on Windows in some other way, shell aliases may not work for you.
5051
51-
Quotes must always be used when defining a command as in the examples.
52+
Quotes must always be used when defining a command as in the examples unless you pass '-'
53+
as the expansion parameter and pipe your command to 'gh alias set'.
5254
`),
5355
Example: heredoc.Doc(`
5456
$ gh alias set pv 'pr view'
@@ -70,19 +72,16 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
7072
#=> gh issue list --label="epic" | grep "foo"
7173
7274
# users.txt contains multiline 'api graphql -F name="$1" ...' with mixed quotes
73-
$ gh alias set users < users.txt
75+
$ gh alias set users - < users.txt
7476
$ gh users octocat
7577
#=> gh api graphql -F name="octocat" ...
7678
`),
77-
Args: cobra.RangeArgs(1, 2),
79+
Args: cobra.ExactArgs(2),
7880
RunE: func(cmd *cobra.Command, args []string) error {
7981
opts.RootCmd = cmd.Root()
8082

8183
opts.Name = args[0]
82-
83-
if len(args) > 1 {
84-
opts.Expansion = args[1]
85-
}
84+
opts.Expansion = args[1]
8685

8786
if runF != nil {
8887
return runF(opts)
@@ -165,7 +164,7 @@ func validCommand(rootCmd *cobra.Command, expansion string) bool {
165164
}
166165

167166
func getExpansion(opts *SetOptions) (string, error) {
168-
if opts.Expansion == "" {
167+
if opts.Expansion == "-" {
169168
stdin, err := ioutil.ReadAll(opts.IO.In)
170169
if err != nil {
171170
return "", fmt.Errorf("failed to read from STDIN: %w", err)

pkg/cmd/alias/set/set_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func TestShellAlias_from_stdin(t *testing.T) {
267267

268268
cfg := config.NewFromString(``)
269269

270-
output, err := runCommand(cfg, true, "users", `api graphql -F name="$1" -f query='
270+
output, err := runCommand(cfg, true, "users -", `api graphql -F name="$1" -f query='
271271
query ($name: String!) {
272272
user(login: $name) {
273273
name
@@ -311,9 +311,10 @@ func TestShellAlias_getExpansion(t *testing.T) {
311311
stdin: "api graphql -F name=\"$1\"",
312312
},
313313
{
314-
name: "stdin",
315-
want: "api graphql -F name=\"$1\"",
316-
stdin: "api graphql -F name=\"$1\"",
314+
name: "stdin",
315+
expansionArg: "-",
316+
want: "api graphql -F name=\"$1\"",
317+
stdin: "api graphql -F name=\"$1\"",
317318
},
318319
}
319320

0 commit comments

Comments
 (0)