From 947209a0286c3fa6cb047fc39eb7b9d8999a377c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 May 2013 14:45:17 -0700 Subject: [PATCH] packer: PACKER_LOG to toggle logging from an env var [GH-3] --- packer.go | 9 +++++++++ packer/environment.go | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/packer.go b/packer.go index 630b5cf75..d63eeb0e5 100644 --- a/packer.go +++ b/packer.go @@ -5,12 +5,21 @@ import ( "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer/plugin" "fmt" + "io/ioutil" "log" "os" "os/exec" ) func main() { + if os.Getenv("PACKER_LOG") == "" { + // If we don't have logging explicitly enabled, then disable it + log.SetOutput(ioutil.Discard) + } else { + // Logging is enabled, make sure it goes to stderr + log.SetOutput(os.Stderr) + } + defer plugin.CleanupClients() commands := map[string]string { diff --git a/packer/environment.go b/packer/environment.go index a6f9694d0..4dd3401e0 100644 --- a/packer/environment.go +++ b/packer/environment.go @@ -4,6 +4,7 @@ package packer import ( "errors" "fmt" + "log" "os" "sort" "strings" @@ -87,6 +88,8 @@ func (e *coreEnvironment) Builder(name string) (b Builder, err error) { // Executes a command as if it was typed on the command-line interface. // The return value is the exit code of the command. func (e *coreEnvironment) Cli(args []string) (result int, err error) { + log.Printf("Environment.Cli: %#v\n", args) + if len(args) == 0 || args[0] == "--help" || args[0] == "-h" { e.printHelp() return 1, nil @@ -115,6 +118,7 @@ func (e *coreEnvironment) Cli(args []string) (result int, err error) { // If we still don't have a command, show the help. if command == nil { + log.Printf("Environment.CLI: command not found: %s\n", args[0]) e.printHelp() return 1, nil }