github.com/jdextraze/terraform@v0.6.17-0.20160511153921-e33847c8a8af/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  		"init": func() (cli.Command, error) {
    77  			return &command.InitCommand{
    78  				Meta: meta,
    79  			}, nil
    80  		},
    81  
    82  		"internal-plugin": func() (cli.Command, error) {
    83  			return &command.InternalPluginCommand{
    84  				Meta: meta,
    85  			}, nil
    86  		},
    87  
    88  		"output": func() (cli.Command, error) {
    89  			return &command.OutputCommand{
    90  				Meta: meta,
    91  			}, nil
    92  		},
    93  
    94  		"plan": func() (cli.Command, error) {
    95  			return &command.PlanCommand{
    96  				Meta: meta,
    97  			}, nil
    98  		},
    99  
   100  		"push": func() (cli.Command, error) {
   101  			return &command.PushCommand{
   102  				Meta: meta,
   103  			}, nil
   104  		},
   105  
   106  		"refresh": func() (cli.Command, error) {
   107  			return &command.RefreshCommand{
   108  				Meta: meta,
   109  			}, nil
   110  		},
   111  
   112  		"remote": func() (cli.Command, error) {
   113  			return &command.RemoteCommand{
   114  				Meta: meta,
   115  			}, nil
   116  		},
   117  
   118  		"show": func() (cli.Command, error) {
   119  			return &command.ShowCommand{
   120  				Meta: meta,
   121  			}, nil
   122  		},
   123  
   124  		"taint": func() (cli.Command, error) {
   125  			return &command.TaintCommand{
   126  				Meta: meta,
   127  			}, nil
   128  		},
   129  
   130  		"validate": func() (cli.Command, error) {
   131  			return &command.ValidateCommand{
   132  				Meta: meta,
   133  			}, nil
   134  		},
   135  
   136  		"version": func() (cli.Command, error) {
   137  			return &command.VersionCommand{
   138  				Meta:              meta,
   139  				Revision:          GitCommit,
   140  				Version:           Version,
   141  				VersionPrerelease: VersionPrerelease,
   142  				CheckFunc:         commandVersionCheck,
   143  			}, nil
   144  		},
   145  
   146  		"untaint": func() (cli.Command, error) {
   147  			return &command.UntaintCommand{
   148  				Meta: meta,
   149  			}, nil
   150  		},
   151  
   152  		//-----------------------------------------------------------
   153  		// Plumbing
   154  		//-----------------------------------------------------------
   155  
   156  		"state": func() (cli.Command, error) {
   157  			return &command.StateCommand{
   158  				Meta: meta,
   159  			}, nil
   160  		},
   161  
   162  		"state list": func() (cli.Command, error) {
   163  			return &command.StateListCommand{
   164  				Meta: meta,
   165  			}, nil
   166  		},
   167  
   168  		"state show": func() (cli.Command, error) {
   169  			return &command.StateShowCommand{
   170  				Meta: meta,
   171  			}, nil
   172  		},
   173  	}
   174  }
   175  
   176  // makeShutdownCh creates an interrupt listener and returns a channel.
   177  // A message will be sent on the channel for every interrupt received.
   178  func makeShutdownCh() <-chan struct{} {
   179  	resultCh := make(chan struct{})
   180  
   181  	signalCh := make(chan os.Signal, 4)
   182  	signal.Notify(signalCh, os.Interrupt)
   183  	go func() {
   184  		for {
   185  			<-signalCh
   186  			resultCh <- struct{}{}
   187  		}
   188  	}()
   189  
   190  	return resultCh
   191  }