github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/commands.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"os/signal"
     6  
     7  	foundationConsul "github.com/hashicorp/otto/builtin/foundation/consul"
     8  	infraAws "github.com/hashicorp/otto/builtin/infra/aws"
     9  
    10  	"github.com/hashicorp/otto/builtin/pluginmap"
    11  	"github.com/hashicorp/otto/command"
    12  	"github.com/hashicorp/otto/foundation"
    13  	"github.com/hashicorp/otto/infrastructure"
    14  	"github.com/hashicorp/otto/otto"
    15  	"github.com/mitchellh/cli"
    16  )
    17  
    18  // Commands is the mapping of all the available Otto commands.
    19  var Commands map[string]cli.CommandFactory
    20  var CommandsInclude []string
    21  
    22  // Ui is the cli.Ui used for communicating to the outside world.
    23  var Ui cli.Ui
    24  
    25  const (
    26  	ErrorPrefix  = "e:"
    27  	OutputPrefix = "o:"
    28  )
    29  
    30  func init() {
    31  	Ui = &cli.ColoredUi{
    32  		OutputColor: cli.UiColorNone,
    33  		InfoColor:   cli.UiColorNone,
    34  		ErrorColor:  cli.UiColorRed,
    35  		WarnColor:   cli.UiColorNone,
    36  		Ui: &cli.PrefixedUi{
    37  			AskPrefix:    OutputPrefix,
    38  			OutputPrefix: OutputPrefix,
    39  			InfoPrefix:   OutputPrefix,
    40  			ErrorPrefix:  ErrorPrefix,
    41  			Ui:           &cli.BasicUi{Writer: os.Stdout},
    42  		},
    43  	}
    44  
    45  	foundations := foundationConsul.Tuples.Map(foundation.StructFactory(new(foundationConsul.Foundation)))
    46  
    47  	meta := command.Meta{
    48  		CoreConfig: &otto.CoreConfig{
    49  			Foundations: foundations,
    50  			Infrastructures: map[string]infrastructure.Factory{
    51  				"aws": infraAws.Infra,
    52  			},
    53  		},
    54  		Ui:        Ui,
    55  		PluginMap: pluginmap.Map,
    56  	}
    57  
    58  	CommandsInclude = []string{
    59  		"compile",
    60  		"build",
    61  		"deploy",
    62  		"dev",
    63  		"infra",
    64  		"status",
    65  		"version",
    66  	}
    67  
    68  	Commands = map[string]cli.CommandFactory{
    69  		"compile": func() (cli.Command, error) {
    70  			return &command.CompileCommand{
    71  				Meta: meta,
    72  			}, nil
    73  		},
    74  
    75  		"build": func() (cli.Command, error) {
    76  			return &command.BuildCommand{
    77  				Meta: meta,
    78  			}, nil
    79  		},
    80  
    81  		"deploy": func() (cli.Command, error) {
    82  			return &command.DeployCommand{
    83  				Meta: meta,
    84  			}, nil
    85  		},
    86  
    87  		"dev": func() (cli.Command, error) {
    88  			return &command.DevCommand{
    89  				Meta: meta,
    90  			}, nil
    91  		},
    92  
    93  		"infra": func() (cli.Command, error) {
    94  			return &command.InfraCommand{
    95  				Meta: meta,
    96  			}, nil
    97  		},
    98  
    99  		"status": func() (cli.Command, error) {
   100  			return &command.StatusCommand{
   101  				Meta: meta,
   102  			}, nil
   103  		},
   104  
   105  		"version": func() (cli.Command, error) {
   106  			return &command.VersionCommand{
   107  				Meta:              meta,
   108  				Revision:          GitCommit,
   109  				Version:           Version,
   110  				VersionPrerelease: VersionPrerelease,
   111  				CheckFunc:         commandVersionCheck,
   112  			}, nil
   113  		},
   114  
   115  		// Internal or not shown to users directly
   116  
   117  		"plugin-builtin": func() (cli.Command, error) {
   118  			return &command.PluginBuiltinCommand{
   119  				Meta: meta,
   120  			}, nil
   121  		},
   122  
   123  		"help": func() (cli.Command, error) {
   124  			return &command.HelpCommand{
   125  				Meta: meta,
   126  			}, nil
   127  		},
   128  	}
   129  }
   130  
   131  // makeShutdownCh creates an interrupt listener and returns a channel.
   132  // A message will be sent on the channel for every interrupt received.
   133  func makeShutdownCh() <-chan struct{} {
   134  	resultCh := make(chan struct{})
   135  
   136  	signalCh := make(chan os.Signal, 4)
   137  	signal.Notify(signalCh, os.Interrupt)
   138  	go func() {
   139  		for {
   140  			<-signalCh
   141  			resultCh <- struct{}{}
   142  		}
   143  	}()
   144  
   145  	return resultCh
   146  }