config.go 6.6 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. if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  172. viper.Set("kubeconfig", "")
  173. color.New(color.FgBlue).Println("Removing local kubeconfig")
  174. config.Kubeconfig = ""
  175. }
  176. viper.Set("project", projectID)
  177. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID)
  178. err := viper.WriteConfig()
  179. if err != nil {
  180. return err
  181. }
  182. config.Project = projectID
  183. return nil
  184. }
  185. func (c *CLIConfig) SetCluster(clusterID uint) error {
  186. if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  187. viper.Set("kubeconfig", "")
  188. color.New(color.FgBlue).Println("Removing local kubeconfig")
  189. config.Kubeconfig = ""
  190. }
  191. viper.Set("cluster", clusterID)
  192. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID)
  193. err := viper.WriteConfig()
  194. if err != nil {
  195. return err
  196. }
  197. config.Cluster = clusterID
  198. return nil
  199. }
  200. func (c *CLIConfig) SetToken(token string) error {
  201. viper.Set("token", token)
  202. err := viper.WriteConfig()
  203. if err != nil {
  204. return err
  205. }
  206. config.Token = token
  207. return nil
  208. }
  209. func (c *CLIConfig) SetRegistry(registryID uint) error {
  210. viper.Set("registry", registryID)
  211. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID)
  212. err := viper.WriteConfig()
  213. if err != nil {
  214. return err
  215. }
  216. config.Registry = registryID
  217. return nil
  218. }
  219. func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
  220. viper.Set("helm_repo", helmRepoID)
  221. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID)
  222. err := viper.WriteConfig()
  223. if err != nil {
  224. return err
  225. }
  226. config.HelmRepo = helmRepoID
  227. return nil
  228. }
  229. func (c *CLIConfig) SetKubeconfig(kubeconfig string) error {
  230. path, err := filepath.Abs(kubeconfig)
  231. if err != nil {
  232. return err
  233. }
  234. if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
  235. return fmt.Errorf("%s does not exist", path)
  236. }
  237. viper.Set("kubeconfig", path)
  238. color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", path)
  239. err = viper.WriteConfig()
  240. if err != nil {
  241. return err
  242. }
  243. config.Kubeconfig = kubeconfig
  244. return nil
  245. }