config.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. )
  20. var configCmd = &cobra.Command{
  21. Use: "config",
  22. Short: "Commands that control local configuration settings",
  23. Run: func(cmd *cobra.Command, args []string) {
  24. if err := printConfig(); err != nil {
  25. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  26. os.Exit(1)
  27. }
  28. },
  29. }
  30. var setProjectCmd = &cobra.Command{
  31. Use: "set-project [id]",
  32. Args: cobra.ExactArgs(1),
  33. Short: "Saves the project id in the default configuration",
  34. Run: func(cmd *cobra.Command, args []string) {
  35. projID, err := strconv.ParseUint(args[0], 10, 64)
  36. if err != nil {
  37. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  38. os.Exit(1)
  39. }
  40. err = setProject(uint(projID))
  41. if err != nil {
  42. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. var setClusterCmd = &cobra.Command{
  48. Use: "set-cluster [id]",
  49. Args: cobra.ExactArgs(1),
  50. Short: "Saves the cluster id in the default configuration",
  51. Run: func(cmd *cobra.Command, args []string) {
  52. clusterID, err := strconv.ParseUint(args[0], 10, 64)
  53. if err != nil {
  54. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  55. os.Exit(1)
  56. }
  57. err = setCluster(uint(clusterID))
  58. if err != nil {
  59. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  60. os.Exit(1)
  61. }
  62. },
  63. }
  64. var setRegistryCmd = &cobra.Command{
  65. Use: "set-registry [id]",
  66. Args: cobra.ExactArgs(1),
  67. Short: "Saves the registry id in the default configuration",
  68. Run: func(cmd *cobra.Command, args []string) {
  69. registryID, err := strconv.ParseUint(args[0], 10, 64)
  70. if err != nil {
  71. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  72. os.Exit(1)
  73. }
  74. err = setRegistry(uint(registryID))
  75. if err != nil {
  76. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  77. os.Exit(1)
  78. }
  79. },
  80. }
  81. var setHostCmd = &cobra.Command{
  82. Use: "set-host [host]",
  83. Args: cobra.ExactArgs(1),
  84. Short: "Saves the host in the default configuration",
  85. Run: func(cmd *cobra.Command, args []string) {
  86. err := setHost(args[0])
  87. if err != nil {
  88. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  89. os.Exit(1)
  90. }
  91. },
  92. }
  93. func init() {
  94. rootCmd.AddCommand(configCmd)
  95. configCmd.AddCommand(setProjectCmd)
  96. configCmd.AddCommand(setClusterCmd)
  97. configCmd.AddCommand(setHostCmd)
  98. configCmd.AddCommand(setRegistryCmd)
  99. }
  100. func setDriver(driver string) error {
  101. viper.Set("driver", driver)
  102. err := viper.WriteConfig()
  103. color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
  104. return err
  105. }
  106. func getDriver() string {
  107. if driver != "" {
  108. return driver
  109. }
  110. if opts.driver != "" {
  111. return opts.driver
  112. }
  113. return viper.GetString("driver")
  114. }
  115. func printConfig() error {
  116. config, err := ioutil.ReadFile(filepath.Join(home, ".porter", "porter.yaml"))
  117. if err != nil {
  118. return err
  119. }
  120. fmt.Printf(string(config))
  121. return nil
  122. }
  123. func setProject(id uint) error {
  124. viper.Set("project", id)
  125. color.New(color.FgGreen).Printf("Set the current project id as %d\n", id)
  126. return viper.WriteConfig()
  127. }
  128. func setCluster(id uint) error {
  129. viper.Set("cluster", id)
  130. color.New(color.FgGreen).Printf("Set the current cluster id as %d\n", id)
  131. return viper.WriteConfig()
  132. }
  133. func setRegistry(id uint) error {
  134. viper.Set("registry", id)
  135. color.New(color.FgGreen).Printf("Set the current registry id as %d\n", id)
  136. return viper.WriteConfig()
  137. }
  138. func setHost(host string) error {
  139. viper.Set("host", host)
  140. err := viper.WriteConfig()
  141. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  142. return err
  143. }
  144. func getHost() string {
  145. if host != "" {
  146. return host
  147. }
  148. return viper.GetString("host")
  149. }
  150. func getClusterID() uint {
  151. if clusterID != 0 {
  152. return clusterID
  153. }
  154. return viper.GetUint("cluster")
  155. }
  156. func getRegistryID() uint {
  157. if registryID != 0 {
  158. return registryID
  159. }
  160. return viper.GetUint("registry")
  161. }
  162. func getProjectID() uint {
  163. if projectID != 0 {
  164. return projectID
  165. }
  166. return viper.GetUint("project")
  167. }