github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/supercommand.go (about)

     1  // Copyright 2012 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"runtime"
    10  
    11  	"github.com/juju/cmd"
    12  	"github.com/juju/loggo"
    13  	"github.com/juju/utils/arch"
    14  	"github.com/juju/utils/series"
    15  	"github.com/juju/version"
    16  
    17  	"github.com/juju/juju/juju/osenv"
    18  	jujuversion "github.com/juju/juju/version"
    19  )
    20  
    21  func init() {
    22  	// If the environment key is empty, ConfigureLoggers returns nil and does
    23  	// nothing.
    24  	err := loggo.ConfigureLoggers(os.Getenv(osenv.JujuStartupLoggingConfigEnvKey))
    25  	if err != nil {
    26  		fmt.Fprintf(os.Stderr, "ERROR parsing %s: %s\n\n", osenv.JujuStartupLoggingConfigEnvKey, err)
    27  	}
    28  }
    29  
    30  var logger = loggo.GetLogger("juju.cmd")
    31  
    32  // NewSuperCommand is like cmd.NewSuperCommand but
    33  // it adds juju-specific functionality:
    34  // - The default logging configuration is taken from the environment;
    35  // - The version is configured to the current juju version;
    36  // - The command emits a log message when a command runs.
    37  func NewSuperCommand(p cmd.SuperCommandParams) *cmd.SuperCommand {
    38  	p.Log = &cmd.Log{
    39  		DefaultConfig: os.Getenv(osenv.JujuLoggingConfigEnvKey),
    40  	}
    41  	current := version.Binary{
    42  		Number: jujuversion.Current,
    43  		Arch:   arch.HostArch(),
    44  		Series: series.HostSeries(),
    45  	}
    46  
    47  	// p.Version should be a version.Binary, but juju/cmd does not
    48  	// import juju/juju/version so this cannot happen. We have
    49  	// tests to assert that this string value is correct.
    50  	p.Version = current.String()
    51  	p.NotifyRun = runNotifier
    52  	return cmd.NewSuperCommand(p)
    53  }
    54  
    55  // NewSubSuperCommand should be used to create a SuperCommand
    56  // that runs as a subcommand of some other SuperCommand.
    57  func NewSubSuperCommand(p cmd.SuperCommandParams) *cmd.SuperCommand {
    58  	p.NotifyRun = runNotifier
    59  	return cmd.NewSuperCommand(p)
    60  }
    61  
    62  func runNotifier(name string) {
    63  	logger.Infof("running %s [%s %s %s]", name, jujuversion.Current, runtime.Compiler, runtime.Version())
    64  	logger.Debugf("  args: %#v", os.Args)
    65  }