github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/commands.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"os/signal"
     6  
     7  	"github.com/hashicorp/terraform/command"
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  // Commands is the mapping of all the available Terraform commands.
    12  var Commands map[string]cli.CommandFactory
    13  var PlumbingCommands map[string]struct{}
    14  
    15  // Ui is the cli.Ui used for communicating to the outside world.
    16  var Ui cli.Ui
    17  
    18  const (
    19  	ErrorPrefix  = "e:"
    20  	OutputPrefix = "o:"
    21  )
    22  
    23  func init() {
    24  	Ui = &cli.PrefixedUi{
    25  		AskPrefix:    OutputPrefix,
    26  		OutputPrefix: OutputPrefix,
    27  		InfoPrefix:   OutputPrefix,
    28  		ErrorPrefix:  ErrorPrefix,
    29  		Ui:           &cli.BasicUi{Writer: os.Stdout},
    30  	}
    31  
    32  	meta := command.Meta{
    33  		Color:       true,
    34  		ContextOpts: &ContextOpts,
    35  		Ui:          Ui,
    36  	}
    37  
    38  	// The command list is included in the terraform -help
    39  	// output, which is in turn included in the docs at
    40  	// website/source/docs/commands/index.html.markdown; if you
    41  	// add, remove or reclassify commands then consider updating
    42  	// that to match.
    43  
    44  	PlumbingCommands = map[string]struct{}{
    45  		"state":        struct{}{}, // includes all subcommands
    46  		"debug":        struct{}{}, // includes all subcommands
    47  		"force-unlock": struct{}{},
    48  	}
    49  
    50  	Commands = map[string]cli.CommandFactory{
    51  		"apply": func() (cli.Command, error) {
    52  			return &command.ApplyCommand{
    53  				Meta:       meta,
    54  				ShutdownCh: makeShutdownCh(),
    55  			}, nil
    56  		},
    57  
    58  		"console": func() (cli.Command, error) {
    59  			return &command.ConsoleCommand{
    60  				Meta:       meta,
    61  				ShutdownCh: makeShutdownCh(),
    62  			}, nil
    63  		},
    64  
    65  		"destroy": func() (cli.Command, error) {
    66  			return &command.ApplyCommand{
    67  				Meta:       meta,
    68  				Destroy:    true,
    69  				ShutdownCh: makeShutdownCh(),
    70  			}, nil
    71  		},
    72  
    73  		"env": func() (cli.Command, error) {
    74  			return &command.EnvCommand{
    75  				Meta: meta,
    76  			}, nil
    77  		},
    78  
    79  		"env list": func() (cli.Command, error) {
    80  			return &command.EnvListCommand{
    81  				Meta: meta,
    82  			}, nil
    83  		},
    84  
    85  		"env select": func() (cli.Command, error) {
    86  			return &command.EnvSelectCommand{
    87  				Meta: meta,
    88  			}, nil
    89  		},
    90  
    91  		"env new": func() (cli.Command, error) {
    92  			return &command.EnvNewCommand{
    93  				Meta: meta,
    94  			}, nil
    95  		},
    96  
    97  		"env delete": func() (cli.Command, error) {
    98  			return &command.EnvDeleteCommand{
    99  				Meta: meta,
   100  			}, nil
   101  		},
   102  
   103  		"fmt": func() (cli.Command, error) {
   104  			return &command.FmtCommand{
   105  				Meta: meta,
   106  			}, nil
   107  		},
   108  
   109  		"get": func() (cli.Command, error) {
   110  			return &command.GetCommand{
   111  				Meta: meta,
   112  			}, nil
   113  		},
   114  
   115  		"graph": func() (cli.Command, error) {
   116  			return &command.GraphCommand{
   117  				Meta: meta,
   118  			}, nil
   119  		},
   120  
   121  		"import": func() (cli.Command, error) {
   122  			return &command.ImportCommand{
   123  				Meta: meta,
   124  			}, nil
   125  		},
   126  
   127  		"init": func() (cli.Command, error) {
   128  			return &command.InitCommand{
   129  				Meta: meta,
   130  			}, nil
   131  		},
   132  
   133  		"internal-plugin": func() (cli.Command, error) {
   134  			return &command.InternalPluginCommand{
   135  				Meta: meta,
   136  			}, nil
   137  		},
   138  
   139  		"output": func() (cli.Command, error) {
   140  			return &command.OutputCommand{
   141  				Meta: meta,
   142  			}, nil
   143  		},
   144  
   145  		"plan": func() (cli.Command, error) {
   146  			return &command.PlanCommand{
   147  				Meta: meta,
   148  			}, nil
   149  		},
   150  
   151  		"push": func() (cli.Command, error) {
   152  			return &command.PushCommand{
   153  				Meta: meta,
   154  			}, nil
   155  		},
   156  
   157  		"refresh": func() (cli.Command, error) {
   158  			return &command.RefreshCommand{
   159  				Meta: meta,
   160  			}, nil
   161  		},
   162  
   163  		"show": func() (cli.Command, error) {
   164  			return &command.ShowCommand{
   165  				Meta: meta,
   166  			}, nil
   167  		},
   168  
   169  		"taint": func() (cli.Command, error) {
   170  			return &command.TaintCommand{
   171  				Meta: meta,
   172  			}, nil
   173  		},
   174  
   175  		"validate": func() (cli.Command, error) {
   176  			return &command.ValidateCommand{
   177  				Meta: meta,
   178  			}, nil
   179  		},
   180  
   181  		"version": func() (cli.Command, error) {
   182  			return &command.VersionCommand{
   183  				Meta:              meta,
   184  				Revision:          GitCommit,
   185  				Version:           Version,
   186  				VersionPrerelease: VersionPrerelease,
   187  				CheckFunc:         commandVersionCheck,
   188  			}, nil
   189  		},
   190  
   191  		"untaint": func() (cli.Command, error) {
   192  			return &command.UntaintCommand{
   193  				Meta: meta,
   194  			}, nil
   195  		},
   196  
   197  		//-----------------------------------------------------------
   198  		// Plumbing
   199  		//-----------------------------------------------------------
   200  
   201  		"debug": func() (cli.Command, error) {
   202  			return &command.DebugCommand{
   203  				Meta: meta,
   204  			}, nil
   205  		},
   206  
   207  		"debug json2dot": func() (cli.Command, error) {
   208  			return &command.DebugJSON2DotCommand{
   209  				Meta: meta,
   210  			}, nil
   211  		},
   212  
   213  		"force-unlock": func() (cli.Command, error) {
   214  			return &command.UnlockCommand{
   215  				Meta: meta,
   216  			}, nil
   217  		},
   218  
   219  		"state": func() (cli.Command, error) {
   220  			return &command.StateCommand{}, nil
   221  		},
   222  
   223  		"state list": func() (cli.Command, error) {
   224  			return &command.StateListCommand{
   225  				Meta: meta,
   226  			}, nil
   227  		},
   228  
   229  		"state rm": func() (cli.Command, error) {
   230  			return &command.StateRmCommand{
   231  				Meta: meta,
   232  			}, nil
   233  		},
   234  
   235  		"state mv": func() (cli.Command, error) {
   236  			return &command.StateMvCommand{
   237  				Meta: meta,
   238  			}, nil
   239  		},
   240  
   241  		"state pull": func() (cli.Command, error) {
   242  			return &command.StatePullCommand{
   243  				Meta: meta,
   244  			}, nil
   245  		},
   246  
   247  		"state push": func() (cli.Command, error) {
   248  			return &command.StatePushCommand{
   249  				Meta: meta,
   250  			}, nil
   251  		},
   252  
   253  		"state show": func() (cli.Command, error) {
   254  			return &command.StateShowCommand{
   255  				Meta: meta,
   256  			}, nil
   257  		},
   258  	}
   259  }
   260  
   261  // makeShutdownCh creates an interrupt listener and returns a channel.
   262  // A message will be sent on the channel for every interrupt received.
   263  func makeShutdownCh() <-chan struct{} {
   264  	resultCh := make(chan struct{})
   265  
   266  	signalCh := make(chan os.Signal, 4)
   267  	signal.Notify(signalCh, ignoreSignals...)
   268  	signal.Notify(signalCh, forwardSignals...)
   269  	go func() {
   270  		for {
   271  			<-signalCh
   272  			resultCh <- struct{}{}
   273  		}
   274  	}()
   275  
   276  	return resultCh
   277  }