github.com/seilagamo/poc-lava-release@v0.3.3-rc3/cmd/lava/internal/help/help.go (about) 1 // Copyright 2023 Adevinta 2 3 // Package help implements the help command. 4 package help 5 6 import ( 7 "fmt" 8 "os" 9 "strings" 10 "text/template" 11 12 "github.com/seilagamo/poc-lava-release/cmd/lava/internal/base" 13 ) 14 15 // Help prints the documentation of the provided command. 16 func Help(args []string) { 17 if len(args) == 0 { 18 PrintUsage() 19 return 20 } 21 if len(args) != 1 { 22 fmt.Fprintf(os.Stderr, "usage: lava help <topic>\n\nToo many arguments given.\n") 23 os.Exit(2) 24 } 25 26 arg := args[0] 27 28 for _, cmd := range base.Commands { 29 if cmd.Name() == arg { 30 tmpl(helpTemplate, cmd) 31 return 32 } 33 } 34 35 fmt.Fprintf(os.Stderr, "Unknown help topic %q. Run 'lava help'.\n", arg) 36 os.Exit(2) 37 } 38 39 // PrintUsage prints a usage message documenting all the Lava 40 // commands. 41 func PrintUsage() { 42 tmpl(usageTemplate, base.Commands) 43 } 44 45 func tmpl(text string, data interface{}) { 46 t := template.New("top") 47 t.Funcs(template.FuncMap{"trim": strings.TrimSpace}) 48 template.Must(t.Parse(text)) 49 if err := t.Execute(os.Stderr, data); err != nil { 50 panic(err) 51 } 52 } 53 54 const usageTemplate = `Lava runs Vulcan checks locally 55 56 Usage: 57 58 lava <command> [arguments] 59 60 The commands are: 61 {{range .}}{{ if .Run}} 62 {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}} 63 64 Use 'lava help <command>' for more information about a command. 65 66 Additional help topics: 67 {{range .}}{{if not .Run}} 68 {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}} 69 70 Use 'lava help <topic>' for more information about that topic. 71 ` 72 73 const helpTemplate = `{{if .Run}}usage: lava {{.UsageLine}} 74 75 {{end}}{{.Long | trim}} 76 `