From 5b0e465dc593f61b4574afc22deb8b541f0e1c69 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Mon, 27 Feb 2023 23:04:05 -0800 Subject: [PATCH] simplify --- cmd/root.go | 46 ++++++++++++++++++++++++++++---- cmd/temp.go | 77 ----------------------------------------------------- 2 files changed, 41 insertions(+), 82 deletions(-) delete mode 100644 cmd/temp.go diff --git a/cmd/root.go b/cmd/root.go index 372e061..389c6f8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,21 +1,52 @@ package cmd import ( + "fmt" "os" + "strconv" + u "github.com/bcicen/go-units" "github.com/spf13/cobra" ) -var rootCmd = &cobra.Command{ - Use: "converter", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains +var ( + from string + to string + rootCmd = &cobra.Command{ + Use: "converter", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains examples and usage of using your application. For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, -} + Run: func(cmd *cobra.Command, args []string) { + in, err := strconv.ParseFloat(args[0], 64) + if err != nil { + fmt.Println(err.Error()) + return + } + fromVal, err := u.Find(cmd.Flag("from").Value.String()) + if err != nil { + fmt.Println(err.Error()) + return + } + toVal, err := u.Find(cmd.Flag("to").Value.String()) + if err != nil { + fmt.Println(err.Error()) + return + } + val := u.NewValue(in, fromVal) + out, err := val.Convert(toVal) + if err != nil { + fmt.Println(err.Error()) + return + } + fmt.Println(out.String()) + }, + } +) func Execute() { err := rootCmd.Execute() @@ -23,3 +54,8 @@ func Execute() { os.Exit(1) } } + +func init() { + rootCmd.PersistentFlags().StringVarP(&from, "from", "f", "c", "unit system to convert from") + rootCmd.PersistentFlags().StringVarP(&to, "to", "t", "f", "unit system to convert to") +} diff --git a/cmd/temp.go b/cmd/temp.go deleted file mode 100644 index aa19eee..0000000 --- a/cmd/temp.go +++ /dev/null @@ -1,77 +0,0 @@ -package cmd - -import ( - "fmt" - "strconv" - "strings" - - u "github.com/bcicen/go-units" - "github.com/spf13/cobra" -) - -var ( - from string - to string - tempCmd = &cobra.Command{ - Use: "temperature", - Aliases: []string{"temp", "t", "temps"}, - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Args: cobra.MatchAll(cobra.ExactArgs(1)), - RunE: func(cmd *cobra.Command, args []string) error { - err := cmd.ParseFlags(args) - if err != nil { - return err - } - in, err := strconv.ParseFloat(args[0], 64) - if err != nil { - return err - } - fromFlag := strings.ToLower(cmd.Flag("from").Value.String()) - toFlag := strings.ToLower(cmd.Flag("to").Value.String()) - var out u.Value - switch fromFlag { - case "f", "farenheit": - out, err = Convert(in, toFlag, u.Fahrenheit) - if err != nil { - return err - } - case "c", "celsius": - out, err = Convert(in, toFlag, u.Celsius) - if err != nil { - return err - } - case "k", "kelvin": - out, err = Convert(in, toFlag, u.Kelvin) - if err != nil { - return err - } - } - fmt.Println(out.String()) - return nil - }, - } -) - -func Convert(in float64, to string, from u.Unit) (u.Value, error) { - switch to { - case "f", "farenheit": - return u.ConvertFloat(in, from, u.Fahrenheit) - case "c", "celsius": - return u.ConvertFloat(in, from, u.Celsius) - case "k", "kelvin": - return u.ConvertFloat(in, from, u.Kelvin) - } - return u.Value{}, fmt.Errorf("Invalid flags provided") -} - -func init() { - rootCmd.AddCommand(tempCmd) - tempCmd.PersistentFlags().StringVarP(&from, "from", "f", "c", "unit system to convert from, (farenheit, celsius, kelvin)") - tempCmd.PersistentFlags().StringVarP(&to, "to", "t", "f", "unit system to convert to, (farenheit, celsius, kelvin)") -}