github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/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 ErrorPrefix = "e:"
    18  const OutputPrefix = "o:"
    19  
    20  func init() {
    21  	Ui = &cli.PrefixedUi{
    22  		AskPrefix:    OutputPrefix,
    23  		OutputPrefix: OutputPrefix,
    24  		InfoPrefix:   OutputPrefix,
    25  		ErrorPrefix:  ErrorPrefix,
    26  		Ui:           &cli.BasicUi{Writer: os.Stdout},
    27  	}
    28  
    29  	meta := command.Meta{
    30  		Color:       true,
    31  		ContextOpts: &ContextOpts,
    32  		Ui:          Ui,
    33  	}
    34  
    35  	Commands = map[string]cli.CommandFactory{
    36  		"apply": func() (cli.Command, error) {
    37  			return &command.ApplyCommand{
    38  				Meta:       meta,
    39  				ShutdownCh: makeShutdownCh(),
    40  			}, nil
    41  		},
    42  
    43  		"destroy": func() (cli.Command, error) {
    44  			return &command.ApplyCommand{
    45  				Meta:       meta,
    46  				Destroy:    true,
    47  				ShutdownCh: makeShutdownCh(),
    48  			}, nil
    49  		},
    50  
    51  		"get": func() (cli.Command, error) {
    52  			return &command.GetCommand{
    53  				Meta: meta,
    54  			}, nil
    55  		},
    56  
    57  		"graph": func() (cli.Command, error) {
    58  			return &command.GraphCommand{
    59  				Meta: meta,
    60  			}, nil
    61  		},
    62  
    63  		"init": func() (cli.Command, error) {
    64  			return &command.InitCommand{
    65  				Meta: meta,
    66  			}, nil
    67  		},
    68  
    69  		"output": func() (cli.Command, error) {
    70  			return &command.OutputCommand{
    71  				Meta: meta,
    72  			}, nil
    73  		},
    74  
    75  		"plan": func() (cli.Command, error) {
    76  			return &command.PlanCommand{
    77  				Meta: meta,
    78  			}, nil
    79  		},
    80  
    81  		"refresh": func() (cli.Command, error) {
    82  			return &command.RefreshCommand{
    83  				Meta: meta,
    84  			}, nil
    85  		},
    86  
    87  		"show": func() (cli.Command, error) {
    88  			return &command.ShowCommand{
    89  				Meta: meta,
    90  			}, nil
    91  		},
    92  
    93  		"version": func() (cli.Command, error) {
    94  			return &command.VersionCommand{
    95  				Meta:              meta,
    96  				Revision:          GitCommit,
    97  				Version:           Version,
    98  				VersionPrerelease: VersionPrerelease,
    99  				CheckFunc:         commandVersionCheck,
   100  			}, nil
   101  		},
   102  	}
   103  }
   104  
   105  // makeShutdownCh creates an interrupt listener and returns a channel.
   106  // A message will be sent on the channel for every interrupt received.
   107  func makeShutdownCh() <-chan struct{} {
   108  	resultCh := make(chan struct{})
   109  
   110  	signalCh := make(chan os.Signal, 4)
   111  	signal.Notify(signalCh, os.Interrupt)
   112  	go func() {
   113  		for {
   114  			<-signalCh
   115  			resultCh <- struct{}{}
   116  		}
   117  	}()
   118  
   119  	return resultCh
   120  }