Skip to content

Commit

Permalink
Restructure command arguments and options
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Sep 25, 2019
1 parent 00bdc08 commit 1499b73
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 47 deletions.
30 changes: 18 additions & 12 deletions README.md
Expand Up @@ -38,7 +38,7 @@ provider "aws" {
```

```
$ tfupdate terraform -v 0.12.8 -f main.tf
$ tfupdate terraform 0.12.8 main.tf
$ git diff
diff --git a/main.tf b/main.tf
Expand All @@ -59,7 +59,7 @@ $ git add . && git commit -m "Update terraform to v0.12.8"
```

```
$ tfupdate provider -v aws@2.28.0 -f ./
$ tfupdate provider aws@2.28.0 ./
$ git diff
diff --git a/main.tf b/main.tf
Expand All @@ -83,7 +83,7 @@ If you want to update all your Terraform configurations under the current direct
run a command like this:

```
$ tfupdate terraform -v 0.12.8 -f ./ -r
$ tfupdate terraform 0.12.8 ./ -r
```

# Usage
Expand All @@ -100,23 +100,29 @@ Available commands are:

```
$ tfupdate terraform --help
Usage: tfupdate terraform [options]
Usage: tfupdate terraform [options] <VERSION> <PATH>
Arguments
VERSION A new version constraint
PATH A path of file or directory to update
Options:
-v A new version constraint
-f A path of file or directory to update (default: ./)
-r Check a directory recursively (default: false)
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
```

```
$ tfupdate provider --help
Usage: tfupdate provider [options]
Usage: tfupdate provider [options] <PROVIER_NAME>@<VERSION> <PATH>
Arguments
PROVIER_NAME A name of provider (e.g. aws, google, azurerm)
VERSION A new version constraint
PATH A path of file or directory to update
Options:
-v A new version constraint.
The valid format is <PROVIER_NAME>@<VERSION>
-f A path of file or directory to update (default: ./)
-r Check a directory recursively (default: false)
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
```

# License
Expand Down
35 changes: 17 additions & 18 deletions command/provider.go
@@ -1,10 +1,11 @@
package command

import (
"flag"
"fmt"
"strings"

"github.com/minamijoyo/tfupdate/tfupdate"
flag "github.com/spf13/pflag"
)

// ProviderCommand is a command which update version constraints for provider.
Expand All @@ -19,26 +20,22 @@ type ProviderCommand struct {
// Run runs the procedure of this command.
func (c *ProviderCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("provider", flag.ContinueOnError)
cmdFlags.StringVar(&c.target, "v", "", "A new version constraint")
cmdFlags.StringVar(&c.path, "f", "./", "A path of file or directory to update")
cmdFlags.BoolVar(&c.recursive, "r", false, "Check a directory recursively")
cmdFlags.StringVar(&c.ignorePath, "ignore-path", "", "A regular expression for path to ignore")
cmdFlags.BoolVarP(&c.recursive, "recursive", "r", false, "Check a directory recursively")
cmdFlags.StringVarP(&c.ignorePath, "ignore-path", "i", "", "A regular expression for path to ignore")

if err := cmdFlags.Parse(args); err != nil {
c.UI.Error(fmt.Sprintf("failed to parse arguments: %s", err))
return 1
}

if len(cmdFlags.Args()) != 0 {
c.UI.Error("The provider command expects no arguments")
if len(cmdFlags.Args()) != 2 {
c.UI.Error(fmt.Sprintf("The command expects 2 arguments, but got %#v", cmdFlags.Args()))
c.UI.Error(c.Help())
return 1
}

if len(c.target) == 0 {
c.UI.Error("Argument error: -v is required\n")
c.UI.Error(c.Help())
return 1
}
c.target = cmdFlags.Arg(0)
c.path = cmdFlags.Arg(1)

option, err := tfupdate.NewOption("provider", c.target, c.recursive, c.ignorePath)
if err != nil {
Expand All @@ -58,14 +55,16 @@ func (c *ProviderCommand) Run(args []string) int {
// Help returns long-form help text.
func (c *ProviderCommand) Help() string {
helpText := `
Usage: tfupdate provider [options]
Usage: tfupdate provider [options] <PROVIER_NAME>@<VERSION> <PATH>
Arguments
PROVIER_NAME A name of provider (e.g. aws, google, azurerm)
VERSION A new version constraint
PATH A path of file or directory to update
Options:
-v A new version constraint.
The valid format is <PROVIER_NAME>@<VERSION>
-f A path of file or directory to update (default: ./)
-r Check a directory recursively (default: false)
--ignore-path A regular expression for path to ignore
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
`
return strings.TrimSpace(helpText)
}
Expand Down
33 changes: 16 additions & 17 deletions command/terraform.go
@@ -1,10 +1,11 @@
package command

import (
"flag"
"fmt"
"strings"

"github.com/minamijoyo/tfupdate/tfupdate"
flag "github.com/spf13/pflag"
)

// TerraformCommand is a command which update version constraints for terraform.
Expand All @@ -19,26 +20,22 @@ type TerraformCommand struct {
// Run runs the procedure of this command.
func (c *TerraformCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("terraform", flag.ContinueOnError)
cmdFlags.StringVar(&c.target, "v", "", "A new version constraint")
cmdFlags.StringVar(&c.path, "f", "./", "A path of file or directory to update")
cmdFlags.BoolVar(&c.recursive, "r", false, "Check a directory recursively")
cmdFlags.StringVar(&c.ignorePath, "ignore-path", "", "A regular expression for path to ignore")
cmdFlags.BoolVarP(&c.recursive, "recursive", "r", false, "Check a directory recursively")
cmdFlags.StringVarP(&c.ignorePath, "ignore-path", "i", "", "A regular expression for path to ignore")

if err := cmdFlags.Parse(args); err != nil {
c.UI.Error(fmt.Sprintf("failed to parse arguments: %s", err))
return 1
}

if len(cmdFlags.Args()) != 0 {
c.UI.Error("The provider command expects no arguments")
if len(cmdFlags.Args()) != 2 {
c.UI.Error(fmt.Sprintf("The command expects 2 arguments, but got %#v", cmdFlags.Args()))
c.UI.Error(c.Help())
return 1
}

if len(c.target) == 0 {
c.UI.Error("Argument error: -v is required\n")
c.UI.Error(c.Help())
return 1
}
c.target = cmdFlags.Arg(0)
c.path = cmdFlags.Arg(1)

option, err := tfupdate.NewOption("terraform", c.target, c.recursive, c.ignorePath)
if err != nil {
Expand All @@ -58,13 +55,15 @@ func (c *TerraformCommand) Run(args []string) int {
// Help returns long-form help text.
func (c *TerraformCommand) Help() string {
helpText := `
Usage: tfupdate terraform [options]
Usage: tfupdate terraform [options] <VERSION> <PATH>
Arguments
VERSION A new version constraint
PATH A path of file or directory to update
Options:
-v A new version constraint
-f A path of file or directory to update (default: ./)
-r Check a directory recursively (default: false)
--ignore-path A regular expression for path to ignore
-r --recursive Check a directory recursively (default: false)
-i --ignore-path A regular expression for path to ignore
`
return strings.TrimSpace(helpText)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -8,6 +8,7 @@ require (
github.com/mitchellh/cli v1.0.0
github.com/pkg/errors v0.8.1
github.com/spf13/afero v1.2.2
github.com/spf13/pflag v1.0.5
github.com/zclconf/go-cty v1.0.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -61,6 +61,8 @@ github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down

0 comments on commit 1499b73

Please sign in to comment.