-
Notifications
You must be signed in to change notification settings - Fork 0
/
supercommand.go
72 lines (61 loc) · 2.11 KB
/
supercommand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2012 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cmd
import (
"fmt"
"os"
"runtime"
"golang.org/x/crypto/ssh/terminal"
jujuversion "github.com/juju/juju/core/version"
"github.com/juju/juju/internal/cmd"
internallogger "github.com/juju/juju/internal/logger"
"github.com/juju/juju/juju/osenv"
)
func init() {
// If the environment key is empty, ConfigureLoggers returns nil and does
// nothing.
err := internallogger.ConfigureLoggers(os.Getenv(osenv.JujuStartupLoggingConfigEnvKey))
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR parsing %s: %s\n\n", osenv.JujuStartupLoggingConfigEnvKey, err)
}
}
var logger = internallogger.GetLogger("juju.cmd")
// DefaultLog is the default command logging implementation.
var DefaultLog = &cmd.Log{
DefaultConfig: os.Getenv(osenv.JujuLoggingConfigEnvKey),
}
// NewSuperCommand is like cmd.NewSuperCommand but
// it adds juju-specific functionality:
// - The default logging configuration is taken from the environment;
// - The version is configured to the current juju version;
// - The additional version information is sourced from juju/juju/version;
// - The command emits a log message when a command runs.
func NewSuperCommand(p cmd.SuperCommandParams) *cmd.SuperCommand {
if p.NotifyRun != nil {
messenger := p.NotifyRun
p.NotifyRun = func(str string) {
messenger(str)
runNotifier(str)
}
} else {
p.NotifyRun = runNotifier
}
p.FlagKnownAs = "option"
return cmd.NewSuperCommand(p)
}
func runNotifier(name string) {
logger.Infof("running %s [%s %s %s %s]", name, jujuversion.Current, jujuversion.GitCommit, runtime.Compiler, runtime.Version())
logger.Debugf(" args: %#v", os.Args)
}
func Info(i *cmd.Info) *cmd.Info {
info := *i
info.FlagKnownAs = "option"
info.ShowSuperFlags = []string{"show-log", "debug", "logging-config", "verbose", "quiet", "h", "help"}
return &info
}
// IsPiped determines if the command was used in a pipe and,
// hence, it's stdin is not usable for user input.
func IsPiped(ctx *cmd.Context) bool {
stdIn, ok := ctx.Stdin.(*os.File)
return ok && !terminal.IsTerminal(int(stdIn.Fd()))
}