github.com/haklop/terraform@v0.3.6/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  		"pull": func() (cli.Command, error) {
    82  			return &command.PullCommand{
    83  				Meta: meta,
    84  			}, nil
    85  		},
    86  
    87  		"push": func() (cli.Command, error) {
    88  			return &command.PushCommand{
    89  				Meta: meta,
    90  			}, nil
    91  		},
    92  
    93  		"refresh": func() (cli.Command, error) {
    94  			return &command.RefreshCommand{
    95  				Meta: meta,
    96  			}, nil
    97  		},
    98  
    99  		"remote": func() (cli.Command, error) {
   100  			return &command.RemoteCommand{
   101  				Meta: meta,
   102  			}, nil
   103  		},
   104  
   105  		"show": func() (cli.Command, error) {
   106  			return &command.ShowCommand{
   107  				Meta: meta,
   108  			}, nil
   109  		},
   110  
   111  		"version": func() (cli.Command, error) {
   112  			return &command.VersionCommand{
   113  				Meta:              meta,
   114  				Revision:          GitCommit,
   115  				Version:           Version,
   116  				VersionPrerelease: VersionPrerelease,
   117  				CheckFunc:         commandVersionCheck,
   118  			}, nil
   119  		},
   120  	}
   121  }
   122  
   123  // makeShutdownCh creates an interrupt listener and returns a channel.
   124  // A message will be sent on the channel for every interrupt received.
   125  func makeShutdownCh() <-chan struct{} {
   126  	resultCh := make(chan struct{})
   127  
   128  	signalCh := make(chan os.Signal, 4)
   129  	signal.Notify(signalCh, os.Interrupt)
   130  	go func() {
   131  		for {
   132  			<-signalCh
   133  			resultCh <- struct{}{}
   134  		}
   135  	}()
   136  
   137  	return resultCh
   138  }