1
0

feat: add filtering flags to show command

--title
--description
--permissions
--conditions
--limitations
This commit is contained in:
Wiktor Zykubek 2025-01-04 19:10:26 +01:00
parent efd0019d12
commit 090d3a490d
Signed by: wzykubek
GPG Key ID: 2221881F957D89B9

View File

@ -12,8 +12,19 @@ import (
"golang.org/x/text/language" "golang.org/x/text/language"
) )
var printTitle bool
var printDescription bool
var printPermissions bool
var printConditions bool
var printLimitations bool
func init() { func init() {
rootCmd.AddCommand(showCmd) rootCmd.AddCommand(showCmd)
showCmd.Flags().BoolVar(&printTitle, "title", false, "Print title")
showCmd.Flags().BoolVar(&printDescription, "description", false, "Print description")
showCmd.Flags().BoolVar(&printPermissions, "permissions", false, "Print permissions")
showCmd.Flags().BoolVar(&printConditions, "conditions", false, "Print conditions")
showCmd.Flags().BoolVar(&printLimitations, "limitations", false, "Print limitations")
} }
func parseValueList(arr []string) string { func parseValueList(arr []string) string {
@ -37,6 +48,18 @@ var showCmd = &cobra.Command{
Short: "Show license details", Short: "Show license details",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// If none of the flags is passed then print all attributes
anyFlagChanged := false
for _, flag := range []string{"title", "description", "permissions", "conditions", "limitations"} {
if cmd.Flags().Changed(flag) {
anyFlagChanged = true
break
}
}
if !anyFlagChanged {
printTitle, printDescription, printPermissions, printConditions, printLimitations = true, true, true, true, true
}
licenseID := args[0] licenseID := args[0]
tmpl, err := t.Parse(licenseID + ".tmpl") tmpl, err := t.Parse(licenseID + ".tmpl")
if err != nil { if err != nil {
@ -44,17 +67,28 @@ var showCmd = &cobra.Command{
os.Exit(2) os.Exit(2)
} }
permissions := parseValueList(tmpl.Permissions) printable := []string{}
conditions := parseValueList(tmpl.Conditions)
limitations := parseValueList(tmpl.Limitations) if printTitle {
fmt.Printf( printable = append(printable, "Title: "+tmpl.Title)
"%s (%s)\n\n%s\n\nPermissions:\n%s\n\nConditions:\n%s\n\nLimitations:\n%s\n", }
tmpl.Title,
tmpl.ID, if printDescription {
tmpl.Description, printable = append(printable, "Description:\n"+tmpl.Description)
permissions, }
conditions,
limitations, if printPermissions {
) printable = append(printable, "Permissions:\n"+parseValueList(tmpl.Permissions))
}
if printConditions {
printable = append(printable, "Conditions:\n"+parseValueList(tmpl.Conditions))
}
if printLimitations {
printable = append(printable, "Limitations:\n"+parseValueList(tmpl.Limitations))
}
fmt.Println(strings.Join(printable, "\n\n"))
}, },
} }