update.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. func (c *CLIConfig) SetDriver(driver string, currentProfile string) error {
  14. v := CLIConfig{
  15. Driver: driver,
  16. }
  17. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  18. }
  19. // SetHost updates the host for the current profile. This clears the project, cluster, and token as they will not work with the new host
  20. // Trailing slashes will be dropped from the provided host as they may cause issues with the api server
  21. func (c *CLIConfig) SetHost(host string, currentProfile string) error {
  22. host = strings.TrimRight(host, "/")
  23. v := defaultCLIConfig()
  24. v.Host = host
  25. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  26. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  27. }
  28. // SetProject sets a project for all API commands
  29. func (c *CLIConfig) SetProject(ctx context.Context, apiClient api.Client, projectID uint, selectedProfile string) error {
  30. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID)
  31. v := CLIConfig{
  32. Project: projectID,
  33. }
  34. resp, err := apiClient.ListProjectClusters(ctx, projectID)
  35. if err == nil {
  36. clusters := *resp
  37. if len(clusters) == 1 {
  38. v.Cluster = clusters[0].ID
  39. }
  40. }
  41. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  42. }
  43. func (c *CLIConfig) SetCluster(clusterID uint) error {
  44. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID)
  45. if c.Kubeconfig != "" || viper.IsSet("kubeconfig") {
  46. color.New(color.FgYellow).Println("Please change local kubeconfig if needed")
  47. }
  48. err := viper.WriteConfig()
  49. if err != nil {
  50. return err
  51. }
  52. c.Cluster = clusterID
  53. return nil
  54. }
  55. func (c *CLIConfig) SetToken(token string) error {
  56. viper.Set("token", token)
  57. err := viper.WriteConfig()
  58. if err != nil {
  59. return err
  60. }
  61. c.Token = token
  62. return nil
  63. }
  64. func (c *CLIConfig) SetRegistry(registryID uint) error {
  65. viper.Set("registry", registryID)
  66. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID)
  67. err := viper.WriteConfig()
  68. if err != nil {
  69. return err
  70. }
  71. c.Registry = registryID
  72. return nil
  73. }
  74. func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
  75. viper.Set("helm_repo", helmRepoID)
  76. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID)
  77. err := viper.WriteConfig()
  78. if err != nil {
  79. return err
  80. }
  81. c.HelmRepo = helmRepoID
  82. return nil
  83. }
  84. func (c *CLIConfig) SetKubeconfig(kubeconfig string) error {
  85. path, err := filepath.Abs(kubeconfig)
  86. if err != nil {
  87. return err
  88. }
  89. if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
  90. return fmt.Errorf("%s does not exist", path)
  91. }
  92. viper.Set("kubeconfig", path)
  93. color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", path)
  94. err = viper.WriteConfig()
  95. if err != nil {
  96. return err
  97. }
  98. c.Kubeconfig = kubeconfig
  99. return nil
  100. }