github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  		"env": func() (cli.Command, error) {
    73  			return &command.EnvCommand{
    74  				Meta: meta,
    75  			}, nil
    76  		},
    77  
    78  		"env list": func() (cli.Command, error) {
    79  			return &command.EnvListCommand{
    80  				Meta: meta,
    81  			}, nil
    82  		},
    83  
    84  		"env select": func() (cli.Command, error) {
    85  			return &command.EnvSelectCommand{
    86  				Meta: meta,
    87  			}, nil
    88  		},
    89  
    90  		"env new": func() (cli.Command, error) {
    91  			return &command.EnvNewCommand{
    92  				Meta: meta,
    93  			}, nil
    94  		},
    95  
    96  		"env delete": func() (cli.Command, error) {
    97  			return &command.EnvDeleteCommand{
    98  				Meta: meta,
    99  			}, nil
   100  		},
   101  
   102  		"fmt": func() (cli.Command, error) {
   103  			return &command.FmtCommand{
   104  				Meta: meta,
   105  			}, nil
   106  		},
   107  
   108  		"force-unlock": func() (cli.Command, error) {
   109  			return &command.UnlockCommand{
   110  				Meta: meta,
   111  			}, nil
   112  		},
   113  
   114  		"get": func() (cli.Command, error) {
   115  			return &command.GetCommand{
   116  				Meta: meta,
   117  			}, nil
   118  		},
   119  
   120  		"graph": func() (cli.Command, error) {
   121  			return &command.GraphCommand{
   122  				Meta: meta,
   123  			}, nil
   124  		},
   125  
   126  		"import": func() (cli.Command, error) {
   127  			return &command.ImportCommand{
   128  				Meta: meta,
   129  			}, nil
   130  		},
   131  
   132  		"init": func() (cli.Command, error) {
   133  			return &command.InitCommand{
   134  				Meta: meta,
   135  			}, nil
   136  		},
   137  
   138  		"internal-plugin": func() (cli.Command, error) {
   139  			return &command.InternalPluginCommand{
   140  				Meta: meta,
   141  			}, nil
   142  		},
   143  
   144  		"output": func() (cli.Command, error) {
   145  			return &command.OutputCommand{
   146  				Meta: meta,
   147  			}, nil
   148  		},
   149  
   150  		"plan": func() (cli.Command, error) {
   151  			return &command.PlanCommand{
   152  				Meta: meta,
   153  			}, nil
   154  		},
   155  
   156  		"push": func() (cli.Command, error) {
   157  			return &command.PushCommand{
   158  				Meta: meta,
   159  			}, nil
   160  		},
   161  
   162  		"refresh": func() (cli.Command, error) {
   163  			return &command.RefreshCommand{
   164  				Meta: meta,
   165  			}, nil
   166  		},
   167  
   168  		"show": func() (cli.Command, error) {
   169  			return &command.ShowCommand{
   170  				Meta: meta,
   171  			}, nil
   172  		},
   173  
   174  		"taint": func() (cli.Command, error) {
   175  			return &command.TaintCommand{
   176  				Meta: meta,
   177  			}, nil
   178  		},
   179  
   180  		"validate": func() (cli.Command, error) {
   181  			return &command.ValidateCommand{
   182  				Meta: meta,
   183  			}, nil
   184  		},
   185  
   186  		"version": func() (cli.Command, error) {
   187  			return &command.VersionCommand{
   188  				Meta:              meta,
   189  				Revision:          GitCommit,
   190  				Version:           Version,
   191  				VersionPrerelease: VersionPrerelease,
   192  				CheckFunc:         commandVersionCheck,
   193  			}, nil
   194  		},
   195  
   196  		"untaint": func() (cli.Command, error) {
   197  			return &command.UntaintCommand{
   198  				Meta: meta,
   199  			}, nil
   200  		},
   201  
   202  		//-----------------------------------------------------------
   203  		// Plumbing
   204  		//-----------------------------------------------------------
   205  
   206  		"debug": func() (cli.Command, error) {
   207  			return &command.DebugCommand{
   208  				Meta: meta,
   209  			}, nil
   210  		},
   211  
   212  		"debug json2dot": func() (cli.Command, error) {
   213  			return &command.DebugJSON2DotCommand{
   214  				Meta: meta,
   215  			}, nil
   216  		},
   217  
   218  		"state": func() (cli.Command, error) {
   219  			return &command.StateCommand{}, nil
   220  		},
   221  
   222  		"state list": func() (cli.Command, error) {
   223  			return &command.StateListCommand{
   224  				Meta: meta,
   225  			}, nil
   226  		},
   227  
   228  		"state rm": func() (cli.Command, error) {
   229  			return &command.StateRmCommand{
   230  				Meta: meta,
   231  			}, nil
   232  		},
   233  
   234  		"state mv": func() (cli.Command, error) {
   235  			return &command.StateMvCommand{
   236  				Meta: meta,
   237  			}, nil
   238  		},
   239  
   240  		"state pull": func() (cli.Command, error) {
   241  			return &command.StatePullCommand{
   242  				Meta: meta,
   243  			}, nil
   244  		},
   245  
   246  		"state push": func() (cli.Command, error) {
   247  			return &command.StatePushCommand{
   248  				Meta: meta,
   249  			}, nil
   250  		},
   251  
   252  		"state show": func() (cli.Command, error) {
   253  			return &command.StateShowCommand{
   254  				Meta: meta,
   255  			}, nil
   256  		},
   257  	}
   258  }
   259  
   260  // makeShutdownCh creates an interrupt listener and returns a channel.
   261  // A message will be sent on the channel for every interrupt received.
   262  func makeShutdownCh() <-chan struct{} {
   263  	resultCh := make(chan struct{})
   264  
   265  	signalCh := make(chan os.Signal, 4)
   266  	signal.Notify(signalCh, ignoreSignals...)
   267  	signal.Notify(signalCh, forwardSignals...)
   268  	go func() {
   269  		for {
   270  			<-signalCh
   271  			resultCh <- struct{}{}
   272  		}
   273  	}()
   274  
   275  	return resultCh
   276  }