config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. clusterID uint
  15. )
  16. var configCmd = &cobra.Command{
  17. Use: "config",
  18. Short: "Commands that control local configuration settings",
  19. }
  20. var setProjectCmd = &cobra.Command{
  21. Use: "set-project [id]",
  22. Args: cobra.ExactArgs(1),
  23. Short: "Saves the project id in the default configuration",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. projID, err := strconv.ParseUint(args[0], 10, 64)
  26. if err != nil {
  27. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  28. os.Exit(1)
  29. }
  30. err = setProject(uint(projID))
  31. if err != nil {
  32. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  33. os.Exit(1)
  34. }
  35. },
  36. }
  37. var setClusterCmd = &cobra.Command{
  38. Use: "set-cluster [id]",
  39. Args: cobra.ExactArgs(1),
  40. Short: "Saves the cluster id in the default configuration",
  41. Run: func(cmd *cobra.Command, args []string) {
  42. clusterID, err := strconv.ParseUint(args[0], 10, 64)
  43. if err != nil {
  44. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  45. os.Exit(1)
  46. }
  47. err = setCluster(uint(clusterID))
  48. if err != nil {
  49. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  50. os.Exit(1)
  51. }
  52. },
  53. }
  54. var setHostCmd = &cobra.Command{
  55. Use: "set-host [host]",
  56. Args: cobra.ExactArgs(1),
  57. Short: "Saves the host in the default configuration",
  58. Run: func(cmd *cobra.Command, args []string) {
  59. err := setHost(args[0])
  60. if err != nil {
  61. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  62. os.Exit(1)
  63. }
  64. },
  65. }
  66. func init() {
  67. rootCmd.AddCommand(configCmd)
  68. configCmd.AddCommand(setProjectCmd)
  69. configCmd.AddCommand(setClusterCmd)
  70. configCmd.AddCommand(setHostCmd)
  71. }
  72. func setDriver(driver string) error {
  73. viper.Set("driver", driver)
  74. err := viper.WriteConfig()
  75. color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
  76. return err
  77. }
  78. func getDriver() string {
  79. if driver != "" {
  80. return driver
  81. }
  82. return viper.GetString("driver")
  83. }
  84. func setProject(id uint) error {
  85. viper.Set("project", id)
  86. color.New(color.FgGreen).Printf("Set the current project id as %d\n", id)
  87. return viper.WriteConfig()
  88. }
  89. func setCluster(id uint) error {
  90. viper.Set("cluster", id)
  91. color.New(color.FgGreen).Printf("Set the current cluster id as %d\n", id)
  92. return viper.WriteConfig()
  93. }
  94. func setHost(host string) error {
  95. viper.Set("host", host)
  96. err := viper.WriteConfig()
  97. color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
  98. return err
  99. }
  100. func getHost() string {
  101. if host != "" {
  102. return host
  103. }
  104. return viper.GetString("host")
  105. }
  106. func getClusterID() uint {
  107. if clusterID != 0 {
  108. return clusterID
  109. }
  110. return viper.GetUint("cluster")
  111. }
  112. func getProjectID() uint {
  113. if projectID != 0 {
  114. return projectID
  115. }
  116. return viper.GetUint("project")
  117. }