update.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/fatih/color"
  9. )
  10. // SetProfile sets the current profile to the supplied name. If one does not exist, it will be created
  11. func SetProfile(currentProfile string) error {
  12. fmt.Println("Setting profile to", currentProfile)
  13. err := updateCurrentProfileInFile(currentProfile, porterConfigFilePath)
  14. if err != nil {
  15. return fmt.Errorf("unable to update current profile in file: %w", err)
  16. }
  17. return nil
  18. }
  19. // SetDriver sets the driver used when building images. This can either be locla or github
  20. func SetDriver(driver string, currentProfile string) error {
  21. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withDriver(driver))
  22. }
  23. // SetHost updates the host for the current profile. This clears the project, cluster, and token as they will not work with the new host
  24. // Trailing slashes will be dropped from the provided host as they may cause issues with the api server
  25. func SetHost(host string, currentProfile string) error {
  26. host = strings.TrimRight(host, "/")
  27. color.New(color.FgGreen).Printf("Set the current host as %s\n", host) //nolint:errcheck,gosec
  28. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withHost(host))
  29. }
  30. // SetProject sets a project for all API commands
  31. func SetProject(projectID uint, currentProfile string) error {
  32. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID) //nolint:errcheck,gosec
  33. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withProjectID(projectID))
  34. }
  35. // SetCluster sets the cluster in the current profile. All further actions will be targeted at this cluster
  36. func SetCluster(clusterID uint, currentProfile string) error {
  37. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID) //nolint:errcheck,gosec
  38. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withClusterID(clusterID), withKubeconfig(""))
  39. }
  40. // SetToken sets the token in the current profile. All further actions will be authenticated with this token
  41. func SetToken(token string, currentProfile string) error {
  42. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withToken(token))
  43. }
  44. // SetRegistry sets the docker registry in the current profile. All further actions will be targeted at this registry
  45. func SetRegistry(registryID uint) error {
  46. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID) //nolint:errcheck,gosec
  47. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withRegistryID(registryID))
  48. }
  49. // SetHelmRepo sets the helm repo in the current profile. All further actions will be targeted at this helm repo
  50. func SetHelmRepo(helmRepoID uint) error {
  51. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID) //nolint:errcheck,gosec
  52. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withHelmRepoID(helmRepoID))
  53. }
  54. // SetKubeconfig sets the kubeconfig in the current profile. All further actions which require kubernetes will be targeted at this kubeconfig
  55. func SetKubeconfig(kubeconfig string) error {
  56. path, err := filepath.Abs(kubeconfig)
  57. if err != nil {
  58. return err
  59. }
  60. if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
  61. return fmt.Errorf("%s does not exist", path)
  62. }
  63. color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", path) //nolint:errcheck,gosec
  64. return updateValuesForSelectedProfile(currentProfile, porterConfigFilePath, withKubeconfig(kubeconfig))
  65. }
  66. // cliConfigValue allows for overridden CLI config values allowing for default values, without requiring nil.
  67. type cliConfigValue func(*CLIConfig)
  68. func withClusterID(clusterID uint) cliConfigValue {
  69. return func(c *CLIConfig) {
  70. c.Cluster = clusterID
  71. }
  72. }
  73. func withToken(token string) cliConfigValue {
  74. return func(c *CLIConfig) {
  75. c.Token = token
  76. }
  77. }
  78. func withRegistryID(registryID uint) cliConfigValue {
  79. return func(c *CLIConfig) {
  80. c.Registry = registryID
  81. }
  82. }
  83. func withHelmRepoID(helmRepoID uint) cliConfigValue {
  84. return func(c *CLIConfig) {
  85. c.HelmRepo = helmRepoID
  86. }
  87. }
  88. func withKubeconfig(kubeconfig string) cliConfigValue {
  89. return func(c *CLIConfig) {
  90. c.Kubeconfig = kubeconfig
  91. }
  92. }
  93. func withHost(host string) cliConfigValue {
  94. return func(c *CLIConfig) {
  95. c.Host = host
  96. }
  97. }
  98. func withProjectID(projectID uint) cliConfigValue {
  99. return func(c *CLIConfig) {
  100. c.Project = projectID
  101. }
  102. }
  103. func withDriver(driver string) cliConfigValue {
  104. return func(c *CLIConfig) {
  105. c.Driver = driver
  106. }
  107. }
  108. // withCLIConfig will completely override the CLI config with the provided one
  109. func withCLIConfig(newConfig CLIConfig) cliConfigValue {
  110. return func(c *CLIConfig) {
  111. c = &newConfig
  112. }
  113. }