|
| 1 | +package action |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/rsteube/carapace" |
| 8 | + "gopkg.in/yaml.v3" |
| 9 | +) |
| 10 | + |
| 11 | +func ActionConfigHosts() carapace.Action { |
| 12 | + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { |
| 13 | + if hosts, err := hosts(); err != nil { |
| 14 | + return carapace.ActionMessage(err.Error()) |
| 15 | + } else { |
| 16 | + return carapace.ActionValues(hosts...) |
| 17 | + } |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +type glabConfig struct { |
| 22 | + Hosts map[string]interface{} |
| 23 | +} |
| 24 | + |
| 25 | +func hosts() ([]string, error) { |
| 26 | + config, err := loadConfig() |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + hosts := make([]string, 0) |
| 32 | + for host := range config.Hosts { |
| 33 | + hosts = append(hosts, host) |
| 34 | + } |
| 35 | + return hosts, nil |
| 36 | +} |
| 37 | + |
| 38 | +func loadConfig() (config *glabConfig, err error) { |
| 39 | + var dir string |
| 40 | + if dir, err = os.UserConfigDir(); err == nil { |
| 41 | + var content []byte |
| 42 | + if content, err = ioutil.ReadFile(dir + "/glab-cli/config.yml"); err == nil { |
| 43 | + err = yaml.Unmarshal(content, &config) |
| 44 | + } |
| 45 | + } |
| 46 | + return |
| 47 | +} |
| 48 | + |
| 49 | +func ActionConfigKeys() carapace.Action { |
| 50 | + return carapace.ActionValuesDescribed( |
| 51 | + "token", "Your gitlab access token, defaults to environment variables", |
| 52 | + "gitlab_uri", "if unset, defaults to https://gitlab.com", |
| 53 | + "browser", "if unset, defaults to environment variables", |
| 54 | + "editor", "if unset, defaults to environment variables.", |
| 55 | + "visual", "alternative for editor. if unset, defaults to environment variables.", |
| 56 | + "glamour_style", "Your desired markdown renderer style.", |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +func ActionConfigValues(key string) carapace.Action { |
| 61 | + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { |
| 62 | + actions := map[string]carapace.Action{ |
| 63 | + "token": carapace.ActionValues(), |
| 64 | + "gitlab_uri": carapace.ActionValues(), |
| 65 | + "browser": carapace.ActionFiles(), |
| 66 | + "editor": carapace.ActionFiles(), |
| 67 | + "visual": carapace.ActionFiles(), |
| 68 | + "glamour_style": carapace.ActionValues("dark", "light", "notty"), |
| 69 | + } |
| 70 | + |
| 71 | + return actions[key] |
| 72 | + }) |
| 73 | +} |
0 commit comments