config.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. "github.com/spf13/viper"
  6. )
  7. // Define the 'config' command and its subcommands
  8. var configCmd = &cobra.Command{
  9. Use: "config",
  10. Short: "Manage configuration settings",
  11. }
  12. // Subcommand for 'config show'
  13. var showCmd = &cobra.Command{
  14. Use: "show",
  15. Short: "Show configuration settings",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. fmt.Println("host:", viper.GetString("host"))
  18. },
  19. }
  20. // Subcommand for 'config update'
  21. var updateCmd = &cobra.Command{
  22. Use: "update",
  23. Short: "Update a configuration setting",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. viper.Set("host", host)
  26. err := viper.WriteConfig()
  27. if err != nil {
  28. fmt.Println("Error updating 'host' configuration:", err)
  29. return
  30. }
  31. fmt.Println("Updating 'host' configuration:", host)
  32. },
  33. }
  34. // Flag to specify the 'host' configuration value
  35. var host string
  36. func init() {
  37. // Set up Viper configuration
  38. // Set a default value for 'host' configuration
  39. viper.SetDefault("host", "https://api.ente.io")
  40. // Add 'config' subcommands to the root command
  41. //rootCmd.AddCommand(configCmd)
  42. // Add flags to the 'config store' and 'config update' subcommands
  43. updateCmd.Flags().StringVarP(&host, "host", "H", viper.GetString("host"), "Update the 'host' configuration")
  44. // Mark 'host' flag as required for the 'update' command
  45. updateCmd.MarkFlagRequired("host")
  46. // Add 'config' subcommands to the 'config' command
  47. configCmd.AddCommand(showCmd, updateCmd)
  48. }