From efd0019d122c3607d16775b550bad635d3881edc Mon Sep 17 00:00:00 2001 From: Wiktor Zykubek Date: Sat, 4 Jan 2025 15:28:22 +0100 Subject: [PATCH] feat: add show command --- cmd/show.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 63 insertions(+) create mode 100644 cmd/show.go diff --git a/cmd/show.go b/cmd/show.go new file mode 100644 index 0000000..1629e5f --- /dev/null +++ b/cmd/show.go @@ -0,0 +1,60 @@ +package cmd + +import ( + "fmt" + "os" + "strings" + + t "go.wzykubek.xyz/licensmith/internal/template" + + "github.com/spf13/cobra" + "golang.org/x/text/cases" + "golang.org/x/text/language" +) + +func init() { + rootCmd.AddCommand(showCmd) +} + +func parseValueList(arr []string) string { + titleCaser := cases.Title(language.English) + var o string + + for k, v := range arr { + v = strings.ReplaceAll(v, "-", " ") + v = titleCaser.String(v) + o = o + "- " + v + if k < len(arr)-1 { + o = o + "\n" + } + } + + return o +} + +var showCmd = &cobra.Command{ + Use: "show [license id]", + Short: "Show license details", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + licenseID := args[0] + tmpl, err := t.Parse(licenseID + ".tmpl") + if err != nil { + fmt.Printf("Error: There is no '%s' license\n", licenseID) + os.Exit(2) + } + + permissions := parseValueList(tmpl.Permissions) + conditions := parseValueList(tmpl.Conditions) + limitations := parseValueList(tmpl.Limitations) + fmt.Printf( + "%s (%s)\n\n%s\n\nPermissions:\n%s\n\nConditions:\n%s\n\nLimitations:\n%s\n", + tmpl.Title, + tmpl.ID, + tmpl.Description, + permissions, + conditions, + limitations, + ) + }, +} diff --git a/go.mod b/go.mod index 8704a88..364bbc7 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.4 require ( github.com/spf13/cobra v1.8.1 + golang.org/x/text v0.21.0 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index a01295b..2096219 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=