github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/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  		"graph": func() (cli.Command, error) {
    44  			return &command.GraphCommand{
    45  				Meta: meta,
    46  			}, nil
    47  		},
    48  
    49  		"output": func() (cli.Command, error) {
    50  			return &command.OutputCommand{
    51  				Meta: meta,
    52  			}, nil
    53  		},
    54  
    55  		"plan": func() (cli.Command, error) {
    56  			return &command.PlanCommand{
    57  				Meta: meta,
    58  			}, nil
    59  		},
    60  
    61  		"refresh": func() (cli.Command, error) {
    62  			return &command.RefreshCommand{
    63  				Meta: meta,
    64  			}, nil
    65  		},
    66  
    67  		"show": func() (cli.Command, error) {
    68  			return &command.ShowCommand{
    69  				Meta: meta,
    70  			}, nil
    71  		},
    72  
    73  		"version": func() (cli.Command, error) {
    74  			return &command.VersionCommand{
    75  				Meta:              meta,
    76  				Revision:          GitCommit,
    77  				Version:           Version,
    78  				VersionPrerelease: VersionPrerelease,
    79  			}, nil
    80  		},
    81  	}
    82  }
    83  
    84  // makeShutdownCh creates an interrupt listener and returns a channel.
    85  // A message will be sent on the channel for every interrupt received.
    86  func makeShutdownCh() <-chan struct{} {
    87  	resultCh := make(chan struct{})
    88  
    89  	signalCh := make(chan os.Signal, 4)
    90  	signal.Notify(signalCh, os.Interrupt)
    91  	go func() {
    92  		for {
    93  			<-signalCh
    94  			resultCh <- struct{}{}
    95  		}
    96  	}()
    97  
    98  	return resultCh
    99  }