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