Refactor some functions for better enterprise use (#13280)

This commit is contained in:
Chris S. Kim 2022-05-30 09:46:55 -04:00 committed by GitHub
parent c4cc793066
commit ffa7d737af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 30 deletions

View File

@ -1,6 +1,11 @@
package command package command
import ( import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/hashicorp/consul/command/acl" "github.com/hashicorp/consul/command/acl"
aclagent "github.com/hashicorp/consul/command/acl/agenttokens" aclagent "github.com/hashicorp/consul/command/acl/agenttokens"
aclam "github.com/hashicorp/consul/command/acl/authmethod" aclam "github.com/hashicorp/consul/command/acl/authmethod"
@ -109,40 +114,16 @@ import (
"github.com/hashicorp/consul/command/version" "github.com/hashicorp/consul/command/version"
"github.com/hashicorp/consul/command/watch" "github.com/hashicorp/consul/command/watch"
"os"
"os/signal"
"syscall"
mcli "github.com/mitchellh/cli" mcli "github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/cli" "github.com/hashicorp/consul/command/cli"
) )
// factory is a function that returns a new instance of a CLI-sub command. // RegisteredCommands returns a realized mapping of available CLI commands in a format that
type factory func(cli.Ui) (cli.Command, error) // the CLI class can consume.
func RegisteredCommands(ui cli.Ui) map[string]mcli.CommandFactory {
// entry is a struct that contains a command's name and a factory for that command. registry := map[string]mcli.CommandFactory{}
type entry struct { registerCommands(ui, registry,
name string
fn factory
}
func createCommands(ui cli.Ui, cmdEntries ...entry) map[string]mcli.CommandFactory {
m := make(map[string]mcli.CommandFactory)
for _, ent := range cmdEntries {
thisFn := ent.fn
m[ent.name] = func() (mcli.Command, error) {
return thisFn(ui)
}
}
return m
}
// CommandsFromRegistry returns a realized mapping of available CLI commands in a format that
// the CLI class can consume. This should be called after all registration is
// complete.
func CommandsFromRegistry(ui cli.Ui) map[string]mcli.CommandFactory {
registry := createCommands(ui,
entry{"acl", func(cli.Ui) (cli.Command, error) { return acl.New(), nil }}, entry{"acl", func(cli.Ui) (cli.Command, error) { return acl.New(), nil }},
entry{"acl bootstrap", func(ui cli.Ui) (cli.Command, error) { return aclbootstrap.New(ui), nil }}, entry{"acl bootstrap", func(ui cli.Ui) (cli.Command, error) { return aclbootstrap.New(ui), nil }},
entry{"acl policy", func(cli.Ui) (cli.Command, error) { return aclpolicy.New(), nil }}, entry{"acl policy", func(cli.Ui) (cli.Command, error) { return aclpolicy.New(), nil }},
@ -255,6 +236,27 @@ func CommandsFromRegistry(ui cli.Ui) map[string]mcli.CommandFactory {
return registry return registry
} }
// factory is a function that returns a new instance of a CLI-sub command.
type factory func(cli.Ui) (cli.Command, error)
// entry is a struct that contains a command's name and a factory for that command.
type entry struct {
name string
fn factory
}
func registerCommands(ui cli.Ui, m map[string]mcli.CommandFactory, cmdEntries ...entry) {
for _, ent := range cmdEntries {
thisFn := ent.fn
if _, ok := m[ent.name]; ok {
panic(fmt.Sprintf("duplicate command: %q", ent.name))
}
m[ent.name] = func() (mcli.Command, error) {
return thisFn(ui)
}
}
}
// MakeShutdownCh returns a channel that can be used for shutdown notifications // MakeShutdownCh returns a channel that can be used for shutdown notifications
// for commands. This channel will send a message for every interrupt or SIGTERM // for commands. This channel will send a message for every interrupt or SIGTERM
// received. // received.

View File

@ -29,7 +29,7 @@ func realMain() int {
ui := &cli.BasicUI{ ui := &cli.BasicUI{
BasicUi: mcli.BasicUi{Writer: os.Stdout, ErrorWriter: os.Stderr}, BasicUi: mcli.BasicUi{Writer: os.Stdout, ErrorWriter: os.Stderr},
} }
cmds := command.CommandsFromRegistry(ui) cmds := command.RegisteredCommands(ui)
var names []string var names []string
for c := range cmds { for c := range cmds {
names = append(names, c) names = append(names, c)