|
| 1 | +package alias |
| 2 | + |
| 3 | +type Config struct { |
| 4 | + // Aliases are raw aliases that allow to expand a command |
| 5 | + // "scw instance sl", sl may be an alias and would expand command |
| 6 | + // "scw instance server list" |
| 7 | + // key = sl |
| 8 | + // value = server, list |
| 9 | + Aliases map[string][]string `yaml:"aliases"` |
| 10 | + |
| 11 | + // map of alias using their first word as key |
| 12 | + // value can contain multiple aliases with the same first word |
| 13 | + // key = instance |
| 14 | + // value = isl, isc |
| 15 | + aliasesByFirstWord map[string][]Alias |
| 16 | +} |
| 17 | + |
| 18 | +func EmptyConfig() *Config { |
| 19 | + return &Config{ |
| 20 | + Aliases: map[string][]string{}, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// GetAlias return raw alias for a given string |
| 25 | +func (c *Config) GetAlias(name string) []string { |
| 26 | + alias, aliasExists := c.Aliases[name] |
| 27 | + if aliasExists { |
| 28 | + return alias |
| 29 | + } |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +// ResolveAliases resolve aliases in given command |
| 34 | +// "scw isl" may return "scw instance server list" |
| 35 | +func (c *Config) ResolveAliases(command []string) []string { |
| 36 | + expandedCommand := make([]string, 0, len(command)) |
| 37 | + for _, arg := range command { |
| 38 | + if alias := c.GetAlias(arg); alias != nil { |
| 39 | + expandedCommand = append(expandedCommand, alias...) |
| 40 | + } else { |
| 41 | + expandedCommand = append(expandedCommand, arg) |
| 42 | + } |
| 43 | + } |
| 44 | + return expandedCommand |
| 45 | +} |
| 46 | + |
| 47 | +// AddAlias add alias to config |
| 48 | +// return true if alias has been replaced |
| 49 | +func (c *Config) AddAlias(name string, command []string) bool { |
| 50 | + _, exists := c.Aliases[name] |
| 51 | + c.Aliases[name] = command |
| 52 | + return exists |
| 53 | +} |
| 54 | + |
| 55 | +// DeleteAlias deletes an alias |
| 56 | +// return true if alias was deleted |
| 57 | +func (c *Config) DeleteAlias(name string) bool { |
| 58 | + _, exists := c.Aliases[name] |
| 59 | + delete(c.Aliases, name) |
| 60 | + return exists |
| 61 | +} |
| 62 | + |
| 63 | +func (c *Config) fillAliasByFirstWord() { |
| 64 | + c.aliasesByFirstWord = make(map[string][]Alias, len(c.Aliases)) |
| 65 | + for alias, cmd := range c.Aliases { |
| 66 | + if len(cmd) == 0 { |
| 67 | + continue |
| 68 | + } |
| 69 | + path := cmd[0] |
| 70 | + c.aliasesByFirstWord[path] = append(c.aliasesByFirstWord[path], Alias{ |
| 71 | + Name: alias, |
| 72 | + Command: cmd, |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// ResolveAliasesByFirstWord return list of aliases that start with given first word |
| 78 | +// firstWord: instance |
| 79 | +// may return |
| 80 | +// isl => instance server list |
| 81 | +// isc => instance server create |
| 82 | +func (c *Config) ResolveAliasesByFirstWord(firstWord string) ([]Alias, bool) { |
| 83 | + if c.aliasesByFirstWord == nil { |
| 84 | + c.fillAliasByFirstWord() |
| 85 | + } |
| 86 | + alias, ok := c.aliasesByFirstWord[firstWord] |
| 87 | + return alias, ok |
| 88 | +} |
0 commit comments