github.com/loicalbertin/terraform@v0.6.15-0.20170626182346-8e2583055467/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  		GlobalPluginDirs: globalPluginDirs(),
    35  		PluginOverrides:  &PluginOverrides,
    36  		Ui:               Ui,
    37  	}
    38  
    39  	// The command list is included in the terraform -help
    40  	// output, which is in turn included in the docs at
    41  	// website/source/docs/commands/index.html.markdown; if you
    42  	// add, remove or reclassify commands then consider updating
    43  	// that to match.
    44  
    45  	PlumbingCommands = map[string]struct{}{
    46  		"state":        struct{}{}, // includes all subcommands
    47  		"debug":        struct{}{}, // includes all subcommands
    48  		"force-unlock": struct{}{},
    49  	}
    50  
    51  	Commands = map[string]cli.CommandFactory{
    52  		"apply": func() (cli.Command, error) {
    53  			return &command.ApplyCommand{
    54  				Meta:       meta,
    55  				ShutdownCh: makeShutdownCh(),
    56  			}, nil
    57  		},
    58  
    59  		"console": func() (cli.Command, error) {
    60  			return &command.ConsoleCommand{
    61  				Meta:       meta,
    62  				ShutdownCh: makeShutdownCh(),
    63  			}, nil
    64  		},
    65  
    66  		"destroy": func() (cli.Command, error) {
    67  			return &command.ApplyCommand{
    68  				Meta:       meta,
    69  				Destroy:    true,
    70  				ShutdownCh: makeShutdownCh(),
    71  			}, nil
    72  		},
    73  
    74  		"env": func() (cli.Command, error) {
    75  			return &command.WorkspaceCommand{
    76  				Meta:       meta,
    77  				LegacyName: true,
    78  			}, nil
    79  		},
    80  
    81  		"env list": func() (cli.Command, error) {
    82  			return &command.WorkspaceListCommand{
    83  				Meta:       meta,
    84  				LegacyName: true,
    85  			}, nil
    86  		},
    87  
    88  		"env select": func() (cli.Command, error) {
    89  			return &command.WorkspaceSelectCommand{
    90  				Meta:       meta,
    91  				LegacyName: true,
    92  			}, nil
    93  		},
    94  
    95  		"env new": func() (cli.Command, error) {
    96  			return &command.WorkspaceNewCommand{
    97  				Meta:       meta,
    98  				LegacyName: true,
    99  			}, nil
   100  		},
   101  
   102  		"env delete": func() (cli.Command, error) {
   103  			return &command.WorkspaceDeleteCommand{
   104  				Meta:       meta,
   105  				LegacyName: true,
   106  			}, nil
   107  		},
   108  
   109  		"fmt": func() (cli.Command, error) {
   110  			return &command.FmtCommand{
   111  				Meta: meta,
   112  			}, nil
   113  		},
   114  
   115  		"get": func() (cli.Command, error) {
   116  			return &command.GetCommand{
   117  				Meta: meta,
   118  			}, nil
   119  		},
   120  
   121  		"graph": func() (cli.Command, error) {
   122  			return &command.GraphCommand{
   123  				Meta: meta,
   124  			}, nil
   125  		},
   126  
   127  		"import": func() (cli.Command, error) {
   128  			return &command.ImportCommand{
   129  				Meta: meta,
   130  			}, nil
   131  		},
   132  
   133  		"init": func() (cli.Command, error) {
   134  			return &command.InitCommand{
   135  				Meta: meta,
   136  			}, nil
   137  		},
   138  
   139  		"internal-plugin": func() (cli.Command, error) {
   140  			return &command.InternalPluginCommand{
   141  				Meta: meta,
   142  			}, nil
   143  		},
   144  
   145  		"output": func() (cli.Command, error) {
   146  			return &command.OutputCommand{
   147  				Meta: meta,
   148  			}, nil
   149  		},
   150  
   151  		"plan": func() (cli.Command, error) {
   152  			return &command.PlanCommand{
   153  				Meta: meta,
   154  			}, nil
   155  		},
   156  
   157  		"providers": func() (cli.Command, error) {
   158  			return &command.ProvidersCommand{
   159  				Meta: meta,
   160  			}, nil
   161  		},
   162  
   163  		"push": func() (cli.Command, error) {
   164  			return &command.PushCommand{
   165  				Meta: meta,
   166  			}, nil
   167  		},
   168  
   169  		"refresh": func() (cli.Command, error) {
   170  			return &command.RefreshCommand{
   171  				Meta: meta,
   172  			}, nil
   173  		},
   174  
   175  		"show": func() (cli.Command, error) {
   176  			return &command.ShowCommand{
   177  				Meta: meta,
   178  			}, nil
   179  		},
   180  
   181  		"taint": func() (cli.Command, error) {
   182  			return &command.TaintCommand{
   183  				Meta: meta,
   184  			}, nil
   185  		},
   186  
   187  		"validate": func() (cli.Command, error) {
   188  			return &command.ValidateCommand{
   189  				Meta: meta,
   190  			}, nil
   191  		},
   192  
   193  		"version": func() (cli.Command, error) {
   194  			return &command.VersionCommand{
   195  				Meta:              meta,
   196  				Revision:          GitCommit,
   197  				Version:           Version,
   198  				VersionPrerelease: VersionPrerelease,
   199  				CheckFunc:         commandVersionCheck,
   200  			}, nil
   201  		},
   202  
   203  		"untaint": func() (cli.Command, error) {
   204  			return &command.UntaintCommand{
   205  				Meta: meta,
   206  			}, nil
   207  		},
   208  
   209  		"workspace": func() (cli.Command, error) {
   210  			return &command.WorkspaceCommand{
   211  				Meta: meta,
   212  			}, nil
   213  		},
   214  
   215  		"workspace list": func() (cli.Command, error) {
   216  			return &command.WorkspaceListCommand{
   217  				Meta: meta,
   218  			}, nil
   219  		},
   220  
   221  		"workspace select": func() (cli.Command, error) {
   222  			return &command.WorkspaceSelectCommand{
   223  				Meta: meta,
   224  			}, nil
   225  		},
   226  
   227  		"workspace new": func() (cli.Command, error) {
   228  			return &command.WorkspaceNewCommand{
   229  				Meta: meta,
   230  			}, nil
   231  		},
   232  
   233  		"workspace delete": func() (cli.Command, error) {
   234  			return &command.WorkspaceDeleteCommand{
   235  				Meta: meta,
   236  			}, nil
   237  		},
   238  
   239  		//-----------------------------------------------------------
   240  		// Plumbing
   241  		//-----------------------------------------------------------
   242  
   243  		"debug": func() (cli.Command, error) {
   244  			return &command.DebugCommand{
   245  				Meta: meta,
   246  			}, nil
   247  		},
   248  
   249  		"debug json2dot": func() (cli.Command, error) {
   250  			return &command.DebugJSON2DotCommand{
   251  				Meta: meta,
   252  			}, nil
   253  		},
   254  
   255  		"force-unlock": func() (cli.Command, error) {
   256  			return &command.UnlockCommand{
   257  				Meta: meta,
   258  			}, nil
   259  		},
   260  
   261  		"state": func() (cli.Command, error) {
   262  			return &command.StateCommand{}, nil
   263  		},
   264  
   265  		"state list": func() (cli.Command, error) {
   266  			return &command.StateListCommand{
   267  				Meta: meta,
   268  			}, nil
   269  		},
   270  
   271  		"state rm": func() (cli.Command, error) {
   272  			return &command.StateRmCommand{
   273  				Meta: meta,
   274  			}, nil
   275  		},
   276  
   277  		"state mv": func() (cli.Command, error) {
   278  			return &command.StateMvCommand{
   279  				Meta: meta,
   280  			}, nil
   281  		},
   282  
   283  		"state pull": func() (cli.Command, error) {
   284  			return &command.StatePullCommand{
   285  				Meta: meta,
   286  			}, nil
   287  		},
   288  
   289  		"state push": func() (cli.Command, error) {
   290  			return &command.StatePushCommand{
   291  				Meta: meta,
   292  			}, nil
   293  		},
   294  
   295  		"state show": func() (cli.Command, error) {
   296  			return &command.StateShowCommand{
   297  				Meta: meta,
   298  			}, nil
   299  		},
   300  	}
   301  }
   302  
   303  // makeShutdownCh creates an interrupt listener and returns a channel.
   304  // A message will be sent on the channel for every interrupt received.
   305  func makeShutdownCh() <-chan struct{} {
   306  	resultCh := make(chan struct{})
   307  
   308  	signalCh := make(chan os.Signal, 4)
   309  	signal.Notify(signalCh, ignoreSignals...)
   310  	signal.Notify(signalCh, forwardSignals...)
   311  	go func() {
   312  		for {
   313  			<-signalCh
   314  			resultCh <- struct{}{}
   315  		}
   316  	}()
   317  
   318  	return resultCh
   319  }