github.com/ffrizzo/terraform@v0.8.2-0.20161219200057-992e12335f3d/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  	}
    48  
    49  	Commands = map[string]cli.CommandFactory{
    50  		"apply": func() (cli.Command, error) {
    51  			return &command.ApplyCommand{
    52  				Meta:       meta,
    53  				ShutdownCh: makeShutdownCh(),
    54  			}, nil
    55  		},
    56  
    57  		"console": func() (cli.Command, error) {
    58  			return &command.ConsoleCommand{
    59  				Meta:       meta,
    60  				ShutdownCh: makeShutdownCh(),
    61  			}, nil
    62  		},
    63  
    64  		"destroy": func() (cli.Command, error) {
    65  			return &command.ApplyCommand{
    66  				Meta:       meta,
    67  				Destroy:    true,
    68  				ShutdownCh: makeShutdownCh(),
    69  			}, nil
    70  		},
    71  
    72  		"fmt": func() (cli.Command, error) {
    73  			return &command.FmtCommand{
    74  				Meta: meta,
    75  			}, nil
    76  		},
    77  
    78  		"get": func() (cli.Command, error) {
    79  			return &command.GetCommand{
    80  				Meta: meta,
    81  			}, nil
    82  		},
    83  
    84  		"graph": func() (cli.Command, error) {
    85  			return &command.GraphCommand{
    86  				Meta: meta,
    87  			}, nil
    88  		},
    89  
    90  		"import": func() (cli.Command, error) {
    91  			return &command.ImportCommand{
    92  				Meta: meta,
    93  			}, nil
    94  		},
    95  
    96  		"init": func() (cli.Command, error) {
    97  			return &command.InitCommand{
    98  				Meta: meta,
    99  			}, nil
   100  		},
   101  
   102  		"internal-plugin": func() (cli.Command, error) {
   103  			return &command.InternalPluginCommand{
   104  				Meta: meta,
   105  			}, nil
   106  		},
   107  
   108  		"output": func() (cli.Command, error) {
   109  			return &command.OutputCommand{
   110  				Meta: meta,
   111  			}, nil
   112  		},
   113  
   114  		"plan": func() (cli.Command, error) {
   115  			return &command.PlanCommand{
   116  				Meta: meta,
   117  			}, nil
   118  		},
   119  
   120  		"push": func() (cli.Command, error) {
   121  			return &command.PushCommand{
   122  				Meta: meta,
   123  			}, nil
   124  		},
   125  
   126  		"refresh": func() (cli.Command, error) {
   127  			return &command.RefreshCommand{
   128  				Meta: meta,
   129  			}, nil
   130  		},
   131  
   132  		"remote": func() (cli.Command, error) {
   133  			return &command.RemoteCommand{
   134  				Meta: meta,
   135  			}, nil
   136  		},
   137  
   138  		"show": func() (cli.Command, error) {
   139  			return &command.ShowCommand{
   140  				Meta: meta,
   141  			}, nil
   142  		},
   143  
   144  		"taint": func() (cli.Command, error) {
   145  			return &command.TaintCommand{
   146  				Meta: meta,
   147  			}, nil
   148  		},
   149  
   150  		"validate": func() (cli.Command, error) {
   151  			return &command.ValidateCommand{
   152  				Meta: meta,
   153  			}, nil
   154  		},
   155  
   156  		"version": func() (cli.Command, error) {
   157  			return &command.VersionCommand{
   158  				Meta:              meta,
   159  				Revision:          GitCommit,
   160  				Version:           Version,
   161  				VersionPrerelease: VersionPrerelease,
   162  				CheckFunc:         commandVersionCheck,
   163  			}, nil
   164  		},
   165  
   166  		"untaint": func() (cli.Command, error) {
   167  			return &command.UntaintCommand{
   168  				Meta: meta,
   169  			}, nil
   170  		},
   171  
   172  		//-----------------------------------------------------------
   173  		// Plumbing
   174  		//-----------------------------------------------------------
   175  
   176  		"debug": func() (cli.Command, error) {
   177  			return &command.DebugCommand{
   178  				Meta: meta,
   179  			}, nil
   180  		},
   181  
   182  		"debug json2dot": func() (cli.Command, error) {
   183  			return &command.DebugJSON2DotCommand{
   184  				Meta: meta,
   185  			}, nil
   186  		},
   187  
   188  		"state": func() (cli.Command, error) {
   189  			return &command.StateCommand{
   190  				Meta: meta,
   191  			}, nil
   192  		},
   193  
   194  		"state list": func() (cli.Command, error) {
   195  			return &command.StateListCommand{
   196  				Meta: meta,
   197  			}, nil
   198  		},
   199  
   200  		"state rm": func() (cli.Command, error) {
   201  			return &command.StateRmCommand{
   202  				Meta: meta,
   203  			}, nil
   204  		},
   205  
   206  		"state mv": func() (cli.Command, error) {
   207  			return &command.StateMvCommand{
   208  				Meta: meta,
   209  			}, nil
   210  		},
   211  
   212  		"state show": func() (cli.Command, error) {
   213  			return &command.StateShowCommand{
   214  				Meta: meta,
   215  			}, nil
   216  		},
   217  	}
   218  }
   219  
   220  // makeShutdownCh creates an interrupt listener and returns a channel.
   221  // A message will be sent on the channel for every interrupt received.
   222  func makeShutdownCh() <-chan struct{} {
   223  	resultCh := make(chan struct{})
   224  
   225  	signalCh := make(chan os.Signal, 4)
   226  	signal.Notify(signalCh, ignoreSignals...)
   227  	signal.Notify(signalCh, forwardSignals...)
   228  	go func() {
   229  		for {
   230  			<-signalCh
   231  			resultCh <- struct{}{}
   232  		}
   233  	}()
   234  
   235  	return resultCh
   236  }