diff --git a/README.md b/README.md index ab77dfb..a2c298e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -100,23 +100,29 @@ Available commands are: ``` $ tfupdate terraform --help -Usage: tfupdate terraform [options] +Usage: tfupdate terraform [options] + +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] @ + +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 @ - -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 diff --git a/command/provider.go b/command/provider.go index 56899ce..c90800f 100644 --- a/command/provider.go +++ b/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. @@ -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 { @@ -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] @ + +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 @ - -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) } diff --git a/command/terraform.go b/command/terraform.go index 6396c9f..00abc51 100644 --- a/command/terraform.go +++ b/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. @@ -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 { @@ -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] + +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) } diff --git a/go.mod b/go.mod index 95129e6..507d2fd 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index d320557..b629cee 100644 --- a/go.sum +++ b/go.sum @@ -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=