config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package cmd
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "github.com/fatih/color"
  7. "github.com/spf13/viper"
  8. flag "github.com/spf13/pflag"
  9. )
  10. // shared sets of flags used by multiple commands
  11. var driverFlagSet = flag.NewFlagSet("driver", flag.ExitOnError)
  12. var defaultFlagSet = flag.NewFlagSet("shared", flag.ExitOnError) // used by all commands
  13. var registryFlagSet = flag.NewFlagSet("registry", flag.ExitOnError)
  14. var helmRepoFlagSet = flag.NewFlagSet("helmrepo", flag.ExitOnError)
  15. // config is a shared object used by all commands
  16. var config = &CLIConfig{}
  17. // CLIConfig is the set of shared configuration options for the CLI commands.
  18. // This config is used by viper: calling Set() function for any parameter will
  19. // update the corresponding field in the viper config file.
  20. type CLIConfig struct {
  21. // Driver can be either "docker" or "local", and represents which driver is
  22. // used to run an instance of the server.
  23. Driver string `yaml:"driver"`
  24. Host string `yaml:"host"`
  25. Project uint `yaml:"project"`
  26. Cluster uint `yaml:"cluster"`
  27. Token string `yaml:"token"`
  28. Registry uint `yaml:"registry"`
  29. HelmRepo uint `yaml:"helm_repo"`
  30. }
  31. // InitAndLoadConfig populates the config object with the following precedence rules:
  32. // 1. flag
  33. // 2. env
  34. // 3. config
  35. // 4. default
  36. //
  37. // It populates the shared config object above
  38. func InitAndLoadConfig() {
  39. initAndLoadConfig(config)
  40. }
  41. func InitAndLoadNewConfig() *CLIConfig {
  42. newConfig := &CLIConfig{}
  43. initAndLoadConfig(newConfig)
  44. return newConfig
  45. }
  46. func initAndLoadConfig(_config *CLIConfig) {
  47. initFlagSet()
  48. // check that the .porter folder exists; create if not
  49. porterDir := filepath.Join(home, ".porter")
  50. if _, err := os.Stat(porterDir); os.IsNotExist(err) {
  51. os.Mkdir(porterDir, 0700)
  52. } else if err != nil {
  53. color.New(color.FgRed).Printf("%v\n", err)
  54. os.Exit(1)
  55. }
  56. viper.SetConfigName("porter")
  57. viper.SetConfigType("yaml")
  58. viper.AddConfigPath(porterDir)
  59. // Bind the flagset initialized above
  60. viper.BindPFlags(driverFlagSet)
  61. viper.BindPFlags(defaultFlagSet)
  62. viper.BindPFlags(registryFlagSet)
  63. viper.BindPFlags(helmRepoFlagSet)
  64. // Bind the environment variables with prefix "PORTER_"
  65. viper.SetEnvPrefix("PORTER")
  66. viper.BindEnv("host")
  67. viper.BindEnv("project")
  68. viper.BindEnv("cluster")
  69. viper.BindEnv("token")
  70. err := viper.ReadInConfig()
  71. if err != nil {
  72. if _, ok := err.(viper.ConfigFileNotFoundError); ok {
  73. // create blank config file
  74. err := ioutil.WriteFile(filepath.Join(home, ".porter", "porter.yaml"), []byte{}, 0644)
  75. if err != nil {
  76. color.New(color.FgRed).Printf("%v\n", err)
  77. os.Exit(1)
  78. }
  79. } else {
  80. // Config file was found but another error was produced
  81. color.New(color.FgRed).Printf("%v\n", err)
  82. os.Exit(1)
  83. }
  84. }
  85. // unmarshal the config into the shared config struct
  86. viper.Unmarshal(_config)
  87. }
  88. // initFlagSet initializes the shared flags used by multiple commands
  89. func initFlagSet() {
  90. driverFlagSet.StringVar(
  91. &config.Driver,
  92. "driver",
  93. "local",
  94. "driver to use (local or docker)",
  95. )
  96. defaultFlagSet.StringVar(
  97. &config.Host,
  98. "host",
  99. "https://dashboard.getporter.dev",
  100. "host URL of Porter instance",
  101. )
  102. defaultFlagSet.UintVar(
  103. &config.Project,
  104. "project",
  105. 0,
  106. "project ID of Porter project",
  107. )
  108. defaultFlagSet.UintVar(
  109. &config.Cluster,
  110. "cluster",
  111. 0,
  112. "cluster ID of Porter cluster",
  113. )
  114. defaultFlagSet.StringVar(
  115. &config.Token,
  116. "token",
  117. "",
  118. "token for Porter authentication",
  119. )
  120. registryFlagSet.UintVar(
  121. &config.Registry,
  122. "registry",
  123. 0,
  124. "registry ID of connected Porter registry",
  125. )
  126. helmRepoFlagSet.UintVar(
  127. &config.HelmRepo,
  128. "helmrepo",
  129. 0,
  130. "helm repo ID of connected Porter Helm repository",
  131. )
  132. }
  133. func (c *CLIConfig) SetDriver(driver string) error {
  134. viper.Set("driver", driver)
  135. color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
  136. err := viper.WriteConfig()
  137. if err != nil {
  138. return err
  139. }
  140. config.Driver = driver
  141. return nil
  142. }
  143. func (c *CLIConfig) SetHost(host string) error {
  144. viper.Set("host", host)
  145. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  146. err := viper.WriteConfig()
  147. if err != nil {
  148. return err
  149. }
  150. config.Host = host
  151. return nil
  152. }
  153. func (c *CLIConfig) SetProject(projectID uint) error {
  154. viper.Set("project", projectID)
  155. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID)
  156. err := viper.WriteConfig()
  157. if err != nil {
  158. return err
  159. }
  160. config.Project = projectID
  161. return nil
  162. }
  163. func (c *CLIConfig) SetCluster(clusterID uint) error {
  164. viper.Set("cluster", clusterID)
  165. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID)
  166. err := viper.WriteConfig()
  167. if err != nil {
  168. return err
  169. }
  170. config.Cluster = clusterID
  171. return nil
  172. }
  173. func (c *CLIConfig) SetToken(token string) error {
  174. viper.Set("token", token)
  175. err := viper.WriteConfig()
  176. if err != nil {
  177. return err
  178. }
  179. config.Token = token
  180. return nil
  181. }
  182. func (c *CLIConfig) SetRegistry(registryID uint) error {
  183. viper.Set("registry", registryID)
  184. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID)
  185. err := viper.WriteConfig()
  186. if err != nil {
  187. return err
  188. }
  189. config.Registry = registryID
  190. return nil
  191. }
  192. func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
  193. viper.Set("helm_repo", helmRepoID)
  194. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID)
  195. err := viper.WriteConfig()
  196. if err != nil {
  197. return err
  198. }
  199. config.HelmRepo = helmRepoID
  200. return nil
  201. }
  202. // var configCmd = &cobra.Command{
  203. // Use: "config",
  204. // Short: "Commands that control local configuration settings",
  205. // Run: func(cmd *cobra.Command, args []string) {
  206. // if err := printConfig(); err != nil {
  207. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  208. // os.Exit(1)
  209. // }
  210. // },
  211. // }
  212. // var config.SetProjectCmd = &cobra.Command{
  213. // Use: "set-project [id]",
  214. // Args: cobra.ExactArgs(1),
  215. // Short: "Saves the project id in the default configuration",
  216. // Run: func(cmd *cobra.Command, args []string) {
  217. // projID, err := strconv.ParseUint(args[0], 10, 64)
  218. // if err != nil {
  219. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  220. // os.Exit(1)
  221. // }
  222. // err = config.SetProject(uint(projID))
  223. // if err != nil {
  224. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  225. // os.Exit(1)
  226. // }
  227. // },
  228. // }
  229. // var config.SetClusterCmd = &cobra.Command{
  230. // Use: "set-cluster [id]",
  231. // Args: cobra.ExactArgs(1),
  232. // Short: "Saves the cluster id in the default configuration",
  233. // Run: func(cmd *cobra.Command, args []string) {
  234. // clusterID, err := strconv.ParseUint(args[0], 10, 64)
  235. // if err != nil {
  236. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  237. // os.Exit(1)
  238. // }
  239. // err = config.SetCluster(uint(clusterID))
  240. // if err != nil {
  241. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  242. // os.Exit(1)
  243. // }
  244. // },
  245. // }
  246. // var setRegistryCmd = &cobra.Command{
  247. // Use: "set-registry [id]",
  248. // Args: cobra.ExactArgs(1),
  249. // Short: "Saves the registry id in the default configuration",
  250. // Run: func(cmd *cobra.Command, args []string) {
  251. // registryID, err := strconv.ParseUint(args[0], 10, 64)
  252. // if err != nil {
  253. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  254. // os.Exit(1)
  255. // }
  256. // err = setRegistry(uint(registryID))
  257. // if err != nil {
  258. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  259. // os.Exit(1)
  260. // }
  261. // },
  262. // }
  263. // var setHelmRepoCmd = &cobra.Command{
  264. // Use: "set-helmrepo [id]",
  265. // Args: cobra.ExactArgs(1),
  266. // Short: "Saves the helm repo id in the default configuration",
  267. // Run: func(cmd *cobra.Command, args []string) {
  268. // hrID, err := strconv.ParseUint(args[0], 10, 64)
  269. // if err != nil {
  270. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  271. // os.Exit(1)
  272. // }
  273. // err = setHelmRepo(uint(hrID))
  274. // if err != nil {
  275. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  276. // os.Exit(1)
  277. // }
  278. // },
  279. // }
  280. // var config.SetHostCmd = &cobra.Command{
  281. // Use: "set-host [host]",
  282. // Args: cobra.ExactArgs(1),
  283. // Short: "Saves the host in the default configuration",
  284. // Run: func(cmd *cobra.Command, args []string) {
  285. // err := config.SetHost(args[0])
  286. // if err != nil {
  287. // color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  288. // os.Exit(1)
  289. // }
  290. // },
  291. // }
  292. // func init() {
  293. // rootCmd.AddCommand(configCmd)
  294. // configCmd.AddCommand(config.SetProjectCmd)
  295. // configCmd.AddCommand(config.SetClusterCmd)
  296. // configCmd.AddCommand(config.SetHostCmd)
  297. // configCmd.AddCommand(setRegistryCmd)
  298. // configCmd.AddCommand(setHelmRepoCmd)
  299. // }
  300. // func setDriver(driver string) error {
  301. // viper.Set("driver", driver)
  302. // err := viper.WriteConfig()
  303. // color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
  304. // return err
  305. // }
  306. // func getDriver() string {
  307. // if driver != "" {
  308. // return driver
  309. // }
  310. // if opts.driver != "" {
  311. // return opts.driver
  312. // }
  313. // return viper.GetString("driver")
  314. // }
  315. // func printConfig() error {
  316. // config, err := ioutil.ReadFile(filepath.Join(home, ".porter", "porter.yaml"))
  317. // if err != nil {
  318. // return err
  319. // }
  320. // fmt.Printf(string(config))
  321. // return nil
  322. // }
  323. // func config.SetProject(id uint) error {
  324. // viper.Set("project", id)
  325. // color.New(color.FgGreen).Printf("Set the current project id as %d\n", id)
  326. // return viper.WriteConfig()
  327. // }
  328. // func config.SetCluster(id uint) error {
  329. // viper.Set("cluster", id)
  330. // color.New(color.FgGreen).Printf("Set the current cluster id as %d\n", id)
  331. // return viper.WriteConfig()
  332. // }
  333. // func setRegistry(id uint) error {
  334. // viper.Set("registry", id)
  335. // color.New(color.FgGreen).Printf("Set the current registry id as %d\n", id)
  336. // return viper.WriteConfig()
  337. // }
  338. // func setHelmRepo(id uint) error {
  339. // viper.Set("helm_repo", id)
  340. // color.New(color.FgGreen).Printf("Set the current helm repo id as %d\n", id)
  341. // return viper.WriteConfig()
  342. // }
  343. // func config.SetHost(host string) error {
  344. // viper.Set("host", host)
  345. // err := viper.WriteConfig()
  346. // color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  347. // return err
  348. // }
  349. // func config.SetToken(token string) error {
  350. // viper.Set("token", token)
  351. // err := viper.WriteConfig()
  352. // return err
  353. // }
  354. // func config.Host string {
  355. // if host != "" {
  356. // return host
  357. // }
  358. // return viper.GetString("host")
  359. // }
  360. // func config.Token string {
  361. // return viper.GetString("token")
  362. // }
  363. // func config.Cluster() uint {
  364. // if clusterID != 0 {
  365. // return clusterID
  366. // }
  367. // return viper.GetUint("cluster")
  368. // }
  369. // func config.Registry uint {
  370. // if registryID != 0 {
  371. // return registryID
  372. // }
  373. // return viper.GetUint("registry")
  374. // }
  375. // func config.HelmRepo() uint {
  376. // if helmRepoID != 0 {
  377. // return helmRepoID
  378. // }
  379. // return viper.GetUint("helm_repo")
  380. // }
  381. // func config.Project uint {
  382. // if projectID != 0 {
  383. // return projectID
  384. // }
  385. // return viper.GetUint("project")
  386. // }