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