MCPcopy
hub / github.com/hashicorp/hcl / realmain

Function realmain

cmd/hcldec/main.go:81–254  ·  view source on GitHub ↗
(args []string)

Source from the content-addressed store, hash-verified

79}
80
81func realmain(args []string) error {
82
83 if *specFile == "" {
84 return fmt.Errorf("the --spec=... argument is required")
85 }
86
87 var diags hcl.Diagnostics
88
89 specContent, specDiags := loadSpecFile(*specFile)
90 diags = append(diags, specDiags...)
91 if specDiags.HasErrors() {
92 err := diagWr.WriteDiagnostics(diags)
93 if err != nil {
94 return fmt.Errorf("failed writing diagnostics: %w", err)
95 }
96
97 err = flush(diagWr)
98 if err != nil {
99 return fmt.Errorf("failed flushing diagnostics: %w", err)
100 }
101 os.Exit(2)
102 }
103
104 spec := specContent.RootSpec
105
106 ctx := &hcl.EvalContext{
107 Variables: map[string]cty.Value{},
108 Functions: map[string]function.Function{},
109 }
110 for name, val := range specContent.Variables {
111 ctx.Variables[name] = val
112 }
113 for name, f := range specContent.Functions {
114 ctx.Functions[name] = f
115 }
116 if len(*vars) != 0 {
117 for i, varsSpec := range *vars {
118 var vals map[string]cty.Value
119 var valsDiags hcl.Diagnostics
120 if strings.HasPrefix(strings.TrimSpace(varsSpec), "{") {
121 // literal JSON object on the command line
122 vals, valsDiags = parseVarsArg(varsSpec, i)
123 } else {
124 // path to a file containing either HCL or JSON (by file extension)
125 vals, valsDiags = parseVarsFile(varsSpec)
126 }
127 diags = append(diags, valsDiags...)
128 for k, v := range vals {
129 ctx.Variables[k] = v
130 }
131 }
132 }
133
134 // If we have empty context elements then we'll nil them out so that
135 // we'll produce e.g. "variables are not allowed" errors instead of
136 // "variable not found" errors.
137 if len(ctx.Variables) == 0 {
138 ctx.Variables = nil

Callers 1

mainFunction · 0.70

Calls 15

HasErrorsMethod · 0.95
VariablesFunction · 0.92
DecodeFunction · 0.92
loadSpecFileFunction · 0.85
flushFunction · 0.85
parseVarsArgFunction · 0.85
parseVarsFileFunction · 0.85
showVarRefsJSONFunction · 0.85
stripJSONNullPropertiesFunction · 0.85
ParseHCLMethod · 0.80
ParseJSONFileMethod · 0.80
ParseHCLFileMethod · 0.80

Tested by

no test coverage detected