Skip to content

Commit

Permalink
Merge pull request #5 from im2nguyen/tfvarsfile-tfvar-arguments
Browse files Browse the repository at this point in the history
Add tfvarsfile and tfVar arguments
  • Loading branch information
im2nguyen authored Jul 2, 2021
2 parents a173813 + 537f70a commit ac1b0af
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 22 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ RUN chmod +x /bin/rover

WORKDIR /src

ENTRYPOINT [ "/bin/rover" ]

# CMD ["rover"]
ENTRYPOINT [ "/bin/rover" ]
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ Then, add it as environment variables to your Docker container with `--env-file`
$ docker run --rm -it -p 9000:9000 -v $(pwd):/src --env-file ./.env im2nguyen/rover
```

Use `-tfVarsFile` or `-tfVar` to define variables. For example, you can run the following in the `example/random-test` directory to overload variables.

```
$ docker run --rm -it -p 9000:9000 -v $(pwd):/src im2nguyen/rover -tfVarsFile test.tfvars -tfVar max_length=4
```

## Installation

You can download Rover binary specific to your system by visiting the [Releases page](https://github.com/im2nguyen/rover/releases). Download the binary, unzip, then move `rover` into your `PATH`.
Expand Down Expand Up @@ -101,7 +107,7 @@ $ env GOOS=linux GOARCH=amd64 go build .
Then, build the Docker image.

```
$ docker build . -t im2nguyen/rover
$ docker build . -t im2nguyen/rover --no-cache
```


Expand Down
1 change: 1 addition & 0 deletions example/random-test/test.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_length = 3
50 changes: 45 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,38 @@ import (
//go:embed ui/dist
var frontend embed.FS

type arrayFlags []string

func (i arrayFlags) String() string {
var ts []string
for _, el := range i {
ts = append(ts, el)
}
return strings.Join(ts, ",")
}

func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}

func main() {
log.Println("Starting Rover...")

var tfPath, workingDir, name string
var tfVarsFiles, tfVars arrayFlags
flag.StringVar(&tfPath, "tfPath", "/usr/local/bin/terraform", "Path to Terraform binary")
flag.StringVar(&workingDir, "workingDir", ".", "Path to Terraform configuration")
flag.StringVar(&name, "name", "rover", "Configuration name")
flag.Var(&tfVarsFiles, "tfVarsFile", "Path to *.tfvars files")
flag.Var(&tfVars, "tfVar", "Terraform variable (key=value)")
flag.Parse()

parsedTfVarsFiles := strings.Split(tfVarsFiles.String(), ",")
parsedTfVars := strings.Split(tfVars.String(), ",")

// Generate assets
plan, rso, mapDM, graph := generateAssets(name, workingDir, tfPath)
plan, rso, mapDM, graph := generateAssets(name, workingDir, tfPath, parsedTfVarsFiles, parsedTfVars)

// Save to file (debug)
// saveJSONToFile(name, "plan", "output", plan)
Expand Down Expand Up @@ -99,9 +120,9 @@ func main() {

}

func generateAssets(name string, workingDir string, tfPath string) (*tfjson.Plan, *ResourcesOverview, *Map, Graph) {
func generateAssets(name string, workingDir string, tfPath string, tfVarsFiles []string, tfVars []string) (*tfjson.Plan, *ResourcesOverview, *Map, Graph) {
// Generate Plan
plan, err := generatePlan(name, workingDir, tfPath)
plan, err := generatePlan(name, workingDir, tfPath, tfVarsFiles, tfVars)
if err != nil {
log.Printf(fmt.Sprintf("Unable to parse Plan: %s", err))
os.Exit(2)
Expand Down Expand Up @@ -130,7 +151,7 @@ func generateAssets(name string, workingDir string, tfPath string) (*tfjson.Plan
return plan, rso, mapDM, graph
}

func generatePlan(name string, workingDir string, tfPath string) (*tfjson.Plan, error) {
func generatePlan(name string, workingDir string, tfPath string, tfVarsFiles []string, tfVars []string) (*tfjson.Plan, error) {
tmpDir, err := ioutil.TempDir("", "rover")
if err != nil {
return nil, err
Expand All @@ -151,7 +172,26 @@ func generatePlan(name string, workingDir string, tfPath string) (*tfjson.Plan,

log.Println("Generating plan...")
planPath := fmt.Sprintf("%s/%s-%v", tmpDir, "roverplan", time.Now().Unix())
_, err = tf.Plan(context.Background(), tfexec.Out(planPath))

// Create TF Plan options
var tfPlanOptions []tfexec.PlanOption
tfPlanOptions = append(tfPlanOptions, tfexec.Out(planPath))

// Add *.tfvars files
for _, tfVarsFile := range tfVarsFiles {
if tfVarsFile != "" {
tfPlanOptions = append(tfPlanOptions, tfexec.VarFile(tfVarsFile))
}
}

// Add Terraform variables
for _, tfVar := range tfVars {
if tfVar != "" {
tfPlanOptions = append(tfPlanOptions, tfexec.Var(tfVar))
}
}

_, err = tf.Plan(context.Background(), tfPlanOptions...)
if err != nil {
return nil, errors.New(fmt.Sprintf("Unable to run Plan: %s", err))
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/dist/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>ui</title><link rel="stylesheet" href="/chota.min.css"><link rel="stylesheet" href="/style.css"><link href="/css/app.52fc4fd2.css" rel="preload" as="style"><link href="/js/app.53202c6f.js" rel="preload" as="script"><link href="/js/chunk-vendors.bd6ebaf4.js" rel="preload" as="script"><link href="/css/app.52fc4fd2.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/js/chunk-vendors.bd6ebaf4.js"></script><script src="/js/app.53202c6f.js"></script></body></html>
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>ui</title><link rel="stylesheet" href="/chota.min.css"><link rel="stylesheet" href="/style.css"><link href="/css/app.22910c3c.css" rel="preload" as="style"><link href="/js/app.20524777.js" rel="preload" as="script"><link href="/js/chunk-vendors.bd6ebaf4.js" rel="preload" as="script"><link href="/css/app.22910c3c.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/js/chunk-vendors.bd6ebaf4.js"></script><script src="/js/app.20524777.js"></script></body></html>
2 changes: 2 additions & 0 deletions ui/dist/js/app.20524777.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ui/dist/js/app.20524777.js.map

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions ui/dist/js/app.53202c6f.js

This file was deleted.

1 change: 0 additions & 1 deletion ui/dist/js/app.53202c6f.js.map

This file was deleted.

22 changes: 14 additions & 8 deletions ui/src/components/ResourceDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ c
<span v-if="resourceChange.before">
<div v-for="(val, k) in resourceChange.before" :key="k">
<dd class="key">{{ k }}</dd>
<dt class="value">{{ getBeforeValue(val) }}</dt>
<button
class="copy-button"
@click="copyText(getBeforeValue(val), `${resource.id}-${k}`)"
:ref="`${resource.id}-${k}`"
>
Copy
</button>
<dt class="value">
{{ getBeforeValue(val) }}
<button
class="copy-button"
@click="copyText(getBeforeValue(val), `${resource.id}-${k}`)"
:ref="`${resource.id}-${k}`"
>
Copy
</button>
</dt>
</div>
</span>
<span v-else>Resource doesn't currently exist.</span>
Expand Down Expand Up @@ -326,6 +328,10 @@ export default {
resourceID = `local.${rArray[lastIndex]}`;
}
if (rArray[lastIndex - 1] == "var") {
resourceID = `var.${rArray[lastIndex]}`;
}
// If resourceID is a child only (no . in id)
if (resourceID.match(/^[\w-]+[[]/g) != null) {
resourceID = rArray.slice(1).join(".");
Expand Down

0 comments on commit ac1b0af

Please sign in to comment.