1
0

feat: add show command

This commit is contained in:
Wiktor Zykubek 2025-01-04 15:28:22 +01:00
parent dca641bfa1
commit efd0019d12
Signed by: wzykubek
GPG Key ID: 2221881F957D89B9
3 changed files with 63 additions and 0 deletions

60
cmd/show.go Normal file
View File

@ -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,
)
},
}

1
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=