config.go 2.6 KB

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