converter/cmd/root.go

58 lines
1.2 KiB
Go
Raw Normal View History

2023-02-28 06:12:00 +00:00
package cmd
import (
2023-02-28 07:04:05 +00:00
"fmt"
2023-02-28 06:12:00 +00:00
"os"
2023-02-28 07:04:05 +00:00
"strconv"
2023-02-28 06:12:00 +00:00
2023-02-28 07:04:05 +00:00
u "github.com/bcicen/go-units"
2023-02-28 06:12:00 +00:00
"github.com/spf13/cobra"
)
2023-02-28 07:04:05 +00:00
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
2023-02-28 06:12:00 +00:00
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.`,
2023-02-28 07:08:17 +00:00
Args: cobra.MatchAll(cobra.ExactArgs(3)),
2023-02-28 07:04:05 +00:00
Run: func(cmd *cobra.Command, args []string) {
2023-02-28 07:08:17 +00:00
in, err := strconv.ParseFloat(args[2], 64)
2023-02-28 07:04:05 +00:00
if err != nil {
fmt.Println(err.Error())
return
}
2023-02-28 07:08:17 +00:00
fromVal, err := u.Find(args[0])
2023-02-28 07:04:05 +00:00
if err != nil {
fmt.Println(err.Error())
return
}
2023-02-28 07:08:17 +00:00
toVal, err := u.Find(args[1])
2023-02-28 07:04:05 +00:00
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())
},
}
)
2023-02-28 06:12:00 +00:00
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}