2
0

update.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. v := CLIConfig{
  22. Driver: driver,
  23. }
  24. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  25. }
  26. // SetHost updates the host for the current profile. This clears the project, cluster, and token as they will not work with the new host
  27. // Trailing slashes will be dropped from the provided host as they may cause issues with the api server
  28. func SetHost(host string, currentProfile string) error {
  29. host = strings.TrimRight(host, "/")
  30. v := defaultCLIConfig()
  31. v.Host = host
  32. color.New(color.FgGreen).Printf("Set the current host as %s\n", host) //nolint:errcheck,gosec
  33. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  34. }
  35. // SetProject sets a project for all API commands
  36. func SetProject(projectID uint, currentProfile string) error {
  37. color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID) //nolint:errcheck,gosec
  38. v := CLIConfig{
  39. Project: projectID,
  40. }
  41. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  42. }
  43. // SetCluster sets the cluster in the current profile. All further actions will be targeted at this cluster
  44. func SetCluster(clusterID uint, currentProfile string) error {
  45. color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID) //nolint:errcheck,gosec
  46. v := CLIConfig{
  47. Cluster: clusterID,
  48. Kubeconfig: "",
  49. }
  50. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  51. }
  52. // SetToken sets the token in the current profile. All further actions will be authenticated with this token
  53. func SetToken(token string, currentProfile string) error {
  54. v := CLIConfig{
  55. Token: token,
  56. }
  57. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  58. }
  59. // SetRegistry sets the docker registry in the current profile. All further actions will be targeted at this registry
  60. func SetRegistry(registryID uint) error {
  61. color.New(color.FgGreen).Printf("Set the current registry as %d\n", registryID) //nolint:errcheck,gosec
  62. v := CLIConfig{
  63. Registry: registryID,
  64. }
  65. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  66. }
  67. // SetHelmRepo sets the helm repo in the current profile. All further actions will be targeted at this helm repo
  68. func SetHelmRepo(helmRepoID uint) error {
  69. color.New(color.FgGreen).Printf("Set the current Helm repo as %d\n", helmRepoID) //nolint:errcheck,gosec
  70. v := CLIConfig{
  71. HelmRepo: helmRepoID,
  72. }
  73. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  74. }
  75. // SetKubeconfig sets the kubeconfig in the current profile. All further actions which require kubernetes will be targeted at this kubeconfig
  76. func SetKubeconfig(kubeconfig string) error {
  77. path, err := filepath.Abs(kubeconfig)
  78. if err != nil {
  79. return err
  80. }
  81. if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
  82. return fmt.Errorf("%s does not exist", path)
  83. }
  84. color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", path) //nolint:errcheck,gosec
  85. v := CLIConfig{
  86. Kubeconfig: kubeconfig,
  87. }
  88. return updateValuesForSelectedProfile(currentProfile, v, porterConfigFilePath)
  89. }