github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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  		"fmt": func() (cli.Command, error) {
    54  			return &command.FmtCommand{
    55  				Meta: meta,
    56  			}, nil
    57  		},
    58  
    59  		"get": func() (cli.Command, error) {
    60  			return &command.GetCommand{
    61  				Meta: meta,
    62  			}, nil
    63  		},
    64  
    65  		"graph": func() (cli.Command, error) {
    66  			return &command.GraphCommand{
    67  				Meta: meta,
    68  			}, nil
    69  		},
    70  
    71  		"init": func() (cli.Command, error) {
    72  			return &command.InitCommand{
    73  				Meta: meta,
    74  			}, nil
    75  		},
    76  
    77  		"output": func() (cli.Command, error) {
    78  			return &command.OutputCommand{
    79  				Meta: meta,
    80  			}, nil
    81  		},
    82  
    83  		"plan": func() (cli.Command, error) {
    84  			return &command.PlanCommand{
    85  				Meta: meta,
    86  			}, nil
    87  		},
    88  
    89  		"push": func() (cli.Command, error) {
    90  			return &command.PushCommand{
    91  				Meta: meta,
    92  			}, nil
    93  		},
    94  
    95  		"refresh": func() (cli.Command, error) {
    96  			return &command.RefreshCommand{
    97  				Meta: meta,
    98  			}, nil
    99  		},
   100  
   101  		"remote": func() (cli.Command, error) {
   102  			return &command.RemoteCommand{
   103  				Meta: meta,
   104  			}, nil
   105  		},
   106  
   107  		"show": func() (cli.Command, error) {
   108  			return &command.ShowCommand{
   109  				Meta: meta,
   110  			}, nil
   111  		},
   112  
   113  		"taint": func() (cli.Command, error) {
   114  			return &command.TaintCommand{
   115  				Meta: meta,
   116  			}, nil
   117  		},
   118  
   119  		"validate": func() (cli.Command, error) {
   120  			return &command.ValidateCommand{
   121  				Meta: meta,
   122  			}, nil
   123  		},
   124  
   125  		"version": func() (cli.Command, error) {
   126  			return &command.VersionCommand{
   127  				Meta:              meta,
   128  				Revision:          GitCommit,
   129  				Version:           Version,
   130  				VersionPrerelease: VersionPrerelease,
   131  				CheckFunc:         commandVersionCheck,
   132  			}, nil
   133  		},
   134  
   135  		"untaint": func() (cli.Command, error) {
   136  			return &command.UntaintCommand{
   137  				Meta: meta,
   138  			}, nil
   139  		},
   140  	}
   141  }
   142  
   143  // makeShutdownCh creates an interrupt listener and returns a channel.
   144  // A message will be sent on the channel for every interrupt received.
   145  func makeShutdownCh() <-chan struct{} {
   146  	resultCh := make(chan struct{})
   147  
   148  	signalCh := make(chan os.Signal, 4)
   149  	signal.Notify(signalCh, os.Interrupt)
   150  	go func() {
   151  		for {
   152  			<-signalCh
   153  			resultCh <- struct{}{}
   154  		}
   155  	}()
   156  
   157  	return resultCh
   158  }