config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package cmd
  2. import (
  3. "os"
  4. "strconv"
  5. "github.com/fatih/color"
  6. "github.com/spf13/cobra"
  7. "github.com/spf13/viper"
  8. )
  9. // a set of shared flags
  10. var (
  11. driver string
  12. host string
  13. projectID uint
  14. registryID uint
  15. clusterID uint
  16. )
  17. var configCmd = &cobra.Command{
  18. Use: "config",
  19. Short: "Commands that control local configuration settings",
  20. }
  21. var setProjectCmd = &cobra.Command{
  22. Use: "set-project [id]",
  23. Args: cobra.ExactArgs(1),
  24. Short: "Saves the project id in the default configuration",
  25. Run: func(cmd *cobra.Command, args []string) {
  26. projID, err := strconv.ParseUint(args[0], 10, 64)
  27. if err != nil {
  28. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  29. os.Exit(1)
  30. }
  31. err = setProject(uint(projID))
  32. if err != nil {
  33. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  34. os.Exit(1)
  35. }
  36. },
  37. }
  38. var setClusterCmd = &cobra.Command{
  39. Use: "set-cluster [id]",
  40. Args: cobra.ExactArgs(1),
  41. Short: "Saves the cluster id in the default configuration",
  42. Run: func(cmd *cobra.Command, args []string) {
  43. clusterID, err := strconv.ParseUint(args[0], 10, 64)
  44. if err != nil {
  45. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  46. os.Exit(1)
  47. }
  48. err = setCluster(uint(clusterID))
  49. if err != nil {
  50. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  51. os.Exit(1)
  52. }
  53. },
  54. }
  55. var setRegistryCmd = &cobra.Command{
  56. Use: "set-registry [id]",
  57. Args: cobra.ExactArgs(1),
  58. Short: "Saves the registry id in the default configuration",
  59. Run: func(cmd *cobra.Command, args []string) {
  60. registryID, err := strconv.ParseUint(args[0], 10, 64)
  61. if err != nil {
  62. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  63. os.Exit(1)
  64. }
  65. err = setRegistry(uint(registryID))
  66. if err != nil {
  67. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  68. os.Exit(1)
  69. }
  70. },
  71. }
  72. var setHostCmd = &cobra.Command{
  73. Use: "set-host [host]",
  74. Args: cobra.ExactArgs(1),
  75. Short: "Saves the host in the default configuration",
  76. Run: func(cmd *cobra.Command, args []string) {
  77. err := setHost(args[0])
  78. if err != nil {
  79. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  80. os.Exit(1)
  81. }
  82. },
  83. }
  84. func init() {
  85. rootCmd.AddCommand(configCmd)
  86. configCmd.AddCommand(setProjectCmd)
  87. configCmd.AddCommand(setClusterCmd)
  88. configCmd.AddCommand(setHostCmd)
  89. configCmd.AddCommand(setRegistryCmd)
  90. }
  91. func setDriver(driver string) error {
  92. viper.Set("driver", driver)
  93. err := viper.WriteConfig()
  94. color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
  95. return err
  96. }
  97. func getDriver() string {
  98. if driver != "" {
  99. return driver
  100. }
  101. if opts.driver != "" {
  102. return opts.driver
  103. }
  104. return viper.GetString("driver")
  105. }
  106. func setProject(id uint) error {
  107. viper.Set("project", id)
  108. color.New(color.FgGreen).Printf("Set the current project id as %d\n", id)
  109. return viper.WriteConfig()
  110. }
  111. func setCluster(id uint) error {
  112. viper.Set("cluster", id)
  113. color.New(color.FgGreen).Printf("Set the current cluster id as %d\n", id)
  114. return viper.WriteConfig()
  115. }
  116. func setRegistry(id uint) error {
  117. viper.Set("registry", id)
  118. color.New(color.FgGreen).Printf("Set the current registry id as %d\n", id)
  119. return viper.WriteConfig()
  120. }
  121. func setHost(host string) error {
  122. viper.Set("host", host)
  123. err := viper.WriteConfig()
  124. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  125. return err
  126. }
  127. func getHost() string {
  128. if host != "" {
  129. return host
  130. }
  131. return viper.GetString("host")
  132. }
  133. func getClusterID() uint {
  134. if clusterID != 0 {
  135. return clusterID
  136. }
  137. return viper.GetUint("cluster")
  138. }
  139. func getRegistryID() uint {
  140. if registryID != 0 {
  141. return registryID
  142. }
  143. return viper.GetUint("registry")
  144. }
  145. func getProjectID() uint {
  146. if projectID != 0 {
  147. return projectID
  148. }
  149. return viper.GetUint("project")
  150. }