| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package cmd
- import (
- "fmt"
- "os"
- "strconv"
- "github.com/spf13/cobra"
- "github.com/spf13/viper"
- )
- // a set of shared flags
- var (
- host string
- projectID uint
- )
- var configCmd = &cobra.Command{
- Use: "config",
- Short: "Commands that control local configuration settings",
- }
- var setProjectCmd = &cobra.Command{
- Use: "set-project [id]",
- Args: cobra.ExactArgs(1),
- Short: "Saves the project id in the default configuration",
- Run: func(cmd *cobra.Command, args []string) {
- projID, err := strconv.ParseUint(args[0], 10, 64)
- if err != nil {
- fmt.Printf("An error occurred: %v\n", err)
- os.Exit(1)
- }
- err = setProject(uint(projID))
- if err != nil {
- fmt.Printf("An error occurred: %v\n", err)
- os.Exit(1)
- }
- },
- }
- var setHostCmd = &cobra.Command{
- Use: "set-host [host]",
- Args: cobra.ExactArgs(1),
- Short: "Saves the host in the default configuration",
- Run: func(cmd *cobra.Command, args []string) {
- err := setHost(args[0])
- if err != nil {
- fmt.Printf("An error occurred: %v\n", err)
- os.Exit(1)
- }
- },
- }
- func init() {
- rootCmd.AddCommand(configCmd)
- configCmd.AddCommand(setProjectCmd)
- configCmd.AddCommand(setHostCmd)
- }
- func setProject(id uint) error {
- viper.Set("project", id)
- fmt.Printf("Set the current project id as %d\n", id)
- return viper.WriteConfig()
- }
- func setHost(host string) error {
- viper.Set("host", host)
- fmt.Printf("Set the current host as %s\n", host)
- return viper.WriteConfig()
- }
- func getHost() string {
- if host != "" {
- return host
- }
- return viper.GetString("host")
- }
- func getProjectID() uint {
- if projectID != 0 {
- return projectID
- }
- return viper.GetUint("project")
- }
|