commands: get HTTP API flags for usage automatically

This commit is contained in:
Frank Schroeder 2017-10-18 00:00:01 +02:00 committed by Frank Schröder
parent cb8faa3559
commit 8f58a603ea
40 changed files with 79 additions and 80 deletions

View File

@ -70,7 +70,7 @@ type cmd struct {
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
config.AddFlags(c.flags, &c.flagArgs)
c.help = flags.Usage(help, c.flags, nil, nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -20,7 +20,7 @@ func (c *cmd) Synopsis() string {
}
func (c *cmd) Help() string {
return flags.Usage(help, nil, nil, nil)
return flags.Usage(help, nil)
}
const synopsis = "Interact with the catalog"

View File

@ -26,7 +26,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -48,7 +48,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -46,7 +46,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -40,7 +40,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -60,7 +60,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -10,6 +10,6 @@ func Merge(dst, src *flag.FlagSet) {
return
}
src.VisitAll(func(f *flag.Flag) {
dst.Var(f.Value, f.Name, f.DefValue)
dst.Var(f.Value, f.Name, f.Usage)
})
}

View File

@ -10,21 +10,17 @@ import (
text "github.com/tonnerre/golang-text"
)
func Usage(txt string, cmdFlags, clientFlags, serverFlags *flag.FlagSet) string {
func Usage(txt string, flags *flag.FlagSet) string {
u := &Usager{
Usage: txt,
CmdFlags: cmdFlags,
HTTPClientFlags: clientFlags,
HTTPServerFlags: serverFlags,
Usage: txt,
Flags: flags,
}
return u.String()
}
type Usager struct {
Usage string
CmdFlags *flag.FlagSet
HTTPClientFlags *flag.FlagSet
HTTPServerFlags *flag.FlagSet
Usage string
Flags *flag.FlagSet
}
func (u *Usager) String() string {
@ -33,28 +29,39 @@ func (u *Usager) String() string {
out.WriteString("\n")
out.WriteString("\n")
httpFlags := u.HTTPClientFlags
if httpFlags == nil {
httpFlags = u.HTTPServerFlags
} else {
Merge(httpFlags, u.HTTPServerFlags)
}
if u.Flags != nil {
f := &HTTPFlags{}
clientFlags := f.ClientFlags()
serverFlags := f.ServerFlags()
if httpFlags != nil {
printTitle(out, "HTTP API Options")
httpFlags.VisitAll(func(f *flag.Flag) {
printFlag(out, f)
})
}
if u.CmdFlags != nil {
printTitle(out, "Command Options")
u.CmdFlags.VisitAll(func(f *flag.Flag) {
if flagContains(httpFlags, f) {
return
var httpFlags, cmdFlags *flag.FlagSet
u.Flags.VisitAll(func(f *flag.Flag) {
if contains(clientFlags, f) || contains(serverFlags, f) {
if httpFlags == nil {
httpFlags = flag.NewFlagSet("", flag.ContinueOnError)
}
httpFlags.Var(f.Value, f.Name, f.Usage)
} else {
if cmdFlags == nil {
cmdFlags = flag.NewFlagSet("", flag.ContinueOnError)
}
cmdFlags.Var(f.Value, f.Name, f.Usage)
}
printFlag(out, f)
})
if httpFlags != nil {
printTitle(out, "HTTP API Options")
httpFlags.VisitAll(func(f *flag.Flag) {
printFlag(out, f)
})
}
if cmdFlags != nil {
printTitle(out, "Command Options")
cmdFlags.VisitAll(func(f *flag.Flag) {
printFlag(out, f)
})
}
}
return strings.TrimRight(out.String(), "\n")
@ -78,26 +85,18 @@ func printFlag(w io.Writer, f *flag.Flag) {
fmt.Fprintf(w, "%s\n\n", indented)
}
// flagContains returns true if the given flag is contained in the given flag
// contains returns true if the given flag is contained in the given flag
// set or false otherwise.
func flagContains(fs *flag.FlagSet, f *flag.Flag) bool {
func contains(fs *flag.FlagSet, f *flag.Flag) bool {
if fs == nil {
return false
}
var skip bool
var in bool
fs.VisitAll(func(hf *flag.Flag) {
if skip {
return
}
if f.Name == hf.Name {
skip = true
return
}
in = in || f.Name == hf.Name
})
return skip
return in
}
// maxLineLength is the maximum width of any line.

View File

@ -25,7 +25,7 @@ func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -26,7 +26,7 @@ func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -29,7 +29,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -24,7 +24,7 @@ type cmd struct {
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.help = flags.Usage(help, c.flags, nil, nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -51,7 +51,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -39,7 +39,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -29,7 +29,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -54,7 +54,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -38,7 +38,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -20,7 +20,7 @@ func (c *cmd) Synopsis() string {
}
func (c *cmd) Help() string {
return flags.Usage(help, nil, nil, nil)
return flags.Usage(help, nil)
}
const synopsis = "Interact with the key-value store"

View File

@ -70,7 +70,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -25,7 +25,7 @@ func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -103,7 +103,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -44,7 +44,7 @@ func (c *cmd) init() {
c.flags.StringVar(&c.serviceID, "service", "",
"Control maintenance mode for a specific service ID.")
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -51,7 +51,7 @@ func (c *cmd) init() {
c.flags.StringVar(&c.segment, "segment", consulapi.AllSegments,
"(Enterprise-only) If provided, output is filtered to only nodes in"+
"the given segment.")
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -40,7 +40,7 @@ func (c *cmd) init() {
c.flags.StringVar(&c.logLevel, "log-level", "INFO",
"Log level of the agent.")
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -27,7 +27,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -20,7 +20,7 @@ func (c *cmd) Synopsis() string {
}
func (c *cmd) Help() string {
return flags.Usage(help, nil, nil, nil)
return flags.Usage(help, nil)
}
const synopsis = "Provides tools for modifying Autopilot configuration"

View File

@ -62,7 +62,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -20,7 +20,7 @@ func (c *cmd) Synopsis() string {
}
func (c *cmd) Help() string {
return flags.Usage(help, nil, nil, nil)
return flags.Usage(help, nil)
}
const synopsis = "Provides cluster-level tools for Consul operators"

View File

@ -28,7 +28,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -20,7 +20,7 @@ func (c *cmd) Synopsis() string {
}
func (c *cmd) Help() string {
return flags.Usage(help, nil, nil, nil)
return flags.Usage(help, nil)
}
const synopsis = "Provides cluster-level tools for Consul operators"

View File

@ -36,7 +36,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -25,7 +25,7 @@ func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -34,7 +34,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -26,7 +26,7 @@ type cmd struct {
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.help = flags.Usage(help, c.flags, nil, nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -27,7 +27,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -30,7 +30,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -20,7 +20,7 @@ func (c *cmd) Synopsis() string {
}
func (c *cmd) Help() string {
return flags.Usage(help, nil, nil, nil)
return flags.Usage(help, nil)
}
const synopsis = "Saves, restores and inspects snapshots of Consul server state"

View File

@ -26,7 +26,7 @@ func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.quiet, "quiet", false,
"When given, a successful run will produce no output.")
c.help = flags.Usage(help, c.flags, nil, nil)
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {

View File

@ -70,7 +70,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.help = flags.Usage(help, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {