-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor config management for testability
- Loading branch information
Showing
8 changed files
with
115 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cli | ||
|
||
import ( | ||
"slices" | ||
"strings" | ||
|
||
"github.com/moyiz/na/internal/config" | ||
) | ||
|
||
// Given a list of alias name parts, return a list of valid next parts. | ||
// Example: | ||
// | ||
// my: | ||
// aliases: | ||
// one: cmd1 | ||
// two: cmd2 | ||
// | ||
// ListNextParts([]string{"my"}) -> []string{"aliases"} | ||
// ListNextParts([]string{"my", "aliases"}) -> []string{"cmd1", "cmd2"} | ||
func ListNextParts(aliases []config.Alias, parts []string) []string { | ||
currentPrefix := strings.Join(parts, " ") | ||
suggestions := make([]string, 0) | ||
for _, a := range aliases { | ||
trail, found := strings.CutPrefix(a.Name, currentPrefix) | ||
if tf := strings.Fields(trail); found && len(tf) > 0 && !slices.Contains(suggestions, tf[0]) { | ||
suggestions = append(suggestions, tf[0]) | ||
} | ||
} | ||
|
||
return suggestions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ErrAliasNotFound = errors.New("not found") | ||
|
||
type ErrInvalidAliasKey struct { | ||
value string | ||
} | ||
|
||
func (e ErrInvalidAliasKey) Error() string { | ||
return fmt.Sprintf("Key is invalid. Did you mean `" + e.value + "`?") | ||
} |