diff --git a/command/job_revert.go b/command/job_revert.go index 819638fea..9226f36e2 100644 --- a/command/job_revert.go +++ b/command/job_revert.go @@ -12,7 +12,7 @@ type JobRevertCommand struct { func (c *JobRevertCommand) Help() string { helpText := ` -Usage: nomad job revert [options] +Usage: nomad job revert [options] Revert is used to revert a job to a prior version of the job. The available versions to revert to can be found using "nomad job history" command. @@ -21,10 +21,7 @@ General Options: ` + generalOptionsUsage() + ` -History Options: - - -job-version - Revert to the given job version. +Revert Options: -detach Return immediately instead of entering monitor mode. After job dispatch, @@ -43,11 +40,9 @@ func (c *JobRevertCommand) Synopsis() string { func (c *JobRevertCommand) Run(args []string) int { var detach, verbose bool - var versionStr string - flags := c.Meta.FlagSet("job history", FlagSetClient) + flags := c.Meta.FlagSet("job revert", FlagSetClient) flags.Usage = func() { c.Ui.Output(c.Help()) } - flags.StringVar(&versionStr, "job-version", "", "") flags.BoolVar(&detach, "detach", false, "") flags.BoolVar(&verbose, "verbose", false, "") @@ -61,9 +56,9 @@ func (c *JobRevertCommand) Run(args []string) int { length = fullId } - // Check that we got exactly one node + // Check that we got two args args = flags.Args() - if l := len(args); l < 1 || l > 2 { + if l := len(args); l != 2 { c.Ui.Error(c.Help()) return 1 } @@ -76,6 +71,15 @@ func (c *JobRevertCommand) Run(args []string) int { } jobID := args[0] + revertVersion, ok, err := parseVersion(args[1]) + if !ok { + c.Ui.Error("The job version to revert to must be specified using the -job-version flag") + return 1 + } + if err != nil { + c.Ui.Error(fmt.Sprintf("Failed to parse job-version flag: %v", err)) + return 1 + } // Check if the job exists jobs, _, err := client.Jobs().PrefixList(jobID) @@ -92,16 +96,6 @@ func (c *JobRevertCommand) Run(args []string) int { return 0 } - revertVersion, ok, err := parseVersion(versionStr) - if !ok { - c.Ui.Error("The job version to revert to must be specified using the -job-version flag") - return 1 - } - if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to parse job-version flag: %v", err)) - return 1 - } - // Prefix lookup matched a single job resp, _, err := client.Jobs().Revert(jobs[0].ID, revertVersion, nil, nil) if err != nil { diff --git a/vendor/github.com/mitchellh/cli/cli.go b/vendor/github.com/mitchellh/cli/cli.go index 350575e64..4a69d176d 100644 --- a/vendor/github.com/mitchellh/cli/cli.go +++ b/vendor/github.com/mitchellh/cli/cli.go @@ -120,7 +120,13 @@ func (c *CLI) Run() (int, error) { // Just show the version and exit if instructed. if c.IsVersion() && c.Version != "" { c.HelpWriter.Write([]byte(c.Version + "\n")) - return 1, nil + return 0, nil + } + + // Just print the help when only '-h' or '--help' is passed. + if c.IsHelp() && c.Subcommand() == "" { + c.HelpWriter.Write([]byte(c.HelpFunc(c.Commands) + "\n")) + return 0, nil } // Attempt to get the factory function for creating the command @@ -133,13 +139,13 @@ func (c *CLI) Run() (int, error) { command, err := raw.(CommandFactory)() if err != nil { - return 0, err + return 1, err } // If we've been instructed to just print the help, then print it if c.IsHelp() { c.commandHelp(command) - return 1, nil + return 0, nil } // If there is an invalid flag, then error diff --git a/vendor/github.com/mitchellh/cli/ui_mock.go b/vendor/github.com/mitchellh/cli/ui_mock.go index c46772855..bdae2a664 100644 --- a/vendor/github.com/mitchellh/cli/ui_mock.go +++ b/vendor/github.com/mitchellh/cli/ui_mock.go @@ -7,12 +7,25 @@ import ( "sync" ) -// MockUi is a mock UI that is used for tests and is exported publicly for -// use in external tests if needed as well. +// NewMockUi returns a fully initialized MockUi instance +// which is safe for concurrent use. +func NewMockUi() *MockUi { + m := new(MockUi) + m.once.Do(m.init) + return m +} + +// MockUi is a mock UI that is used for tests and is exported publicly +// for use in external tests if needed as well. Do not instantite this +// directly since the buffers will be initialized on the first write. If +// there is no write then you will get a nil panic. Please use the +// NewMockUi() constructor function instead. You can fix your code with +// +// sed -i -e 's/new(cli.MockUi)/cli.NewMockUi()/g' *_test.go type MockUi struct { InputReader io.Reader - ErrorWriter *bytes.Buffer - OutputWriter *bytes.Buffer + ErrorWriter *syncBuffer + OutputWriter *syncBuffer once sync.Once } @@ -59,6 +72,36 @@ func (u *MockUi) Warn(message string) { } func (u *MockUi) init() { - u.ErrorWriter = new(bytes.Buffer) - u.OutputWriter = new(bytes.Buffer) + u.ErrorWriter = new(syncBuffer) + u.OutputWriter = new(syncBuffer) +} + +type syncBuffer struct { + sync.RWMutex + b bytes.Buffer +} + +func (b *syncBuffer) Write(data []byte) (int, error) { + b.Lock() + defer b.Unlock() + return b.b.Write(data) +} + +func (b *syncBuffer) Read(data []byte) (int, error) { + b.RLock() + defer b.RUnlock() + return b.b.Read(data) +} + +func (b *syncBuffer) Reset() { + b.Lock() + b.b.Reset() + b.Unlock() +} + +func (b *syncBuffer) String() string { + b.RLock() + data := b.b.Bytes() + b.RUnlock() + return string(data) } diff --git a/vendor/vendor.json b/vendor/vendor.json index 01f87fc24..6920b7b8b 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -960,10 +960,10 @@ "revision": "7e024ce8ce18b21b475ac6baf8fa3c42536bf2fa" }, { - "checksumSHA1": "PfnjkP75J4WIemUueJcrH2VbRHY=", + "checksumSHA1": "bUuI7AVR3IZPLlBaEKmw/ke7wqA=", "path": "github.com/mitchellh/cli", - "revision": "8d6d9ab3c912dcb005ece87c40a41b9e73e1999a", - "revisionTime": "2017-03-03T02:36:54Z" + "revision": "b481eac70eea3ad671b7c360a013f89bb759b252", + "revisionTime": "2017-05-23T17:27:49Z" }, { "checksumSHA1": "ttEN1Aupb7xpPMkQLqb3tzLFdXs=",