Compare commits
2 Commits
dca641bfa1
...
090d3a490d
Author | SHA1 | Date | |
---|---|---|---|
090d3a490d | |||
efd0019d12 |
94
cmd/show.go
Normal file
94
cmd/show.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
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"
|
||||||
|
)
|
||||||
|
|
||||||
|
var printTitle bool
|
||||||
|
var printDescription bool
|
||||||
|
var printPermissions bool
|
||||||
|
var printConditions bool
|
||||||
|
var printLimitations bool
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
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 {
|
||||||
|
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) {
|
||||||
|
// 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]
|
||||||
|
tmpl, err := t.Parse(licenseID + ".tmpl")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: There is no '%s' license\n", licenseID)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
printable := []string{}
|
||||||
|
|
||||||
|
if printTitle {
|
||||||
|
printable = append(printable, "Title: "+tmpl.Title)
|
||||||
|
}
|
||||||
|
|
||||||
|
if printDescription {
|
||||||
|
printable = append(printable, "Description:\n"+tmpl.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
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"))
|
||||||
|
},
|
||||||
|
}
|
1
go.mod
1
go.mod
@ -4,6 +4,7 @@ go 1.23.4
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
|
golang.org/x/text v0.21.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
2
go.sum
2
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/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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user