Skip to content

Commit

Permalink
Add terraform show command for human readable output
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Larfors <[email protected]>
  • Loading branch information
jlarfors committed Nov 8, 2022
1 parent 5a72f9e commit 3b00045
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
61 changes: 61 additions & 0 deletions cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright © 2021 Verifa <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/verifa/terraplate/parser"
"github.com/verifa/terraplate/runner"
)

var (
showJobs int
)

// showCmd represents the show command
var showCmd = &cobra.Command{
Use: "show",
Short: "Runs terraform show on all subdirectories",
Long: `Runs terraform show on all subdirectories.`,
RunE: func(cmd *cobra.Command, args []string) error {
config, err := parser.Parse(&config.ParserConfig)
if err != nil {
return fmt.Errorf("parsing terraplate: %w", err)
}
fmt.Print(terraformStartMessage)
runOpts := []func(r *runner.TerraRunOpts){
runner.RunShow(),
runner.RunShowPlan(),
runner.Jobs(showJobs),
}
runOpts = append(runOpts, runner.ExtraArgs(args))
r := runner.Run(config, runOpts...)

fmt.Println(r.Log())

fmt.Println(r.Summary())

return r.Errors()
},
}

func init() {
RootCmd.AddCommand(showCmd)

showCmd.Flags().IntVarP(&showJobs, "jobs", "j", runner.DefaultJobs, "Number of concurrent terraform jobs to run at one time")
}
38 changes: 33 additions & 5 deletions runner/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ func (t terraCmd) Action() string {
return "planning"
case terraApply:
return "applying"
case terraShowPlan:
case terraShow:
return "summarizing"
case terraShowJSON:
return "summarizing"
}
return "Unknown action"
}

func (t terraCmd) Cmd() string {
switch t {
case terraShowJSON:
return string(terraShow)
default:
return string(t)
}
}

const (
terraExe = "terraform"

Expand All @@ -41,7 +52,11 @@ const (
terraInit terraCmd = "init"
terraPlan terraCmd = "plan"
terraApply terraCmd = "apply"
terraShowPlan terraCmd = "show"
// terraShow is used to run `terraform show` to provide human readle output
terraShow terraCmd = "show"
// terraShowJSON is used to run `terraform show -json` to provide machine
// readable output
terraShowJSON terraCmd = "showPlan"
)

func buildCmd(opts TerraRunOpts, tf *parser.Terrafile) *TaskResult {
Expand Down Expand Up @@ -88,13 +103,26 @@ func showPlanCmd(opts TerraRunOpts, tf *parser.Terrafile) *TaskResult {
plan := tf.ExecBlock.PlanBlock
if plan.SkipOut {
return &TaskResult{
TerraCmd: terraShowPlan,
TerraCmd: terraShowJSON,
Skipped: true,
}
}
var args []string
args = append(args, "-json", plan.Out)
return runCmd(opts.out, tf, terraShowPlan, args)
return runCmd(opts.out, tf, terraShowJSON, args)
}

func showCmd(opts TerraRunOpts, tf *parser.Terrafile) *TaskResult {
plan := tf.ExecBlock.PlanBlock
if plan.SkipOut {
return &TaskResult{
TerraCmd: terraShow,
Skipped: true,
}
}
var args []string
args = append(args, plan.Out)
return runCmd(opts.out, tf, terraShow, args)
}

func applyCmd(opts TerraRunOpts, tf *parser.Terrafile) *TaskResult {
Expand All @@ -119,7 +147,7 @@ func runCmd(out io.Writer, tf *parser.Terrafile, tfCmd terraCmd, args []string)
task := TaskResult{
TerraCmd: tfCmd,
}
cmdArgs := append(tfArgs(tf), string(tfCmd))
cmdArgs := append(tfArgs(tf), tfCmd.Cmd())
cmdArgs = append(cmdArgs, args...)
task.ExecCmd = exec.Command(terraExe, cmdArgs...)

Expand Down
7 changes: 7 additions & 0 deletions runner/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func RunPlan() func(r *TerraRunOpts) {
}
}

func RunShow() func(r *TerraRunOpts) {
return func(r *TerraRunOpts) {
r.show = true
}
}

func RunShowPlan() func(r *TerraRunOpts) {
return func(r *TerraRunOpts) {
r.showPlan = true
Expand Down Expand Up @@ -98,6 +104,7 @@ type TerraRunOpts struct {
init bool
initUpgrade bool
plan bool
show bool
showPlan bool
apply bool

Expand Down
14 changes: 11 additions & 3 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func (r *TerraRun) Start() {
return
}
}
if r.Opts.show {
taskResult := showCmd(r.Opts, tf)
r.Tasks = append(r.Tasks, taskResult)
if taskResult.HasError() {
return
}
}
if r.Opts.showPlan {
taskResult := showPlanCmd(r.Opts, tf)
r.ProcessPlan(taskResult)
Expand Down Expand Up @@ -183,7 +190,8 @@ func (r *TerraRun) IsPlanned() bool {
return false
}
for _, task := range r.Tasks {
if task.TerraCmd == terraPlan {
switch task.TerraCmd {
case terraPlan, terraShowJSON:
return true
}
}
Expand Down Expand Up @@ -265,12 +273,12 @@ func (r *TerraRun) HasPlan() bool {
return r.Plan != nil
}

// ProcessPlanText takes a TaskResult from a terraform show (without -json option)
// ProcessPlanText takes a TaskResult from a terraform show (with -json option)
// which makes for a compact human-readable output which we can show instead of
// the raw output from terraform plan
func (r *TerraRun) ProcessPlan(task *TaskResult) error {
// Make sure we received a `terraform show` task result
if task.TerraCmd != terraShowPlan {
if task.TerraCmd != terraShowJSON {
return fmt.Errorf("terraform show command required for processing plan: received %s", task.TerraCmd)
}
// Cannot process a plan if the `terraform show` command error'd
Expand Down
3 changes: 3 additions & 0 deletions runner/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (t *TaskResult) IsRelevant() bool {
case terraPlan:
// Plan outputs are interesting
return true
case terraShow:
// Show outputs are interesting
return true
case terraApply:
// Apply outputs are interesting
return true
Expand Down

0 comments on commit 3b00045

Please sign in to comment.