Skip to content

Commit

Permalink
Ui hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 27, 2014
1 parent 501f926 commit 01319e1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
20 changes: 20 additions & 0 deletions command/hook_ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package command

import (
"fmt"

"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)

type UiHook struct {
terraform.NilHook

Ui cli.Ui
}

func (h *UiHook) PreRefresh(
id string, s *terraform.ResourceState) (terraform.HookAction, error) {
h.Ui.Output(fmt.Sprintf("Refreshing state for %s (ID: %s)", id, s.ID))
return terraform.HookActionContinue, nil
}
11 changes: 7 additions & 4 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
// Commands is the mapping of all the available Terraform commands.
var Commands map[string]cli.CommandFactory

// Ui is the cli.Ui used for communicating to the outside world.
var Ui cli.Ui

const ErrorPrefix = "e:"
const OutputPrefix = "o:"

func init() {
ui := &cli.PrefixedUi{
Ui = &cli.PrefixedUi{
AskPrefix: OutputPrefix,
OutputPrefix: OutputPrefix,
InfoPrefix: OutputPrefix,
Expand All @@ -26,14 +29,14 @@ func init() {
"apply": func() (cli.Command, error) {
return &command.ApplyCommand{
TFConfig: &TFConfig,
Ui: ui,
Ui: Ui,
}, nil
},

"plan": func() (cli.Command, error) {
return &command.PlanCommand{
TFConfig: &TFConfig,
Ui: ui,
Ui: Ui,
}, nil
},

Expand All @@ -42,7 +45,7 @@ func init() {
Revision: GitCommit,
Version: Version,
VersionPrerelease: VersionPrerelease,
Ui: ui,
Ui: Ui,
}, nil
},
}
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"log"
"os"

"github.com/hashicorp/terraform/command"
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
"github.com/mitchellh/panicwrap"
"github.com/mitchellh/prefixedio"
Expand Down Expand Up @@ -84,6 +86,7 @@ func wrappedMain() int {
defer plugin.CleanupClients()

// Initialize the TFConfig settings for the commands...
TFConfig.Hooks = []terraform.Hook{&command.UiHook{Ui: Ui}}
TFConfig.Providers = config.ProviderFactories()

// Get the command line args. We shortcut "--version" and "-v" to
Expand Down
8 changes: 4 additions & 4 deletions terraform/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ const (
// nothing. Then, override only the functions you want to implement.
type Hook interface {
// PreRefresh is called before a resource is refreshed.
PreRefresh(*ResourceState) (HookAction, error)
PreRefresh(string, *ResourceState) (HookAction, error)

// PostRefresh is called after a resource is refreshed.
PostRefresh(*ResourceState) (HookAction, error)
PostRefresh(string, *ResourceState) (HookAction, error)
}

// NilHook is a Hook implementation that does nothing. It exists only to
// simplify implementing hooks. You can embed this into your Hook implementation
// and only implement the functions you are interested in.
type NilHook struct{}

func (*NilHook) PreRefresh(*ResourceState) (HookAction, error) {
func (*NilHook) PreRefresh(string, *ResourceState) (HookAction, error) {
return HookActionContinue, nil
}

func (*NilHook) PostRefresh(*ResourceState) (HookAction, error) {
func (*NilHook) PostRefresh(string, *ResourceState) (HookAction, error) {
return HookActionContinue, nil
}
8 changes: 6 additions & 2 deletions terraform/hook_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ package terraform
// It records all of its function calls.
type MockHook struct {
PostRefreshCalled bool
PostRefreshId string
PostRefreshState *ResourceState
PostRefreshReturn HookAction
PostRefreshError error

PreRefreshCalled bool
PreRefreshId string
PreRefreshState *ResourceState
PreRefreshReturn HookAction
PreRefreshError error
}

func (h *MockHook) PreRefresh(s *ResourceState) (HookAction, error) {
func (h *MockHook) PreRefresh(n string, s *ResourceState) (HookAction, error) {
h.PreRefreshCalled = true
h.PreRefreshId = n
h.PreRefreshState = s
return h.PreRefreshReturn, h.PreRefreshError
}

func (h *MockHook) PostRefresh(s *ResourceState) (HookAction, error) {
func (h *MockHook) PostRefresh(n string, s *ResourceState) (HookAction, error) {
h.PostRefreshCalled = true
h.PostRefreshId = n
h.PostRefreshState = s
return h.PostRefreshReturn, h.PostRefreshError
}
4 changes: 2 additions & 2 deletions terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (t *Terraform) refreshWalkFn(result *State) depgraph.WalkFunc {
cb := func(r *Resource) (map[string]string, error) {
for _, h := range t.hooks {
// TODO: return value
h.PreRefresh(r.State)
h.PreRefresh(r.Id, r.State)
}

rs, err := r.Provider.Refresh(r.State)
Expand All @@ -153,7 +153,7 @@ func (t *Terraform) refreshWalkFn(result *State) depgraph.WalkFunc {

for _, h := range t.hooks {
// TODO: return value
h.PostRefresh(rs)
h.PostRefresh(r.Id, rs)
}

return nil, nil
Expand Down

0 comments on commit 01319e1

Please sign in to comment.