config.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package cmd
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "github.com/fatih/color"
  9. "github.com/spf13/cobra"
  10. "github.com/spf13/viper"
  11. )
  12. // a set of shared flags
  13. var (
  14. driver string
  15. host string
  16. projectID uint
  17. registryID uint
  18. clusterID uint
  19. helmRepoID uint
  20. )
  21. var configCmd = &cobra.Command{
  22. Use: "config",
  23. Short: "Commands that control local configuration settings",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. if err := printConfig(); err != nil {
  26. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  27. os.Exit(1)
  28. }
  29. },
  30. }
  31. var setProjectCmd = &cobra.Command{
  32. Use: "set-project [id]",
  33. Args: cobra.ExactArgs(1),
  34. Short: "Saves the project id in the default configuration",
  35. Run: func(cmd *cobra.Command, args []string) {
  36. projID, err := strconv.ParseUint(args[0], 10, 64)
  37. if err != nil {
  38. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  39. os.Exit(1)
  40. }
  41. err = setProject(uint(projID))
  42. if err != nil {
  43. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  44. os.Exit(1)
  45. }
  46. },
  47. }
  48. var setClusterCmd = &cobra.Command{
  49. Use: "set-cluster [id]",
  50. Args: cobra.ExactArgs(1),
  51. Short: "Saves the cluster id in the default configuration",
  52. Run: func(cmd *cobra.Command, args []string) {
  53. clusterID, err := strconv.ParseUint(args[0], 10, 64)
  54. if err != nil {
  55. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  56. os.Exit(1)
  57. }
  58. err = setCluster(uint(clusterID))
  59. if err != nil {
  60. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  61. os.Exit(1)
  62. }
  63. },
  64. }
  65. var setRegistryCmd = &cobra.Command{
  66. Use: "set-registry [id]",
  67. Args: cobra.ExactArgs(1),
  68. Short: "Saves the registry id in the default configuration",
  69. Run: func(cmd *cobra.Command, args []string) {
  70. registryID, err := strconv.ParseUint(args[0], 10, 64)
  71. if err != nil {
  72. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  73. os.Exit(1)
  74. }
  75. err = setRegistry(uint(registryID))
  76. if err != nil {
  77. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  78. os.Exit(1)
  79. }
  80. },
  81. }
  82. var setHelmRepoCmd = &cobra.Command{
  83. Use: "set-helmrepo [id]",
  84. Args: cobra.ExactArgs(1),
  85. Short: "Saves the helm repo id in the default configuration",
  86. Run: func(cmd *cobra.Command, args []string) {
  87. hrID, err := strconv.ParseUint(args[0], 10, 64)
  88. if err != nil {
  89. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  90. os.Exit(1)
  91. }
  92. err = setHelmRepo(uint(hrID))
  93. if err != nil {
  94. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  95. os.Exit(1)
  96. }
  97. },
  98. }
  99. var setHostCmd = &cobra.Command{
  100. Use: "set-host [host]",
  101. Args: cobra.ExactArgs(1),
  102. Short: "Saves the host in the default configuration",
  103. Run: func(cmd *cobra.Command, args []string) {
  104. err := setHost(args[0])
  105. if err != nil {
  106. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  107. os.Exit(1)
  108. }
  109. },
  110. }
  111. func init() {
  112. rootCmd.AddCommand(configCmd)
  113. configCmd.AddCommand(setProjectCmd)
  114. configCmd.AddCommand(setClusterCmd)
  115. configCmd.AddCommand(setHostCmd)
  116. configCmd.AddCommand(setRegistryCmd)
  117. configCmd.AddCommand(setHelmRepoCmd)
  118. }
  119. func setDriver(driver string) error {
  120. viper.Set("driver", driver)
  121. err := viper.WriteConfig()
  122. color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
  123. return err
  124. }
  125. func getDriver() string {
  126. if driver != "" {
  127. return driver
  128. }
  129. if opts.driver != "" {
  130. return opts.driver
  131. }
  132. return viper.GetString("driver")
  133. }
  134. func printConfig() error {
  135. config, err := ioutil.ReadFile(filepath.Join(home, ".porter", "porter.yaml"))
  136. if err != nil {
  137. return err
  138. }
  139. fmt.Printf(string(config))
  140. return nil
  141. }
  142. func setProject(id uint) error {
  143. viper.Set("project", id)
  144. color.New(color.FgGreen).Printf("Set the current project id as %d\n", id)
  145. return viper.WriteConfig()
  146. }
  147. func setCluster(id uint) error {
  148. viper.Set("cluster", id)
  149. color.New(color.FgGreen).Printf("Set the current cluster id as %d\n", id)
  150. return viper.WriteConfig()
  151. }
  152. func setRegistry(id uint) error {
  153. viper.Set("registry", id)
  154. color.New(color.FgGreen).Printf("Set the current registry id as %d\n", id)
  155. return viper.WriteConfig()
  156. }
  157. func setHelmRepo(id uint) error {
  158. viper.Set("helm_repo", id)
  159. color.New(color.FgGreen).Printf("Set the current helm repo id as %d\n", id)
  160. return viper.WriteConfig()
  161. }
  162. func setHost(host string) error {
  163. viper.Set("host", host)
  164. err := viper.WriteConfig()
  165. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  166. return err
  167. }
  168. func setToken(token string) error {
  169. viper.Set("token", token)
  170. err := viper.WriteConfig()
  171. return err
  172. }
  173. func getHost() string {
  174. if host != "" {
  175. return host
  176. }
  177. return viper.GetString("host")
  178. }
  179. func getToken() string {
  180. return viper.GetString("token")
  181. }
  182. func getClusterID() uint {
  183. if clusterID != 0 {
  184. return clusterID
  185. }
  186. return viper.GetUint("cluster")
  187. }
  188. func getRegistryID() uint {
  189. if registryID != 0 {
  190. return registryID
  191. }
  192. return viper.GetUint("registry")
  193. }
  194. func getHelmRepoID() uint {
  195. if helmRepoID != 0 {
  196. return helmRepoID
  197. }
  198. return viper.GetUint("helm_repo")
  199. }
  200. func getProjectID() uint {
  201. if projectID != 0 {
  202. return projectID
  203. }
  204. return viper.GetUint("project")
  205. }