Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add clobber flag to alias set #7787

Merged
merged 3 commits into from Aug 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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