Skip to content

Commit

Permalink
feat: expose alias generator functions (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
jazanne authored Nov 2, 2023
1 parent a1e6e82 commit 68be382
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
43 changes: 28 additions & 15 deletions aliases/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,45 @@ func GenerateAliases(flags []string, aliases []options.Alias, dir string) (map[s
return ret, nil
}

func generateAlias(a options.Alias, flag, dir string, allFileContents map[string][]byte) (ret []string, err error) {
func generateAlias(a options.Alias, flag, dir string, allFileContents FileContentsMap) (ret []string, err error) {
switch a.Type.Canonical() {
case options.Literal:
ret = a.Flags[flag]
case options.FilePattern:
ret, err = GenerateAliasesFromFilePattern(a, flag, dir, allFileContents)
case options.Command:
ret, err = GenerateAliasesFromCommand(a, flag, dir)
default:
var alias string
alias, err = GenerateNamingConventionAlias(a, flag)
ret = []string{alias}
}

return ret, err
}

func GenerateNamingConventionAlias(a options.Alias, flag string) (alias string, err error) {
switch a.Type.Canonical() {
case options.CamelCase:
ret = []string{strcase.ToLowerCamel(flag)}
alias = strcase.ToLowerCamel(flag)
case options.PascalCase:
ret = []string{strcase.ToCamel(flag)}
alias = strcase.ToCamel(flag)
case options.SnakeCase:
ret = []string{strcase.ToSnake(flag)}
alias = strcase.ToSnake(flag)
case options.UpperSnakeCase:
ret = []string{strcase.ToScreamingSnake(flag)}
alias = strcase.ToScreamingSnake(flag)
case options.KebabCase:
ret = []string{strcase.ToKebab(flag)}
alias = strcase.ToKebab(flag)
case options.DotCase:
ret = []string{strcase.ToDelimited(flag, '.')}
case options.FilePattern:
ret, err = generateAliasesFromFilePattern(a, flag, dir, allFileContents)
case options.Command:
ret, err = generateAliasesFromCommand(a, flag, dir)
alias = strcase.ToDelimited(flag, '.')
default:
err = fmt.Errorf("naming convention alias type %s not recognized", a.Type)
}

return ret, err
return alias, err
}

func generateAliasesFromFilePattern(a options.Alias, flag, dir string, allFileContents map[string][]byte) ([]string, error) {
func GenerateAliasesFromFilePattern(a options.Alias, flag, dir string, allFileContents FileContentsMap) ([]string, error) {
ret := []string{}
// Concatenate the contents of all files into a single byte array to be matched by specified patterns
fileContents := []byte{}
Expand Down Expand Up @@ -100,7 +113,7 @@ func generateAliasesFromFilePattern(a options.Alias, flag, dir string, allFileCo
return ret, nil
}

func generateAliasesFromCommand(a options.Alias, flag, dir string) ([]string, error) {
func GenerateAliasesFromCommand(a options.Alias, flag, dir string) ([]string, error) {
ret := []string{}
ctx := context.Background()
if a.Timeout != nil && *a.Timeout > 0 {
Expand Down Expand Up @@ -130,7 +143,7 @@ func generateAliasesFromCommand(a options.Alias, flag, dir string) ([]string, er
}

// processFileContent reads and stores the content of files specified by filePattern alias matchers to be matched for aliases
func processFileContent(aliases []options.Alias, dir string) (map[string][]byte, error) {
func processFileContent(aliases []options.Alias, dir string) (FileContentsMap, error) {
allFileContents := map[string][]byte{}
for idx, a := range aliases {
if a.Type.Canonical() != options.FilePattern {
Expand Down
4 changes: 4 additions & 0 deletions aliases/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package aliases

// Map of full file path to file contents
type FileContentsMap = map[string][]byte

0 comments on commit 68be382

Please sign in to comment.