config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/fatih/color"
  10. api "github.com/porter-dev/porter/api/client"
  11. "github.com/porter-dev/porter/cli/cmd/utils"
  12. "github.com/spf13/viper"
  13. "k8s.io/client-go/util/homedir"
  14. )
  15. var home = homedir.HomeDir()
  16. // config is a shared object used by all commands
  17. var config = &CLIConfig{}
  18. // CLIConfig is the set of shared configuration options for the CLI commands.
  19. // This config is used by viper: calling Set() function for any parameter will
  20. // update the corresponding field in the viper config file.
  21. type CLIConfig struct {
  22. // Driver can be either "docker" or "local", and represents which driver is
  23. // used to run an instance of the server.
  24. Driver string `yaml:"driver"`
  25. Host string `yaml:"host"`
  26. Project uint `yaml:"project"`
  27. Cluster uint `yaml:"cluster"`
  28. Token string `yaml:"token"`
  29. Registry uint `yaml:"registry"`
  30. HelmRepo uint `yaml:"helm_repo"`
  31. Kubeconfig string `yaml:"kubeconfig"`
  32. }
  33. // InitAndLoadConfig populates the config object with the following precedence rules:
  34. // 1. flag
  35. // 2. env
  36. // 3. config
  37. // 4. default
  38. //
  39. // It populates the shared config object above
  40. func InitAndLoadConfig() {
  41. initAndLoadConfig(config)
  42. }
  43. func InitAndLoadNewConfig() *CLIConfig {
  44. newConfig := &CLIConfig{}
  45. initAndLoadConfig(newConfig)
  46. return newConfig
  47. }
  48. func initAndLoadConfig(_config *CLIConfig) {
  49. initFlagSet()
  50. // check that the .porter folder exists; create if not
  51. porterDir := filepath.Join(home, ".porter")
  52. if _, err := os.Stat(porterDir); os.IsNotExist(err) {
  53. os.Mkdir(porterDir, 0700)
  54. } else if err != nil {
  55. color.New(color.FgRed).Fprintf(os.Stderr, "%v\n", err)
  56. os.Exit(1)
  57. }
  58. viper.SetConfigName("porter")
  59. viper.SetConfigType("yaml")
  60. viper.AddConfigPath(porterDir)
  61. // Bind the flagset initialized above
  62. viper.BindPFlags(utils.DriverFlagSet)
  63. viper.BindPFlags(utils.DefaultFlagSet)
  64. viper.BindPFlags(utils.RegistryFlagSet)
  65. viper.BindPFlags(utils.HelmRepoFlagSet)
  66. // Bind the environment variables with prefix "PORTER_"
  67. viper.SetEnvPrefix("PORTER")
  68. viper.BindEnv("host")
  69. viper.BindEnv("project")
  70. viper.BindEnv("cluster")
  71. viper.BindEnv("token")
  72. err := viper.ReadInConfig()
  73. if err != nil {
  74. if _, ok := err.(viper.ConfigFileNotFoundError); ok {
  75. // create blank config file
  76. err := ioutil.WriteFile(filepath.Join(home, ".porter", "porter.yaml"), []byte{}, 0644)
  77. if err != nil {
  78. color.New(color.FgRed).Fprintf(os.Stderr, "%v\n", err)
  79. os.Exit(1)
  80. }
  81. } else {
  82. // Config file was found but another error was produced
  83. color.New(color.FgRed).Fprintf(os.Stderr, "%v\n", err)
  84. os.Exit(1)
  85. }
  86. }
  87. // unmarshal the config into the shared config struct
  88. viper.Unmarshal(_config)
  89. }
  90. // initFlagSet initializes the shared flags used by multiple commands
  91. func initFlagSet() {
  92. utils.DriverFlagSet.StringVar(
  93. &config.Driver,
  94. "driver",
  95. "local",
  96. "driver to use (local or docker)",
  97. )
  98. utils.DefaultFlagSet.StringVar(
  99. &config.Host,
  100. "host",
  101. "https://dashboard.getporter.dev",
  102. "host URL of Porter instance",
  103. )
  104. utils.DefaultFlagSet.UintVar(
  105. &config.Project,
  106. "project",
  107. 0,
  108. "project ID of Porter project",
  109. )
  110. utils.DefaultFlagSet.UintVar(
  111. &config.Cluster,
  112. "cluster",
  113. 0,
  114. "cluster ID of Porter cluster",
  115. )
  116. utils.DefaultFlagSet.StringVar(
  117. &config.Token,
  118. "token",
  119. "",
  120. "token for Porter authentication",
  121. )
  122. utils.RegistryFlagSet.UintVar(
  123. &config.Registry,
  124. "registry",
  125. 0,
  126. "registry ID of connected Porter registry",
  127. )
  128. utils.HelmRepoFlagSet.UintVar(
  129. &config.HelmRepo,
  130. "helmrepo",
  131. 0,
  132. "helm repo ID of connected Porter Helm repository",
  133. )
  134. }
  135. func GetCLIConfig() *CLIConfig {
  136. if config == nil {
  137. panic("GetCLIConfig() called before initialisation")
  138. }
  139. return config
  140. }
  141. func GetAPIClient() *api.Client {
  142. config := GetCLIConfig()
  143. if token := config.Token; token != "" {
  144. return api.NewClientWithToken(config.Host+"/api", token)
  145. }
  146. return api.NewClient(config.Host+"/api", "cookie.json")
  147. }
  148. func (c *CLIConfig) SetDriver(driver string) error {
  149. viper.Set("driver", driver)
  150. color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
  151. err := viper.WriteConfig()
  152. if err != nil {
  153. return err
  154. }
  155. config.Driver = driver
  156. return nil
  157. }
  158. func (c *CLIConfig) SetHost(host string) error {
  159. // a trailing / can lead to errors with the api server
  160. host = strings.TrimRight(host, "/")
  161. viper.Set("host", host)
  162. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  163. err := viper.WriteConfig()
  164. if err != nil {
  165. return err
  166. }
  167. config.Host = host
  168. return nil
  169. }
  170. func (c *CLIConfig) SetProject(projectID uint) error {
  171. viper.Set("project", projectID)
  172. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID)
  173. if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  174. color.New(color.FgYellow).Println("Please change local kubeconfig if needed")
  175. }
  176. err := viper.WriteConfig()
  177. if err != nil {
  178. return err
  179. }
  180. config.Project = projectID
  181. return nil
  182. }
  183. func (c *CLIConfig) SetCluster(clusterID uint) error {
  184. viper.Set("cluster", clusterID)
  185. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID)
  186. if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  187. color.New(color.FgYellow).Println("Please change local kubeconfig if needed")
  188. }
  189. err := viper.WriteConfig()
  190. if err != nil {
  191. return err
  192. }
  193. config.Cluster = clusterID
  194. return nil
  195. }
  196. func (c *CLIConfig) SetToken(token string) error {
  197. viper.Set("token", token)
  198. err := viper.WriteConfig()
  199. if err != nil {
  200. return err
  201. }
  202. config.Token = token
  203. return nil
  204. }
  205. func (c *CLIConfig) SetRegistry(registryID uint) error {
  206. viper.Set("registry", registryID)
  207. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID)
  208. err := viper.WriteConfig()
  209. if err != nil {
  210. return err
  211. }
  212. config.Registry = registryID
  213. return nil
  214. }
  215. func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
  216. viper.Set("helm_repo", helmRepoID)
  217. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID)
  218. err := viper.WriteConfig()
  219. if err != nil {
  220. return err
  221. }
  222. config.HelmRepo = helmRepoID
  223. return nil
  224. }
  225. func (c *CLIConfig) SetKubeconfig(kubeconfig string) error {
  226. path, err := filepath.Abs(kubeconfig)
  227. if err != nil {
  228. return err
  229. }
  230. if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
  231. return fmt.Errorf("%s does not exist", path)
  232. }
  233. viper.Set("kubeconfig", path)
  234. color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", path)
  235. err = viper.WriteConfig()
  236. if err != nil {
  237. return err
  238. }
  239. config.Kubeconfig = kubeconfig
  240. return nil
  241. }