github.com/mikesimons/terraform@v0.6.13-0.20160304043642-f11448c69214/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  
    14  // Ui is the cli.Ui used for communicating to the outside world.
    15  var Ui cli.Ui
    16  
    17  const (
    18  	ErrorPrefix  = "e:"
    19  	OutputPrefix = "o:"
    20  )
    21  
    22  func init() {
    23  	Ui = &cli.PrefixedUi{
    24  		AskPrefix:    OutputPrefix,
    25  		OutputPrefix: OutputPrefix,
    26  		InfoPrefix:   OutputPrefix,
    27  		ErrorPrefix:  ErrorPrefix,
    28  		Ui:           &cli.BasicUi{Writer: os.Stdout},
    29  	}
    30  
    31  	meta := command.Meta{
    32  		Color:       true,
    33  		ContextOpts: &ContextOpts,
    34  		Ui:          Ui,
    35  	}
    36  
    37  	Commands = map[string]cli.CommandFactory{
    38  		"apply": func() (cli.Command, error) {
    39  			return &command.ApplyCommand{
    40  				Meta:       meta,
    41  				ShutdownCh: makeShutdownCh(),
    42  			}, nil
    43  		},
    44  
    45  		"destroy": func() (cli.Command, error) {
    46  			return &command.ApplyCommand{
    47  				Meta:       meta,
    48  				Destroy:    true,
    49  				ShutdownCh: makeShutdownCh(),
    50  			}, nil
    51  		},
    52  
    53  		"get": func() (cli.Command, error) {
    54  			return &command.GetCommand{
    55  				Meta: meta,
    56  			}, nil
    57  		},
    58  
    59  		"graph": func() (cli.Command, error) {
    60  			return &command.GraphCommand{
    61  				Meta: meta,
    62  			}, nil
    63  		},
    64  
    65  		"init": func() (cli.Command, error) {
    66  			return &command.InitCommand{
    67  				Meta: meta,
    68  			}, nil
    69  		},
    70  
    71  		"output": func() (cli.Command, error) {
    72  			return &command.OutputCommand{
    73  				Meta: meta,
    74  			}, nil
    75  		},
    76  
    77  		"plan": func() (cli.Command, error) {
    78  			return &command.PlanCommand{
    79  				Meta: meta,
    80  			}, nil
    81  		},
    82  
    83  		"push": func() (cli.Command, error) {
    84  			return &command.PushCommand{
    85  				Meta: meta,
    86  			}, nil
    87  		},
    88  
    89  		"refresh": func() (cli.Command, error) {
    90  			return &command.RefreshCommand{
    91  				Meta: meta,
    92  			}, nil
    93  		},
    94  
    95  		"remote": func() (cli.Command, error) {
    96  			return &command.RemoteCommand{
    97  				Meta: meta,
    98  			}, nil
    99  		},
   100  
   101  		"show": func() (cli.Command, error) {
   102  			return &command.ShowCommand{
   103  				Meta: meta,
   104  			}, nil
   105  		},
   106  
   107  		"taint": func() (cli.Command, error) {
   108  			return &command.TaintCommand{
   109  				Meta: meta,
   110  			}, nil
   111  		},
   112  
   113  		"validate": func() (cli.Command, error) {
   114  			return &command.ValidateCommand{
   115  				Meta: meta,
   116  			}, nil
   117  		},
   118  
   119  		"version": func() (cli.Command, error) {
   120  			return &command.VersionCommand{
   121  				Meta:              meta,
   122  				Revision:          GitCommit,
   123  				Version:           Version,
   124  				VersionPrerelease: VersionPrerelease,
   125  				CheckFunc:         commandVersionCheck,
   126  			}, nil
   127  		},
   128  	}
   129  }
   130  
   131  // makeShutdownCh creates an interrupt listener and returns a channel.
   132  // A message will be sent on the channel for every interrupt received.
   133  func makeShutdownCh() <-chan struct{} {
   134  	resultCh := make(chan struct{})
   135  
   136  	signalCh := make(chan os.Signal, 4)
   137  	signal.Notify(signalCh, os.Interrupt)
   138  	go func() {
   139  		for {
   140  			<-signalCh
   141  			resultCh <- struct{}{}
   142  		}
   143  	}()
   144  
   145  	return resultCh
   146  }