3bac52480e
Reverts: - bdb274852ae469c89092d6050697c0ff97178465 - 2c689179c4f61c11f0016214c0fc127a0b813bfe - d62e25c4a7ab753914b6baccd66f88ffd10949a3 - c727ffbcc98e3e0bf41e1a7bdd40169bd2d22191 - 31b4d18933fd0acbe157e28d03ad59c2abf9a1fb - 85c3f8df3eabc00f490cd392213c3b928a85aa44
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
// Factory is a function that returns a new instance of a CLI-sub command.
|
|
type Factory func(cli.Ui) (cli.Command, error)
|
|
|
|
// Register adds a new CLI sub-command to the registry.
|
|
func Register(name string, fn Factory) {
|
|
if registry == nil {
|
|
registry = make(map[string]Factory)
|
|
}
|
|
|
|
if registry[name] != nil {
|
|
panic(fmt.Errorf("Command %q is already registered", name))
|
|
}
|
|
registry[name] = fn
|
|
}
|
|
|
|
// Map 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 Map(ui cli.Ui) map[string]cli.CommandFactory {
|
|
m := make(map[string]cli.CommandFactory)
|
|
for name, fn := range registry {
|
|
thisFn := fn
|
|
m[name] = func() (cli.Command, error) {
|
|
return thisFn(ui)
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
// registry has an entry for each available CLI sub-command, indexed by sub
|
|
// command name. This should be populated at package init() time via Register().
|
|
var registry map[string]Factory
|
|
|
|
// MakeShutdownCh returns a channel that can be used for shutdown notifications
|
|
// for commands. This channel will send a message for every interrupt or SIGTERM
|
|
// received.
|
|
func MakeShutdownCh() <-chan struct{} {
|
|
resultCh := make(chan struct{})
|
|
signalCh := make(chan os.Signal, 4)
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
|
go func() {
|
|
for {
|
|
<-signalCh
|
|
resultCh <- struct{}{}
|
|
}
|
|
}()
|
|
|
|
return resultCh
|
|
}
|