| 1 |
// Copyright (c) HashiCorp, Inc. |
| 2 |
// SPDX-License-Identifier: MPL-2.0 |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
|
| 11 |
"github.com/hashicorp/terraform-config-inspect/tfconfig" |
| 12 |
flag "github.com/spf13/pflag" |
| 13 |
) |
| 14 |
|
| 15 |
var showJSON = flag.Bool("json", false, "produce JSON-formatted output") |
| 16 |
|
| 17 |
func main() { |
| 18 |
flag.Parse() |
| 19 |
|
| 20 |
var dir string |
| 21 |
if flag.NArg() > 0 { |
| 22 |
dir = flag.Arg(0) |
| 23 |
} else { |
| 24 |
dir = "." |
| 25 |
} |
| 26 |
|
| 27 |
module, _ := tfconfig.LoadModule(dir) |
| 28 |
|
| 29 |
if *showJSON { |
| 30 |
showModuleJSON(module) |
| 31 |
} else { |
| 32 |
showModuleMarkdown(module) |
| 33 |
} |
| 34 |
|
| 35 |
if module.Diagnostics.HasErrors() { |
| 36 |
os.Exit(1) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func showModuleJSON(module *tfconfig.Module) { |
| 41 |
j, err := json.MarshalIndent(module, "", " ") |
| 42 |
if err != nil { |
| 43 |
fmt.Fprintf(os.Stderr, "error producing JSON: %s\n", err) |
| 44 |
os.Exit(2) |
| 45 |
} |
| 46 |
os.Stdout.Write(j) |
| 47 |
os.Stdout.Write([]byte{'\n'}) |
| 48 |
} |
| 49 |
|
| 50 |
func showModuleMarkdown(module *tfconfig.Module) { |
| 51 |
err := tfconfig.RenderMarkdown(os.Stdout, module) |
| 52 |
if err != nil { |
| 53 |
fmt.Fprintf(os.Stderr, "error rendering template: %s\n", err) |
| 54 |
os.Exit(2) |
| 55 |
} |
| 56 |
} |