| 117 | } |
| 118 | |
| 119 | func run(cmd *cobra.Command, args []string) error { |
| 120 | f := cmd.Flags() |
| 121 | |
| 122 | // handle --init early (no config needed) |
| 123 | if initFlag, _ := f.GetBool("init"); initFlag { |
| 124 | home, err := homedir.Dir() |
| 125 | if err != nil { |
| 126 | fmt.Fprintf(os.Stderr, "failed to get user home directory: %v\n", err) |
| 127 | os.Exit(1) |
| 128 | } |
| 129 | envvars := config.EnvVars() |
| 130 | cmdInit(home, envvars) |
| 131 | os.Exit(0) |
| 132 | } |
| 133 | |
| 134 | // handle --version early |
| 135 | if versionFlag, _ := f.GetBool("version"); versionFlag { |
| 136 | fmt.Println(version) |
| 137 | os.Exit(0) |
| 138 | } |
| 139 | |
| 140 | // handle --completion early (no config needed) |
| 141 | if f.Changed("completion") { |
| 142 | shell, _ := f.GetString("completion") |
| 143 | return completions.Generate(cmd, shell, os.Stdout) |
| 144 | } |
| 145 | |
| 146 | // get the user's home directory |
| 147 | home, err := homedir.Dir() |
| 148 | if err != nil { |
| 149 | fmt.Fprintf(os.Stderr, "failed to get user home directory: %v\n", err) |
| 150 | os.Exit(1) |
| 151 | } |
| 152 | |
| 153 | // read the envvars into a map of strings |
| 154 | envvars := config.EnvVars() |
| 155 | |
| 156 | // identify the os-specific paths at which configs may be located |
| 157 | confpaths, err := config.Paths(runtime.GOOS, home, envvars) |
| 158 | if err != nil { |
| 159 | fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err) |
| 160 | os.Exit(1) |
| 161 | } |
| 162 | |
| 163 | // search for the config file in the above paths |
| 164 | confpath, err := config.Path(confpaths) |
| 165 | if err != nil { |
| 166 | // prompt the user to create a config file |
| 167 | yes, err := installer.Prompt( |
| 168 | "A config file was not found. Would you like to create one now? [Y/n]", |
| 169 | true, |
| 170 | ) |
| 171 | if err != nil { |
| 172 | fmt.Fprintf(os.Stderr, "failed to create config: %v\n", err) |
| 173 | os.Exit(1) |
| 174 | } |
| 175 | |
| 176 | // exit early on a negative answer |