github.com/criteo-forks/consul@v1.4.5-criteonogrpc/command/registry.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  // Factory is a function that returns a new instance of a CLI-sub command.
    13  type Factory func(cli.Ui) (cli.Command, error)
    14  
    15  // Register adds a new CLI sub-command to the registry.
    16  func Register(name string, fn Factory) {
    17  	if registry == nil {
    18  		registry = make(map[string]Factory)
    19  	}
    20  
    21  	if registry[name] != nil {
    22  		panic(fmt.Errorf("Command %q is already registered", name))
    23  	}
    24  	registry[name] = fn
    25  }
    26  
    27  // Map returns a realized mapping of available CLI commands in a format that
    28  // the CLI class can consume. This should be called after all registration is
    29  // complete.
    30  func Map(ui cli.Ui) map[string]cli.CommandFactory {
    31  	m := make(map[string]cli.CommandFactory)
    32  	for name, fn := range registry {
    33  		thisFn := fn
    34  		m[name] = func() (cli.Command, error) {
    35  			return thisFn(ui)
    36  		}
    37  	}
    38  	return m
    39  }
    40  
    41  // registry has an entry for each available CLI sub-command, indexed by sub
    42  // command name. This should be populated at package init() time via Register().
    43  var registry map[string]Factory
    44  
    45  // MakeShutdownCh returns a channel that can be used for shutdown notifications
    46  // for commands. This channel will send a message for every interrupt or SIGTERM
    47  // received.
    48  func MakeShutdownCh() <-chan struct{} {
    49  	resultCh := make(chan struct{})
    50  	signalCh := make(chan os.Signal, 4)
    51  	signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
    52  	go func() {
    53  		for {
    54  			<-signalCh
    55  			resultCh <- struct{}{}
    56  		}
    57  	}()
    58  
    59  	return resultCh
    60  }