github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/runtime/controller/help.go (about) 1 package controller 2 3 import ( 4 "os" 5 "text/template" 6 ) 7 8 const templateHelpText string = ` 9 NAME: 10 {{ .Name }} - {{ .Desc }} 11 12 USAGE: 13 {{ .Name }} [global options] command [command options] [arguments...] 14 15 COMMANDS: 16 help, h Shows a list of commands or help for one command 17 18 GLOBAL OPTIONS: 19 --help, -h show help 20 --all show all available variables. by default show only required 21 --yaml show environment variables as a yaml, table by default 22 --without-comments disable printing envs comments 23 24 ENVIRONMENT VARIABLES 25 26 {{ .Envs }} 27 28 ` 29 30 func (c *controller) help() bool { 31 32 var ( 33 help, all, yaml, nocomments bool 34 ) 35 36 for _, arg := range os.Args { 37 switch arg { 38 case "-h": 39 help = true 40 case "--help": 41 help = true 42 case "h": 43 help = true 44 case "help": 45 help = true 46 case "--all": 47 all = true 48 case "--yaml": 49 yaml = true 50 case "--without-comments": 51 nocomments = true 52 } 53 54 } 55 56 if !help { 57 return false 58 } 59 60 type data struct { 61 Name string 62 Desc string 63 Envs string 64 } 65 66 tpl, err := template.New("help").Parse(templateHelpText) 67 if err != nil { 68 panic(err) 69 } 70 71 var envs = c.Config().PrintTable(all, nocomments) 72 73 if yaml { 74 envs = c.Config().PrintYaml(all, nocomments) 75 } 76 77 if err := tpl.Execute(os.Stdout, data{ 78 Name: c.meta.GetName(), 79 Desc: c.meta.GetDescription(), 80 Envs: envs, 81 }); err != nil { 82 panic(err) 83 } 84 85 return true 86 }