Skip to content

Commit 8963d80

Browse files
committed
better get func
1 parent 6bd898e commit 8963d80

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

command/alias.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ func aliasSet(cmd *cobra.Command, args []string) error {
7373

7474
successMsg := fmt.Sprintf("%s Added alias.", utils.Green("✓"))
7575

76-
if aliasCfg.Exists(alias) {
76+
oldExpansion, ok := aliasCfg.Get(alias)
77+
if ok {
7778
successMsg = fmt.Sprintf("%s Changed alias %s from %s to %s",
7879
utils.Green("✓"),
7980
utils.Bold(alias),
80-
utils.Bold(aliasCfg.Get(alias)),
81+
utils.Bold(oldExpansion),
8182
utils.Bold(expansionStr),
8283
)
8384
}
@@ -188,11 +189,11 @@ func aliasDelete(cmd *cobra.Command, args []string) error {
188189
return fmt.Errorf("couldn't read aliases config: %w", err)
189190
}
190191

191-
if !aliasCfg.Exists(alias) {
192+
expansion, ok := aliasCfg.Get(alias)
193+
if !ok {
192194
return fmt.Errorf("no such alias %s", alias)
193-
}
194195

195-
expansion := aliasCfg.Get(alias)
196+
}
196197

197198
err = aliasCfg.Delete(alias)
198199
if err != nil {

command/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,9 @@ func ExpandAlias(args []string) ([]string, error) {
464464
return empty, err
465465
}
466466

467-
if aliases.Exists(args[1]) {
467+
expansion, ok := aliases.Get(args[1])
468+
if ok {
468469
extraArgs := []string{}
469-
expansion := aliases.Get(args[1])
470470
for i, a := range args[2:] {
471471
if !strings.Contains(expansion, "$") {
472472
extraArgs = append(extraArgs, a)

internal/config/alias_config.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,13 @@ type AliasConfig struct {
99
Parent Config
1010
}
1111

12-
func (a *AliasConfig) Exists(alias string) bool {
12+
func (a *AliasConfig) Get(alias string) (string, bool) {
1313
if a.Empty() {
14-
return false
14+
return "", false
1515
}
1616
value, _ := a.GetStringValue(alias)
1717

18-
return value != ""
19-
}
20-
21-
func (a *AliasConfig) Get(alias string) string {
22-
value, _ := a.GetStringValue(alias)
23-
24-
return value
18+
return value, value != ""
2519
}
2620

2721
func (a *AliasConfig) Add(alias, expansion string) error {

0 commit comments

Comments
 (0)