This commit is contained in:
Alex Dadgar
2018-03-21 11:18:26 -07:00
parent d39b13ed58
commit 148a9504e7
4 changed files with 61 additions and 4 deletions

View File

@@ -9,6 +9,11 @@ import (
"github.com/mitchellh/cli"
)
const (
// EnvNomadCLINoColor is an env var that toggles colored UI output.
EnvNomadCLINoColor = `NOMAD_CLI_NO_COLOR`
)
// DeprecatedCommand is a command that wraps an existing command and prints a
// deprecation notice and points the user to the new command. Deprecated
// commands are always hidden from help output.

View File

@@ -181,7 +181,8 @@ func generalOptionsUsage() string {
Defaults to the "default" namespace.
-no-color
Disables colored command output.
Disables colored command output. Alternatively, NOMAD_CLI_NO_COLOR may be
set.
-ca-cert=<path>
Path to a PEM encoded CA cert file to use to verify the

54
main.go
View File

@@ -13,6 +13,7 @@ import (
"github.com/hashicorp/nomad/version"
"github.com/mitchellh/cli"
"github.com/sean-/seed"
"golang.org/x/crypto/ssh/terminal"
)
var (
@@ -63,10 +64,39 @@ func main() {
}
func Run(args []string) int {
return RunCustom(args, command.Commands(nil))
return RunCustom(args)
}
func RunCustom(args []string, commands map[string]cli.CommandFactory) int {
func RunCustom(args []string) int {
// Parse flags into env vars for global use
args = setupEnv(args)
// Create the meta object
metaPtr := new(command.Meta)
// Don't use color if disabled
color := true
if os.Getenv(command.EnvNomadCLINoColor) != "" {
color = false
}
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
metaPtr.Ui = &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
// Only use colored UI if stdout is a tty, and not disabled
if isTerminal && color {
metaPtr.Ui = &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Ui: metaPtr.Ui,
}
}
commands := command.Commands(metaPtr)
cli := &cli.CLI{
Name: "nomad",
Version: version.GetVersion().FullVersionNumber(true),
@@ -78,6 +108,7 @@ func RunCustom(args []string, commands map[string]cli.CommandFactory) int {
HelpFunc: groupedHelpFunc(
cli.BasicHelpFunc("nomad"),
),
HelpWriter: os.Stdout,
}
exitCode, err := cli.Run()
@@ -177,3 +208,22 @@ func printCommand(w io.Writer, name string, cmdFn cli.CommandFactory) {
}
fmt.Fprintf(w, " %s\t%s\n", name, cmd.Synopsis())
}
// setupEnv parses args and may replace them and sets some env vars to known
// values based on format options
func setupEnv(args []string) []string {
noColor := false
for _, arg := range args {
// Check if color is set
if arg == "-no-color" || arg == "--no-color" {
noColor = true
}
}
// Put back into the env for later
if noColor {
os.Setenv(command.EnvNomadCLINoColor, "true")
}
return args
}

View File

@@ -5,7 +5,8 @@
Overrides the `NOMAD_REGION` environment variable if set. Defaults to the
Agent's local region.
- `-no-color`: Disables colored command output.
- `-no-color`: Disables colored command output. Alternatively,
`NOMAD_CLI_NO_COLOR` may be set.
- `-ca-cert=<path>`: Path to a PEM encoded CA cert file to use to verify the
Nomad server SSL certificate. Overrides the `NOMAD_CACERT` environment