update.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package config
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/fatih/color"
  10. api "github.com/porter-dev/porter/api/client"
  11. "github.com/spf13/viper"
  12. )
  13. // SetDriver sets the driver used when building images. This can either be locla or github
  14. func (c *CLIConfig) SetDriver(driver string, currentProfile string) error {
  15. v := CLIConfig{
  16. Driver: driver,
  17. }
  18. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  19. }
  20. // SetHost updates the host for the current profile. This clears the project, cluster, and token as they will not work with the new host
  21. // Trailing slashes will be dropped from the provided host as they may cause issues with the api server
  22. func (c *CLIConfig) SetHost(host string, currentProfile string) error {
  23. host = strings.TrimRight(host, "/")
  24. v := defaultCLIConfig()
  25. v.Host = host
  26. color.New(color.FgGreen).Printf("Set the current host as %s\n", host) //nolint:errcheck,gosec
  27. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  28. }
  29. // SetProject sets a project for all API commands
  30. func (c *CLIConfig) SetProject(ctx context.Context, apiClient api.Client, projectID uint, selectedProfile string) error {
  31. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID) //nolint:errcheck,gosec
  32. v := CLIConfig{
  33. Project: projectID,
  34. }
  35. resp, err := apiClient.ListProjectClusters(ctx, projectID)
  36. if err == nil {
  37. clusters := *resp
  38. if len(clusters) == 1 {
  39. v.Cluster = clusters[0].ID
  40. }
  41. }
  42. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  43. }
  44. // SetCluster sets the cluster in the current profile. All further actions will be targeted at this cluster
  45. func (c *CLIConfig) SetCluster(clusterID uint) error {
  46. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID) //nolint:errcheck,gosec
  47. if c.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  48. color.New(color.FgYellow).Println("Please change local kubeconfig if needed") //nolint:errcheck,gosec
  49. }
  50. err := viper.WriteConfig()
  51. if err != nil {
  52. return err
  53. }
  54. c.Cluster = clusterID
  55. return nil
  56. }
  57. // SetToken sets the token in the current profile. All further actions will be authenticated with this token
  58. func (c *CLIConfig) SetToken(token string) error {
  59. viper.Set("token", token)
  60. err := viper.WriteConfig()
  61. if err != nil {
  62. return err
  63. }
  64. c.Token = token
  65. return nil
  66. }
  67. // SetRegistry sets the docker registry in the current profile. All further actions will be targeted at this registry
  68. func (c *CLIConfig) SetRegistry(registryID uint) error {
  69. viper.Set("registry", registryID)
  70. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID) //nolint:errcheck,gosec
  71. err := viper.WriteConfig()
  72. if err != nil {
  73. return err
  74. }
  75. c.Registry = registryID
  76. return nil
  77. }
  78. // SetHelmRepo sets the helm repo in the current profile. All further actions will be targeted at this helm repo
  79. func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
  80. viper.Set("helm_repo", helmRepoID)
  81. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID) //nolint:errcheck,gosec
  82. err := viper.WriteConfig()
  83. if err != nil {
  84. return err
  85. }
  86. c.HelmRepo = helmRepoID
  87. return nil
  88. }
  89. // SetKubeconfig sets the kubeconfig in the current profile. All further actions which require kubernetes will be targeted at this kubeconfig
  90. func (c *CLIConfig) SetKubeconfig(kubeconfig string) error {
  91. path, err := filepath.Abs(kubeconfig)
  92. if err != nil {
  93. return err
  94. }
  95. if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
  96. return fmt.Errorf("%s does not exist", path)
  97. }
  98. viper.Set("kubeconfig", path)
  99. color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", path) //nolint:errcheck,gosec
  100. err = viper.WriteConfig()
  101. if err != nil {
  102. return err
  103. }
  104. c.Kubeconfig = kubeconfig
  105. return nil
  106. }