Fix alias import clobber flag (#7569)
This commit is contained in:
parent
9be9dc22e9
commit
78839dbe0b
2 changed files with 55 additions and 38 deletions
|
|
@ -122,15 +122,31 @@ func importRun(opts *ImportOptions) error {
|
|||
var msg strings.Builder
|
||||
|
||||
for _, alias := range getSortedKeys(aliasMap) {
|
||||
if !opts.validAliasName(alias) {
|
||||
msg.WriteString(
|
||||
fmt.Sprintf("%s Could not import alias %s: already a gh command, extension, or alias\n",
|
||||
cs.FailureIcon(),
|
||||
cs.Bold(alias),
|
||||
),
|
||||
)
|
||||
var existingAlias bool
|
||||
if _, err := aliasCfg.Get(alias); err == nil {
|
||||
existingAlias = true
|
||||
}
|
||||
|
||||
continue
|
||||
if !opts.validAliasName(alias) {
|
||||
if !existingAlias {
|
||||
msg.WriteString(
|
||||
fmt.Sprintf("%s Could not import alias %s: already a gh command or extension\n",
|
||||
cs.FailureIcon(),
|
||||
cs.Bold(alias),
|
||||
),
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if existingAlias && !opts.OverwriteExisting {
|
||||
msg.WriteString(
|
||||
fmt.Sprintf("%s Could not import alias %s: name already taken\n",
|
||||
cs.FailureIcon(),
|
||||
cs.Bold(alias),
|
||||
),
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
expansion := aliasMap[alias]
|
||||
|
|
@ -142,31 +158,19 @@ func importRun(opts *ImportOptions) error {
|
|||
cs.Bold(alias),
|
||||
),
|
||||
)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := aliasCfg.Get(alias); err == nil {
|
||||
if opts.OverwriteExisting {
|
||||
aliasCfg.Add(alias, expansion)
|
||||
aliasCfg.Add(alias, expansion)
|
||||
|
||||
msg.WriteString(
|
||||
fmt.Sprintf("%s Changed alias %s\n",
|
||||
cs.WarningIcon(),
|
||||
cs.Bold(alias),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
msg.WriteString(
|
||||
fmt.Sprintf("%s Could not import alias %s: name already taken\n",
|
||||
cs.FailureIcon(),
|
||||
cs.Bold(alias),
|
||||
),
|
||||
)
|
||||
}
|
||||
if existingAlias && opts.OverwriteExisting {
|
||||
msg.WriteString(
|
||||
fmt.Sprintf("%s Changed alias %s\n",
|
||||
cs.WarningIcon(),
|
||||
cs.Bold(alias),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
aliasCfg.Add(alias, expansion)
|
||||
|
||||
msg.WriteString(
|
||||
fmt.Sprintf("%s Added alias %s\n",
|
||||
cs.SuccessIcon(),
|
||||
|
|
|
|||
|
|
@ -112,13 +112,14 @@ func TestImportRun(t *testing.T) {
|
|||
importStdinMsg := "- Importing aliases from standard input"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
opts *ImportOptions
|
||||
stdin string
|
||||
fileContents string
|
||||
initConfig string
|
||||
wantConfig string
|
||||
wantStderr string
|
||||
name string
|
||||
opts *ImportOptions
|
||||
stdin string
|
||||
fileContents string
|
||||
initConfig string
|
||||
aliasCommands []*cobra.Command
|
||||
wantConfig string
|
||||
wantStderr string
|
||||
}{
|
||||
{
|
||||
name: "with no existing aliases",
|
||||
|
|
@ -158,6 +159,9 @@ func TestImportRun(t *testing.T) {
|
|||
igrep: '!gh issue list --label="$1" | grep "$2"'
|
||||
editor: vim
|
||||
`),
|
||||
aliasCommands: []*cobra.Command{
|
||||
{Use: "igrep"},
|
||||
},
|
||||
wantConfig: heredoc.Doc(`
|
||||
aliases:
|
||||
igrep: '!gh issue list --label="$1" | grep "$2"'
|
||||
|
|
@ -211,6 +215,9 @@ func TestImportRun(t *testing.T) {
|
|||
co: pr checkout
|
||||
editor: vim
|
||||
`),
|
||||
aliasCommands: []*cobra.Command{
|
||||
{Use: "co"},
|
||||
},
|
||||
wantConfig: heredoc.Doc(`
|
||||
aliases:
|
||||
co: pr checkout
|
||||
|
|
@ -234,6 +241,9 @@ func TestImportRun(t *testing.T) {
|
|||
co: pr checkout
|
||||
editor: vim
|
||||
`),
|
||||
aliasCommands: []*cobra.Command{
|
||||
{Use: "co"},
|
||||
},
|
||||
wantConfig: heredoc.Doc(`
|
||||
aliases:
|
||||
co: pr checkout -R cool/repo
|
||||
|
|
@ -256,9 +266,9 @@ func TestImportRun(t *testing.T) {
|
|||
wantStderr: strings.Join(
|
||||
[]string{
|
||||
importFileMsg,
|
||||
"X Could not import alias api: already a gh command, extension, or alias",
|
||||
"X Could not import alias issue: already a gh command, extension, or alias",
|
||||
"X Could not import alias pr: already a gh command, extension, or alias\n\n",
|
||||
"X Could not import alias api: already a gh command or extension",
|
||||
"X Could not import alias issue: already a gh command or extension",
|
||||
"X Could not import alias pr: already a gh command or extension\n\n",
|
||||
},
|
||||
"\n",
|
||||
),
|
||||
|
|
@ -315,6 +325,9 @@ func TestImportRun(t *testing.T) {
|
|||
apiCmd := &cobra.Command{Use: "api"}
|
||||
apiCmd.AddCommand(&cobra.Command{Use: "graphql"})
|
||||
rootCmd.AddCommand(apiCmd)
|
||||
for _, cmd := range tt.aliasCommands {
|
||||
rootCmd.AddCommand(cmd)
|
||||
}
|
||||
|
||||
tt.opts.validAliasName = shared.ValidAliasNameFunc(rootCmd)
|
||||
tt.opts.validAliasExpansion = shared.ValidAliasExpansionFunc(rootCmd)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue