Skip to content

Commit

Permalink
added -rename=false cli flag (#62)
Browse files Browse the repository at this point in the history
* Added -verbose=true cli flag, [DEBUG] messages off by default

* added -rename=false cli flag

* Added more verbose logging, re issue #30

* filter debug messages from tfschema in non verbose mode

* removed tfschema fork (PR accepted)

* code review

* Updated README with optional CLI flags

* code review: refactor initLogFiltering function
  • Loading branch information
shlomimatichin committed Oct 16, 2020
1 parent e45c8e2 commit 383fed0
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 248 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -139,6 +139,13 @@ locals {
}
```

### Optional CLI flags

* `-dir=<path>` - defaults to `.`. Sets the terraform folder to tag `.tf` files in
* `-skipTerratagFiles=false` - Dont skip processing `*.terratag.tf` files (when running terratag a second time for the same directory)
* `-verbose=true` - Turn on verbose logging
* `-rename=false` - Instead of replacing files named `<basename>.tf` with `<basename>.terratag.tf`, keep the original filename

##### See more samples [here](https://github.com/env0/terratag/tree/master/test/fixture)

## Notes
Expand Down
47 changes: 32 additions & 15 deletions cli/cli.go
@@ -1,35 +1,40 @@
package cli

import (
"github.com/env0/terratag/errors"
"fmt"
"log"
"os"
"strconv"
"strings"

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

func InitArgs() (string, string, bool, bool) {
var tags string
var dir string
var skipTerratagFiles string
var isSkipTerratagFiles bool
type Args struct {
Tags string
Dir string
SkipTerratagFiles string
IsSkipTerratagFiles bool
Verbose bool
Rename bool
}

func InitArgs() (Args, bool) {
args := Args{}
isMissingArg := false

tags = setFlag("tags", "")
dir = setFlag("dir", ".")
skipTerratagFiles = setFlag("skipTerratagFiles", "true")
args.Tags = setFlag("tags", "")
args.Dir = setFlag("dir", ".")
args.IsSkipTerratagFiles = booleanFlag("skipTerratagFiles", true)
args.Verbose = booleanFlag("verbose", false)
args.Rename = booleanFlag("rename", true)

if tags == "" {
if args.Tags == "" {
log.Println("Usage: terratag -tags='{ \"some_tag\": \"value\" }' [-dir=\".\"]")
isMissingArg = true
}

isSkipTerratagFiles, err := strconv.ParseBool(skipTerratagFiles)
errorMessage := "-skipTerratagFiles may only be set to true or false"
errors.PanicOnError(err, &errorMessage)

return tags, dir, isSkipTerratagFiles, isMissingArg
return args, isMissingArg
}

func setFlag(flag string, defaultValue string) string {
Expand All @@ -43,3 +48,15 @@ func setFlag(flag string, defaultValue string) string {

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
}
15 changes: 15 additions & 0 deletions counters.go
@@ -0,0 +1,15 @@
package main

type counters struct {
totalResources int
taggedResources int
totalFiles int
taggedFiles int
}

func (self *counters) Add(other counters) {
self.totalResources += other.totalResources
self.taggedResources += other.taggedResources
self.totalFiles += other.totalFiles
self.taggedFiles += other.taggedFiles
}
29 changes: 20 additions & 9 deletions file/file.go
@@ -1,27 +1,38 @@
package file

import (
"github.com/env0/terratag/errors"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"

"github.com/env0/terratag/errors"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
)

func ReplaceWithTerratagFile(path string, textContent string) {
taggedFilename := strings.TrimSuffix(path, filepath.Ext(path)) + ".terratag.tf"
func ReplaceWithTerratagFile(path string, textContent string, rename bool) {
backupFilename := path + ".bak"

log.Print("Creating file ", taggedFilename)
taggedFileError := ioutil.WriteFile(taggedFilename, []byte(textContent), 0644)
errors.PanicOnError(taggedFileError, nil)
if rename {
taggedFilename := strings.TrimSuffix(path, filepath.Ext(path)) + ".terratag.tf"
CreateFile(taggedFilename, textContent)
}

log.Print("Renaming original file from ", path, " to ", backupFilename)
log.Print("[INFO] Backing up ", path, " to ", backupFilename)
backupFileError := os.Rename(path, backupFilename)
errors.PanicOnError(backupFileError, nil)

if !rename {
CreateFile(path, textContent)
}
}

func CreateFile(path string, textContent string) {
log.Print("[INFO] Creating file ", path)
err := ioutil.WriteFile(path, []byte(textContent), 0644)
errors.PanicOnError(err, nil)
}

func GetFilename(path string) string {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Expand Up @@ -4,9 +4,10 @@ go 1.13

require (
github.com/bmatcuk/doublestar v1.2.2
github.com/hashicorp/go-hclog v0.9.2
github.com/hashicorp/hcl/v2 v2.6.0
github.com/minamijoyo/tfschema v0.5.0
github.com/mitchellh/mapstructure v1.1.2
github.com/hashicorp/logutils v1.0.0
github.com/minamijoyo/tfschema v0.5.1-0.20201012140620-226d0de9aed6
github.com/onsi/gomega v1.10.1
github.com/otiai10/copy v1.2.0
github.com/thoas/go-funk v0.5.0
Expand All @@ -16,5 +17,4 @@ require (
)

// remove the following directive when https://github.com/hashicorp/hcl/issues/402 gets fixed

replace github.com/hashicorp/hcl/v2 => github.com/env0/hcl/v2 v2.2.1-0.20201012055633-9ccfb031dba0

0 comments on commit 383fed0

Please sign in to comment.