config.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, 0o700)
  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{}, 0o644)
  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. // let us clear the project ID, cluster ID, and token when we reset a host
  163. viper.Set("project", 0)
  164. viper.Set("cluster", 0)
  165. viper.Set("token", "")
  166. err := viper.WriteConfig()
  167. if err != nil {
  168. return err
  169. }
  170. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  171. config.Host = host
  172. config.Project = 0
  173. config.Cluster = 0
  174. config.Token = ""
  175. return nil
  176. }
  177. func (c *CLIConfig) SetProject(projectID uint) error {
  178. viper.Set("project", projectID)
  179. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID)
  180. if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  181. color.New(color.FgYellow).Println("Please change local kubeconfig if needed")
  182. }
  183. err := viper.WriteConfig()
  184. if err != nil {
  185. return err
  186. }
  187. config.Project = projectID
  188. return nil
  189. }
  190. func (c *CLIConfig) SetCluster(clusterID uint) error {
  191. viper.Set("cluster", clusterID)
  192. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID)
  193. if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  194. color.New(color.FgYellow).Println("Please change local kubeconfig if needed")
  195. }
  196. err := viper.WriteConfig()
  197. if err != nil {
  198. return err
  199. }
  200. config.Cluster = clusterID
  201. return nil
  202. }
  203. func (c *CLIConfig) SetToken(token string) error {
  204. viper.Set("token", token)
  205. err := viper.WriteConfig()
  206. if err != nil {
  207. return err
  208. }
  209. config.Token = token
  210. return nil
  211. }
  212. func (c *CLIConfig) SetRegistry(registryID uint) error {
  213. viper.Set("registry", registryID)
  214. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID)
  215. err := viper.WriteConfig()
  216. if err != nil {
  217. return err
  218. }
  219. config.Registry = registryID
  220. return nil
  221. }
  222. func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
  223. viper.Set("helm_repo", helmRepoID)
  224. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID)
  225. err := viper.WriteConfig()
  226. if err != nil {
  227. return err
  228. }
  229. config.HelmRepo = helmRepoID
  230. return nil
  231. }
  232. func (c *CLIConfig) SetKubeconfig(kubeconfig string) error {
  233. path, err := filepath.Abs(kubeconfig)
  234. if err != nil {
  235. return err
  236. }
  237. if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
  238. return fmt.Errorf("%s does not exist", path)
  239. }
  240. viper.Set("kubeconfig", path)
  241. color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", path)
  242. err = viper.WriteConfig()
  243. if err != nil {
  244. return err
  245. }
  246. config.Kubeconfig = kubeconfig
  247. return nil
  248. }
  249. func ValidateCLIEnvironment() error {
  250. if GetCLIConfig().Token == "" {
  251. return fmt.Errorf("no auth token present, please run 'porter auth login' to authenticate")
  252. }
  253. if GetCLIConfig().Project == 0 {
  254. return fmt.Errorf("no project selected, please run 'porter config set-project' to select a project")
  255. }
  256. if GetCLIConfig().Cluster == 0 {
  257. return fmt.Errorf("no cluster selected, please run 'porter config set-cluster' to select a cluster")
  258. }
  259. return nil
  260. }