Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions command/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"sort"
"strings"

"github.com/cli/cli/utils"
Expand All @@ -12,6 +13,7 @@ import (
func init() {
RootCmd.AddCommand(aliasCmd)
aliasCmd.AddCommand(aliasSetCmd)
aliasCmd.AddCommand(aliasListCmd)
}

var aliasCmd = &cobra.Command{
Expand Down Expand Up @@ -112,3 +114,55 @@ 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.`,
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch with sorting keys!


for _, alias := range keys {
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()
}

return tp.Render()
}
39 changes: 39 additions & 0 deletions command/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,42 @@ 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 := `---
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)
}

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)
}
16 changes: 16 additions & 0 deletions internal/config/alias_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,19 @@ 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)-1; i += 2 {
key := a.Root.Content[i].Value
value := a.Root.Content[i+1].Value
out[key] = value
}

return out
}