| 93 | } |
| 94 | |
| 95 | func setRun(opts *SetOptions) error { |
| 96 | cs := opts.IO.ColorScheme() |
| 97 | cfg, err := opts.Config() |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | aliasCfg := cfg.Aliases() |
| 103 | |
| 104 | expansion, err := getExpansion(opts) |
| 105 | if err != nil { |
| 106 | return fmt.Errorf("did not understand expansion: %w", err) |
| 107 | } |
| 108 | |
| 109 | if opts.IsShell && !strings.HasPrefix(expansion, "!") { |
| 110 | expansion = "!" + expansion |
| 111 | } |
| 112 | |
| 113 | isTerminal := opts.IO.IsStdoutTTY() |
| 114 | if isTerminal { |
| 115 | fmt.Fprintf(opts.IO.ErrOut, "- Creating alias for %s: %s\n", cs.Bold(opts.Name), cs.Bold(expansion)) |
| 116 | } |
| 117 | |
| 118 | var existingAlias bool |
| 119 | if _, err := aliasCfg.Get(opts.Name); err == nil { |
| 120 | existingAlias = true |
| 121 | } |
| 122 | |
| 123 | if !opts.validAliasName(opts.Name) { |
| 124 | if !existingAlias { |
| 125 | return fmt.Errorf("%s Could not create alias %s: already a gh command or extension", |
| 126 | cs.FailureIcon(), |
| 127 | cs.Bold(opts.Name)) |
| 128 | } |
| 129 | |
| 130 | if existingAlias && !opts.OverwriteExisting { |
| 131 | return fmt.Errorf("%s Could not create alias %s: name already taken, use the --clobber flag to overwrite it", |
| 132 | cs.FailureIcon(), |
| 133 | cs.Bold(opts.Name), |
| 134 | ) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if !opts.validAliasExpansion(expansion) { |
| 139 | return fmt.Errorf("%s Could not create alias %s: expansion does not correspond to a gh command, extension, or alias", |
| 140 | cs.FailureIcon(), |
| 141 | cs.Bold(opts.Name)) |
| 142 | } |
| 143 | |
| 144 | aliasCfg.Add(opts.Name, expansion) |
| 145 | |
| 146 | err = cfg.Write() |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | |
| 151 | successMsg := fmt.Sprintf("%s Added alias %s", cs.SuccessIcon(), cs.Bold(opts.Name)) |
| 152 | if existingAlias && opts.OverwriteExisting { |