From 24e97a460ffb2f8c39ff50ce54f52ee3ceaa4050 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 27 May 2020 16:28:23 -0500 Subject: [PATCH] prevent overriding gh commands --- command/alias.go | 6 +++++- command/alias_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/command/alias.go b/command/alias.go index 12cfe4ace..5c7ef405a 100644 --- a/command/alias.go +++ b/command/alias.go @@ -11,7 +11,7 @@ import ( // TODO // - [ ] DEBUG support -// - [ ] prevent overriding existing gh command +// - [x] prevent overriding existing gh command // - [x] allow overwriting alias // - [x] forward extra arguments // - [x] allow duplication of placeholder @@ -68,6 +68,10 @@ func aliasSet(cmd *cobra.Command, args []string) error { out := colorableOut(cmd) fmt.Fprintf(out, "- Adding alias for %s: %s\n", utils.Bold(alias), utils.Bold(expansion)) + if validCommand(alias) { + return fmt.Errorf("could not create alias: %q is already a gh command", alias) + } + if !validCommand(expansion) { return fmt.Errorf("could not create alias: %s does not correspond to a gh command", utils.Bold(expansion)) } diff --git a/command/alias_test.go b/command/alias_test.go index f222e185b..62af996ca 100644 --- a/command/alias_test.go +++ b/command/alias_test.go @@ -9,6 +9,17 @@ import ( "github.com/cli/cli/test" ) +func TestAliasSet_gh_command(t *testing.T) { + initBlankContext("", "OWNER/REPO", "trunk") + + _, err := RunCommand("alias set pr pr status") + if err == nil { + t.Fatal("expected error") + } + + eq(t, err.Error(), `could not create alias: "pr" is already a gh command`) +} + func TestAliasSet_existing_alias(t *testing.T) { cfg := `--- hosts: