Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command: discard output from flags package and return errs directly #22373

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions command/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"bufio"
"fmt"
"strings"

"github.com/hashicorp/terraform/addrs"
Expand Down Expand Up @@ -29,6 +30,7 @@ func (c *ConsoleCommand) Run(args []string) int {
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (c *FmtCommand) Run(args []string) int {
cmdFlags.BoolVar(&c.recursive, "recursive", false, "recursive")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
2 changes: 2 additions & 0 deletions command/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/tfdiags"
Expand All @@ -24,6 +25,7 @@ func (c *GetCommand) Run(args []string) int {
cmdFlags.BoolVar(&update, "update", false, "update")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (c *GraphCommand) Run(args []string) int {
cmdFlags.BoolVar(&verbose, "verbose", false, "verbose")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
23 changes: 1 addition & 22 deletions command/meta.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package command

import (
"bufio"
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -353,26 +351,7 @@ func (m *Meta) contextOpts() *terraform.ContextOpts {
// defaultFlagSet creates a default flag set for commands.
func (m *Meta) defaultFlagSet(n string) *flag.FlagSet {
f := flag.NewFlagSet(n, flag.ContinueOnError)

// Create an io.Writer that writes to our Ui properly for errors.
// This is kind of a hack, but it does the job. Basically: create
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 deletion of code that says This is kind of a hack

// a pipe, use a scanner to break it into lines, and output each line
// to the UI. Do this forever.
errR, errW := io.Pipe()
errScanner := bufio.NewScanner(errR)
go func() {
// This only needs to be alive long enough to write the help info if
// there is a flag error. Kill the scanner after a short duriation to
// prevent these from accumulating during tests, and cluttering up the
// stack traces.
time.AfterFunc(2*time.Second, func() {
errW.Close()
})
for errScanner.Scan() {
m.Ui.Error(errScanner.Text())
}
}()
f.SetOutput(errW)
f.SetOutput(ioutil.Discard)

// Set the default Usage to empty
f.Usage = func() {}
Expand Down
1 change: 1 addition & 0 deletions command/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (c *OutputCommand) Run(args []string) int {
cmdFlags.StringVar(&module, "module", "", "module")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (c *ProvidersCommand) Run(args []string) int {
cmdFlags := c.Meta.defaultFlagSet("providers")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/providers_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (c *ProvidersSchemaCommand) Run(args []string) int {

cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (c *ShowCommand) Run(args []string) int {
cmdFlags.BoolVar(&jsonOutput, "json", false, "produce JSON output")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/state_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (c *StateListCommand) Run(args []string) int {
cmdFlags.StringVar(&statePath, "state", "", "path")
lookupId := cmdFlags.String("id", "", "Restrict output to paths with a resource having the specified ID.")
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return cli.RunResultHelp
}
args = cmdFlags.Args()
Expand Down
3 changes: 2 additions & 1 deletion command/state_mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (c *StateMvCommand) Run(args []string) int {
cmdFlags.StringVar(&c.statePath, "state", "", "path")
cmdFlags.StringVar(&statePathOut, "state-out", "", "path")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder what RunResultHelp was (and if it is useful, or if it remains useful?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, it took me awhile to track this down.

This is an exit code from "github.com/mitchellh/cli". When it's used as an exit code, the cli package prints the usage text before exiting. It's used in other tf commands, but in this case (because cmdFlags.Parse also prints the usage) it resulted in the help/usage text getting printed twice.

I doubly appreciate the question because when I went back to check something I found the last bit of ~magic wherein the flags package was printing the usage, thanks! 👏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the info, and glad it helped! 🙌

c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}
args = cmdFlags.Args()
if len(args) != 2 {
Expand Down
4 changes: 2 additions & 2 deletions command/state_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform/states/statefile"
"github.com/hashicorp/terraform/states/statemgr"
"github.com/mitchellh/cli"
)

// StatePullCommand is a Command implementation that shows a single resource.
Expand All @@ -24,7 +23,8 @@ func (c *StatePullCommand) Run(args []string) int {

cmdFlags := c.Meta.defaultFlagSet("state pull")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}
args = cmdFlags.Args()

Expand Down
3 changes: 2 additions & 1 deletion command/state_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (c *StatePushCommand) Run(args []string) int {
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}
args = cmdFlags.Args()

Expand Down
3 changes: 2 additions & 1 deletion command/state_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func (c *StateRmCommand) Run(args []string) int {
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
cmdFlags.StringVar(&c.statePath, "state", "", "path")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

args = cmdFlags.Args()
Expand Down
3 changes: 2 additions & 1 deletion command/state_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func (c *StateShowCommand) Run(args []string) int {
cmdFlags := c.Meta.defaultFlagSet("state show")
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}
args = cmdFlags.Args()
if len(args) != 1 {
Expand Down
1 change: 1 addition & 0 deletions command/taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (c *TaintCommand) Run(args []string) int {
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c *UnlockCommand) Run(args []string) int {
cmdFlags.BoolVar(&force, "force", false, "force")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/untaint.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (c *UntaintCommand) Run(args []string) int {
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (c *ValidateCommand) Run(args []string) int {
cmdFlags.Var(varFiles, "var-file", "variable file")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/workspace_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
cmdFlags.DurationVar(&stateLockTimeout, "lock-timeout", 0, "lock timeout")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
2 changes: 2 additions & 0 deletions command/workspace_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"bytes"
"fmt"
"strings"

"github.com/hashicorp/terraform/tfdiags"
Expand All @@ -24,6 +25,7 @@ func (c *WorkspaceListCommand) Run(args []string) int {
cmdFlags := c.Meta.defaultFlagSet("workspace list")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/workspace_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (c *WorkspaceNewCommand) Run(args []string) int {
cmdFlags.StringVar(&statePath, "state", "", "terraform state file")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
1 change: 1 addition & 0 deletions command/workspace_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (c *WorkspaceSelectCommand) Run(args []string) int {
cmdFlags := c.Meta.defaultFlagSet("workspace select")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down
2 changes: 2 additions & 0 deletions command/workspace_show.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"fmt"
"strings"

"github.com/posener/complete"
Expand All @@ -19,6 +20,7 @@ func (c *WorkspaceShowCommand) Run(args []string) int {
cmdFlags := c.Meta.extendedFlagSet("workspace show")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}

Expand Down