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