From a05daf2145e9b1c9b79a911d0c94913d49467ad1 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 22 May 2020 17:10:17 -0500 Subject: [PATCH 1/4] gh alias list --- command/alias.go | 52 +++++++++++++++++++++++++++++++++ command/alias_test.go | 41 ++++++++++++++++++++++++++ internal/config/alias_config.go | 19 ++++++++++++ 3 files changed, 112 insertions(+) diff --git a/command/alias.go b/command/alias.go index 839c8047ed3..f7c7c08352b 100644 --- a/command/alias.go +++ b/command/alias.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "sort" "strings" "github.com/cli/cli/utils" @@ -12,6 +13,7 @@ import ( func init() { RootCmd.AddCommand(aliasCmd) aliasCmd.AddCommand(aliasSetCmd) + aliasCmd.AddCommand(aliasListCmd) } var aliasCmd = &cobra.Command{ @@ -112,3 +114,53 @@ func processArgs(args []string) []string { return newArgs } + +var aliasListCmd = &cobra.Command{ + Use: "list", + Short: "List your aliases", + Long: `This command prints out all of the aliases gh is configured to use.`, + Example: `$ gh alias list +co: pr checkout +bugs: issue list --label="bugs"`, + Args: cobra.ExactArgs(0), + RunE: aliasList, +} + +func aliasList(cmd *cobra.Command, args []string) error { + ctx := contextForCommand(cmd) + cfg, err := ctx.Config() + if err != nil { + return fmt.Errorf("couldn't read config: %w", err) + } + + aliasCfg, err := cfg.Aliases() + if err != nil { + return fmt.Errorf("couldn't read aliases config: %w", err) + } + + stderr := colorableErr(cmd) + + if aliasCfg.Empty() { + fmt.Fprintf(stderr, "no aliases configured\n") + return nil + } + + stdout := colorableOut(cmd) + + tp := utils.NewTablePrinter(stdout) + + aliasMap := aliasCfg.All() + keys := []string{} + for alias := range aliasMap { + keys = append(keys, alias) + } + sort.Strings(keys) + + for _, alias := range keys { + tp.AddField(alias+":", nil, nil) + tp.AddField(aliasMap[alias], nil, nil) + tp.EndRow() + } + + return tp.Render() +} diff --git a/command/alias_test.go b/command/alias_test.go index 666d7458988..ff0452a86a9 100644 --- a/command/alias_test.go +++ b/command/alias_test.go @@ -241,3 +241,44 @@ func TestAliasSet_invalid_command(t *testing.T) { eq(t, err.Error(), "could not create alias: pe checkout does not correspond to a gh command") } + +func TestAliasList_empty(t *testing.T) { + initBlankContext("", "OWNER/REPO", "trunk") + + output, err := RunCommand("alias list") + + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + eq(t, output.String(), "") +} + +func TestAliasList(t *testing.T) { + cfg := `--- +hosts: + github.com: + user: OWNER + oauth_token: token123 +aliases: + co: pr checkout + il: issue list --author=$1 --label=$2 + clone: repo clone + prs: pr status + cs: config set editor 'quoted path' +` + initBlankContext(cfg, "OWNER/REPO", "trunk") + + output, err := RunCommand("alias list") + + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + test.ExpectLines(t, output.String(), + "clone:\trepo clone", + "co:\tpr checkout", + "il:\tissue list --author=\\$1 --label=\\$2", + "prs:\tpr status", + "cs:\tconfig set editor 'quoted path'") +} diff --git a/internal/config/alias_config.go b/internal/config/alias_config.go index 434b74f207e..6e1f03589bf 100644 --- a/internal/config/alias_config.go +++ b/internal/config/alias_config.go @@ -42,3 +42,22 @@ func (a *AliasConfig) Delete(alias string) error { // TODO when we get to gh alias delete return nil } + +func (a *AliasConfig) All() map[string]string { + out := map[string]string{} + + if a.Empty() { + return out + } + + for i := 0; i < len(a.Root.Content); i += 2 { + if i+1 == len(a.Root.Content) { + break + } + key := a.Root.Content[i].Value + value := a.Root.Content[i+1].Value + out[key] = value + } + + return out +} From 8ca53dd3bad35f5e4793dc53bb0f0cc7db689168 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 5 Jun 2020 12:06:05 -0500 Subject: [PATCH 2/4] no example for no-arg command --- command/alias.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/command/alias.go b/command/alias.go index f7c7c08352b..ea796aa5d58 100644 --- a/command/alias.go +++ b/command/alias.go @@ -119,11 +119,8 @@ var aliasListCmd = &cobra.Command{ Use: "list", Short: "List your aliases", Long: `This command prints out all of the aliases gh is configured to use.`, - Example: `$ gh alias list -co: pr checkout -bugs: issue list --label="bugs"`, - Args: cobra.ExactArgs(0), - RunE: aliasList, + Args: cobra.ExactArgs(0), + RunE: aliasList, } func aliasList(cmd *cobra.Command, args []string) error { From 953ea7cf7b9f1a77b50b2f7203a4d80be8084021 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 5 Jun 2020 12:06:18 -0500 Subject: [PATCH 3/4] do not use : when not printing to tty --- command/alias.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/command/alias.go b/command/alias.go index ea796aa5d58..26272d3b0b5 100644 --- a/command/alias.go +++ b/command/alias.go @@ -154,7 +154,12 @@ func aliasList(cmd *cobra.Command, args []string) error { sort.Strings(keys) for _, alias := range keys { - tp.AddField(alias+":", nil, nil) + if tp.IsTTY() { + // ensure that screen readers pause + tp.AddField(alias+":", nil, nil) + } else { + tp.AddField(alias, nil, nil) + } tp.AddField(aliasMap[alias], nil, nil) tp.EndRow() } From 106a0957e16d6a4c9f6ef84db9e7018acd503dbe Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 5 Jun 2020 12:06:34 -0500 Subject: [PATCH 4/4] review feedback --- command/alias_test.go | 18 ++++++++---------- internal/config/alias_config.go | 5 +---- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/command/alias_test.go b/command/alias_test.go index ff0452a86a9..e8c5f887731 100644 --- a/command/alias_test.go +++ b/command/alias_test.go @@ -256,10 +256,6 @@ func TestAliasList_empty(t *testing.T) { func TestAliasList(t *testing.T) { cfg := `--- -hosts: - github.com: - user: OWNER - oauth_token: token123 aliases: co: pr checkout il: issue list --author=$1 --label=$2 @@ -275,10 +271,12 @@ aliases: t.Fatalf("unexpected error: %s", err) } - test.ExpectLines(t, output.String(), - "clone:\trepo clone", - "co:\tpr checkout", - "il:\tissue list --author=\\$1 --label=\\$2", - "prs:\tpr status", - "cs:\tconfig set editor 'quoted path'") + expected := `clone repo clone +co pr checkout +cs config set editor 'quoted path' +il issue list --author=$1 --label=$2 +prs pr status +` + + eq(t, output.String(), expected) } diff --git a/internal/config/alias_config.go b/internal/config/alias_config.go index 6e1f03589bf..979ab3b9af9 100644 --- a/internal/config/alias_config.go +++ b/internal/config/alias_config.go @@ -50,10 +50,7 @@ func (a *AliasConfig) All() map[string]string { return out } - for i := 0; i < len(a.Root.Content); i += 2 { - if i+1 == len(a.Root.Content) { - break - } + for i := 0; i < len(a.Root.Content)-1; i += 2 { key := a.Root.Content[i].Value value := a.Root.Content[i+1].Value out[key] = value