ExpandAlias processes argv to see if it should be rewritten according to a user's aliases.
(expansion string, args []string)
| 77 | |
| 78 | // ExpandAlias processes argv to see if it should be rewritten according to a user's aliases. |
| 79 | func expandAlias(expansion string, args []string) ([]string, error) { |
| 80 | extraArgs := []string{} |
| 81 | for i, a := range args { |
| 82 | if !strings.Contains(expansion, "$") { |
| 83 | extraArgs = append(extraArgs, a) |
| 84 | } else { |
| 85 | expansion = strings.ReplaceAll(expansion, fmt.Sprintf("$%d", i+1), a) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | lingeringRE := regexp.MustCompile(`\$\d`) |
| 90 | if lingeringRE.MatchString(expansion) { |
| 91 | return nil, fmt.Errorf("not enough arguments for alias: %s", expansion) |
| 92 | } |
| 93 | |
| 94 | newArgs, err := shlex.Split(expansion) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | |
| 99 | expanded := append(newArgs, extraArgs...) |
| 100 | |
| 101 | return expanded, nil |
| 102 | } |
| 103 | |
| 104 | // ExpandShellAlias processes argv to see if it should be rewritten according to a user's aliases. |
| 105 | func expandShellAlias(expansion string, args []string, findShFunc func() (string, error)) ([]string, error) { |