github.com/wangzhucn/terraform@v0.6.7-0.20151109233120-4eea011b56b3/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 "version": func() (cli.Command, error) { 114 return &command.VersionCommand{ 115 Meta: meta, 116 Revision: GitCommit, 117 Version: Version, 118 VersionPrerelease: VersionPrerelease, 119 CheckFunc: commandVersionCheck, 120 }, nil 121 }, 122 } 123 } 124 125 // makeShutdownCh creates an interrupt listener and returns a channel. 126 // A message will be sent on the channel for every interrupt received. 127 func makeShutdownCh() <-chan struct{} { 128 resultCh := make(chan struct{}) 129 130 signalCh := make(chan os.Signal, 4) 131 signal.Notify(signalCh, os.Interrupt) 132 go func() { 133 for { 134 <-signalCh 135 resultCh <- struct{}{} 136 } 137 }() 138 139 return resultCh 140 }