github.com/hashicorp/hcl/v2@v2.20.0/cmd/hclspecsuite/main.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"os/exec"
    10  
    11  	"golang.org/x/crypto/ssh/terminal"
    12  
    13  	"github.com/hashicorp/hcl/v2"
    14  	"github.com/hashicorp/hcl/v2/hclparse"
    15  )
    16  
    17  func main() {
    18  	os.Exit(realMain(os.Args[1:]))
    19  }
    20  
    21  func realMain(args []string) int {
    22  	if len(args) != 2 {
    23  		fmt.Fprintf(os.Stderr, "Usage: hclspecsuite <tests-dir> <hcldec-file>\n")
    24  		return 2
    25  	}
    26  
    27  	testsDir := args[0]
    28  	hcldecPath := args[1]
    29  
    30  	hcldecPath, err := exec.LookPath(hcldecPath)
    31  	if err != nil {
    32  		fmt.Fprintf(os.Stderr, "%s\n", err)
    33  		return 2
    34  	}
    35  
    36  	parser := hclparse.NewParser()
    37  
    38  	color := terminal.IsTerminal(int(os.Stderr.Fd()))
    39  	w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
    40  	if err != nil {
    41  		w = 80
    42  	}
    43  	diagWr := hcl.NewDiagnosticTextWriter(os.Stderr, parser.Files(), uint(w), color)
    44  	var diagCount int
    45  
    46  	runner := &Runner{
    47  		parser:     parser,
    48  		hcldecPath: hcldecPath,
    49  		baseDir:    testsDir,
    50  		logBegin: func(name string, file *TestFile) {
    51  			fmt.Printf("- %s\n", name)
    52  		},
    53  		logProblems: func(name string, file *TestFile, diags hcl.Diagnostics) {
    54  			if len(diags) != 0 {
    55  				os.Stderr.WriteString("\n")
    56  				diagWr.WriteDiagnostics(diags)
    57  				diagCount += len(diags)
    58  			}
    59  			fmt.Printf("- %s\n", name)
    60  		},
    61  	}
    62  	diags := runner.Run()
    63  
    64  	if len(diags) != 0 {
    65  		os.Stderr.WriteString("\n\n\n== Test harness problems:\n\n")
    66  		diagWr.WriteDiagnostics(diags)
    67  		diagCount += len(diags)
    68  	}
    69  
    70  	if diagCount > 0 {
    71  		return 2
    72  	}
    73  	return 0
    74  }