Skip to content

Commit

Permalink
Rewrite package terratag/cli to use standard go package flag for the …
Browse files Browse the repository at this point in the history
…CLI arguments
  • Loading branch information
Aleksandr Rybolovlev committed Sep 30, 2021
1 parent 62051ac commit 39110ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
47 changes: 11 additions & 36 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package cli

import (
"fmt"
"flag"
"log"
"os"
"strconv"
"strings"

"github.com/env0/terratag/errors"
)

type Args struct {
Tags string
Dir string
SkipTerratagFiles string
Filter string
IsSkipTerratagFiles bool
Verbose bool
Expand All @@ -24,12 +19,16 @@ func InitArgs() (Args, bool) {
args := Args{}
isMissingArg := false

args.Tags = setFlag("tags", "")
args.Dir = setFlag("dir", ".")
args.IsSkipTerratagFiles = booleanFlag("skipTerratagFiles", true)
args.Filter = setFlag("filter", ".*")
args.Verbose = booleanFlag("verbose", false)
args.Rename = booleanFlag("rename", true)
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)

fs.StringVar(&args.Tags, "tags", "", "Tags as a valid JSON document")
fs.StringVar(&args.Dir, "dir", ".", "Directory to recursively search for .tf files and terratag them")
fs.BoolVar(&args.IsSkipTerratagFiles, "skipTerratagFiles", true, "Skips any previously tagged files")
fs.StringVar(&args.Filter, "filter", ".*", "Only apply tags to the selected resource types (regex)")
fs.BoolVar(&args.Verbose, "verbose", false, "Enable verbose logging")
fs.BoolVar(&args.Rename, "rename", true, "Keep the original filename or replace it with <basename>.terratag.tf")

fs.Parse(os.Args[1:])

if args.Tags == "" {
log.Println("Usage: terratag -tags='{ \"some_tag\": \"value\" }' [-dir=\".\"]")
Expand All @@ -38,27 +37,3 @@ func InitArgs() (Args, bool) {

return args, isMissingArg
}

func setFlag(flag string, defaultValue string) string {
result := defaultValue
prefix := "-" + flag + "="
for _, arg := range os.Args {
if strings.HasPrefix(arg, prefix) {
result = strings.TrimPrefix(arg, prefix)
}
}

return result
}

func booleanFlag(flag string, defaultValue bool) bool {
defaultString := "false"
if defaultValue {
defaultString = "true"
}
stringValue := setFlag(flag, defaultString)
value, err := strconv.ParseBool(stringValue)
errorMessage := fmt.Sprint("-", flag, " may only be set to true or false")
errors.PanicOnError(err, &errorMessage)
return value
}
6 changes: 5 additions & 1 deletion terratag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import (
)

var cleanArgs = append(os.Args)
var args = append(os.Args, "-tags={\"env0_environment_id\":\"40907eff-cf7c-419a-8694-e1c6bf1d1168\",\"env0_project_id\":\"43fd4ff1-8d37-4d9d-ac97-295bd850bf94\"}")

var args = []string{
os.Args[0],
"-tags={\"env0_environment_id\":\"40907eff-cf7c-419a-8694-e1c6bf1d1168\",\"env0_project_id\":\"43fd4ff1-8d37-4d9d-ac97-295bd850bf94\"}",
}
var rootDir = "test/fixture"

type TestCase struct {
Expand Down

0 comments on commit 39110ec

Please sign in to comment.