Skip to content

Commit

Permalink
Add clobber flag to alias set (#7787)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 15, 2023
1 parent d9ea221 commit c5eb188
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 298 deletions.
53 changes: 35 additions & 18 deletions pkg/cmd/alias/set/set.go
Expand Up @@ -17,9 +17,10 @@ type SetOptions struct {
Config func() (config.Config, error)
IO *iostreams.IOStreams

Name string
Expansion string
IsShell bool
Name string
Expansion string
IsShell bool
OverwriteExisting bool

validAliasName func(string) bool
validAliasExpansion func(string) bool
Expand Down Expand Up @@ -86,6 +87,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
}

cmd.Flags().BoolVarP(&opts.IsShell, "shell", "s", false, "Declare an alias to be passed through a shell interpreter")
cmd.Flags().BoolVar(&opts.OverwriteExisting, "clobber", false, "Overwrite existing aliases of the same name")

return cmd
}
Expand All @@ -104,31 +106,39 @@ func setRun(opts *SetOptions) error {
return fmt.Errorf("did not understand expansion: %w", err)
}

if opts.IsShell && !strings.HasPrefix(expansion, "!") {
expansion = "!" + expansion
}

isTerminal := opts.IO.IsStdoutTTY()
if isTerminal {
fmt.Fprintf(opts.IO.ErrOut, "- Adding alias for %s: %s\n", cs.Bold(opts.Name), cs.Bold(expansion))
fmt.Fprintf(opts.IO.ErrOut, "- Creating alias for %s: %s\n", cs.Bold(opts.Name), cs.Bold(expansion))
}

if opts.IsShell && !strings.HasPrefix(expansion, "!") {
expansion = "!" + expansion
var existingAlias bool
if _, err := aliasCfg.Get(opts.Name); err == nil {
existingAlias = true
}

if !opts.validAliasName(opts.Name) {
return fmt.Errorf("could not create alias: %q is already a gh command, extension, or alias", opts.Name)
}
if !existingAlias {
return fmt.Errorf("%s Could not create alias %s: already a gh command or extension",
cs.FailureIcon(),
cs.Bold(opts.Name))
}

if !opts.validAliasExpansion(expansion) {
return fmt.Errorf("could not create alias: %s does not correspond to a gh command, extension, or alias", expansion)
if existingAlias && !opts.OverwriteExisting {
return fmt.Errorf("%s Could not create alias %s: name already taken, use the --clobber flag to overwrite it",
cs.FailureIcon(),
cs.Bold(opts.Name),
)
}
}

successMsg := fmt.Sprintf("%s Added alias.", cs.SuccessIcon())
if oldExpansion, err := aliasCfg.Get(opts.Name); err == nil {
successMsg = fmt.Sprintf("%s Changed alias %s from %s to %s",
cs.SuccessIcon(),
cs.Bold(opts.Name),
cs.Bold(oldExpansion),
cs.Bold(expansion),
)
if !opts.validAliasExpansion(expansion) {
return fmt.Errorf("%s Could not create alias %s: expansion does not correspond to a gh command, extension, or alias",
cs.FailureIcon(),
cs.Bold(opts.Name))
}

aliasCfg.Add(opts.Name, expansion)
Expand All @@ -138,6 +148,13 @@ func setRun(opts *SetOptions) error {
return err
}

successMsg := fmt.Sprintf("%s Added alias %s", cs.SuccessIcon(), cs.Bold(opts.Name))
if existingAlias && opts.OverwriteExisting {
successMsg = fmt.Sprintf("%s Changed alias %s",
cs.WarningIcon(),
cs.Bold(opts.Name))
}

if isTerminal {
fmt.Fprintln(opts.IO.ErrOut, successMsg)
}
Expand Down

0 comments on commit c5eb188

Please sign in to comment.