This commit is contained in:
abs3nt 2023-02-27 22:32:34 -08:00
parent 108d45fb0f
commit bb79eb262c
4 changed files with 36 additions and 82 deletions

View File

@ -1,7 +1,3 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd package cmd
import ( import (
@ -10,9 +6,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "converter", Use: "converter",
Short: "A brief description of your application", Short: "A brief description of your application",
@ -22,30 +15,11 @@ examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications. Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files This application is a tool to generate the needed files
to quickly create a Cobra application.`, to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
} }
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
err := rootCmd.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
} }
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.converter.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -1,6 +1,3 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd package cmd
import ( import (
@ -8,10 +5,10 @@ import (
"strconv" "strconv"
"strings" "strings"
u "github.com/bcicen/go-units"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// tempCmd represents the temp command
var ( var (
from string from string
to string to string
@ -34,76 +31,50 @@ to quickly create a Cobra application.`,
if err != nil { if err != nil {
return err return err
} }
in, err := strconv.ParseFloat(args[0], 64) in, err := strconv.ParseFloat(args[0], 64)
if err != nil { if err != nil {
return err return err
} }
var out float64 fromFlag := strings.ToLower(cmd.Flag("from").Value.String())
switch strings.ToLower(cmd.Flag("from").Value.String()) { toFlag := strings.ToLower(cmd.Flag("to").Value.String())
var out u.Value
switch fromFlag {
case "f", "farenheit": case "f", "farenheit":
switch strings.ToLower(cmd.Flag("to").Value.String()) { out, err = Convert(in, toFlag, u.Fahrenheit)
case "f", "farenheit": if err != nil {
out = in return err
case "c", "celsius":
out = ((in - 32) * five_over_nine)
case "k", "kelvin":
out = ((in-32)*five_over_nine + kconst)
case "r", "rankine":
out = in + rconst
} }
case "c", "celsius": case "c", "celsius":
switch strings.ToLower(cmd.Flag("to").Value.String()) { out, err = Convert(in, toFlag, u.Celsius)
case "f", "farenheit": if err != nil {
out = in*nine_over_five + 32 return err
case "c", "celsius":
out = in
case "k", "kelvin":
out = in + kconst
case "r", "rankine":
out = in*nine_over_five + rconst + 32
} }
case "k", "kelvin": case "k", "kelvin":
switch strings.ToLower(cmd.Flag("to").Value.String()) { out, err = Convert(in, toFlag, u.Kelvin)
case "f", "farenheit": if err != nil {
out = (in-kconst)*nine_over_five + 32 return err
case "c", "celsius":
out = in - kconst
case "k", "kelvin":
out = in
case "r", "rankine":
out = in * nine_over_five
}
case "r", "rankine":
switch strings.ToLower(cmd.Flag("to").Value.String()) {
case "f", "farenheit":
out = in - rconst
case "c", "celsius":
out = (in - (rconst + 32)) * nine_over_five
case "k", "kelvin":
out = in * five_over_nine
case "r", "rankine":
out = in
} }
} }
fmt.Println(strconv.FormatFloat(out, 'f', 2, 64)) fmt.Println(out.String())
return nil 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() { func init() {
rootCmd.AddCommand(tempCmd) rootCmd.AddCommand(tempCmd)
tempCmd.PersistentFlags().StringVarP(&from, "from", "f", "c", "unit system to convert from, (farenheit, celsius, kelvin)") 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)") tempCmd.PersistentFlags().StringVarP(&to, "to", "t", "f", "unit system to convert to, (farenheit, celsius, kelvin)")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// tempCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// tempCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }

3
go.mod
View File

@ -3,6 +3,9 @@ module gitea.asdf.cafe/abs3nt/converter
go 1.20 go 1.20
require ( require (
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/bcicen/bfstree v1.0.0 // indirect
github.com/bcicen/go-units v1.0.4 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/spf13/cobra v1.6.1 // indirect github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect

6
go.sum
View File

@ -1,3 +1,9 @@
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/bcicen/bfstree v1.0.0 h1:Fx9vcyXYspj2GIJqAvd1lwCNI+cQF/r2JJqxHHmsAO0=
github.com/bcicen/bfstree v1.0.0/go.mod h1:u//juIip96SNFkG4iMn9z0KzqLSeFSpBKoBo5ceq1uE=
github.com/bcicen/go-units v1.0.4 h1:ZSllGgj9jFa9n3EikJqkeZr5I0qDlnCTutGHfPhGv2M=
github.com/bcicen/go-units v1.0.4/go.mod h1:c7/sSz9cc6XvnrjsyNwoKHqN6KDDf8LME5vSf+U5Y08=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=