This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
/
variables.go
857 lines (763 loc) · 24.7 KB
/
variables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package variables
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
hcljson "github.com/hashicorp/hcl/v2/json"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/gocty"
"github.com/hashicorp/waypoint/internal/appconfig"
"github.com/hashicorp/waypoint/internal/config/dynamic"
pb "github.com/hashicorp/waypoint/internal/server/gen"
)
const (
// Prefix for collecting variable values from environment variables
varEnvPrefix = "WP_VAR_"
// Variable value sources
// listed in descending precedence order for ease of reference
sourceCLI = "cli"
sourceFile = "file"
sourceEnv = "env"
sourceVCS = "vcs"
sourceServer = "server"
sourceDynamic = "dynamic"
sourceDefault = "default"
)
var (
// sourceMap maps a variable pb source type to its string representation
fromSource = map[reflect.Type]string{
reflect.TypeOf((*pb.Variable_Cli)(nil)): sourceCLI,
reflect.TypeOf((*pb.Variable_File_)(nil)): sourceFile,
reflect.TypeOf((*pb.Variable_Env)(nil)): sourceEnv,
reflect.TypeOf((*pb.Variable_Vcs)(nil)): sourceVCS,
reflect.TypeOf((*pb.Variable_Server)(nil)): sourceServer,
reflect.TypeOf((*pb.Variable_Dynamic)(nil)): sourceDynamic,
}
// The attributes we expect to see in variable blocks
// Future expansion here could include `sensitive`, `validations`, etc
variableBlockSchema = &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{
Name: "default",
},
{
Name: "type",
},
{
Name: "description",
},
{
Name: "env",
},
},
}
)
// Variable stores a parsed variable definition from the waypoint.hcl
type Variable struct {
Name string
// The default value in the variable definition
Default *Value
// A list of environment variables that will be sourced to satisfy
// the value of this variable.
Env []string
// Cty Type of the variable. If the default value or a collected value is
// not of this type nor can be converted to this type an error diagnostic
// will show up. This allows us to assume that values are valid.
//
// When a default value - and no type - is passed into the variable
// declaration, the type of the default variable will be used.
Type cty.Type
// Description of the variable
Description string
// The location of the variable definition block in the waypoint.hcl
Range hcl.Range
}
// HclVariable is used when decoding the waypoint.hcl config. Because we use
// hclsimple for this decode, we need the `Type` to be evaluated as an hcl
// expression. When we parse the config, we need `Type` to be evaluated as
// cty.Type, so this struct is only used for the basic decoding of the file
// to verify HCL syntax.
type HclVariable struct {
Name string `hcl:",label"`
Default hcl.Expression `hcl:"default,optional"`
Type hcl.Expression `hcl:"type,optional"`
Description string `hcl:"description,optional"`
Env []string `hcl:"env,optional"`
}
// Values are used to store values collected from various sources.
// Values are added to the map in precedence order, and then used to
// create the final map of cty.Values for config hcl context evaluation.
type Values map[string]*Value
// Value contain the value of the variable along with associated metada,
// including the source it was set from: cli, file, env, vcs, server/ui
type Value struct {
Value cty.Value
Source string
Expr hcl.Expression
// The location of the variable value if the value was provided from a file
Range hcl.Range
}
// DecodeVariableBlocks uses the hclConfig schema to iterate over all
// variable blocks, validating names and types and checking for duplicates.
// It returns the final map of Variables to store for later reference.
func DecodeVariableBlocks(
ctx *hcl.EvalContext,
content *hcl.BodyContent,
) (map[string]*Variable, hcl.Diagnostics) {
var diags hcl.Diagnostics
vs := map[string]*Variable{}
for _, block := range content.Blocks.OfType("variable") {
v, diags := decodeVariableBlock(ctx, block)
if diags.HasErrors() {
return nil, diags
}
if _, found := vs[v.Name]; found {
return nil, []*hcl.Diagnostic{{
Severity: hcl.DiagError,
Summary: "Duplicate variable",
Detail: "Duplicate " + v.Name + " variable definition found.",
Subject: &v.Range,
Context: block.DefRange.Ptr(),
}}
}
vs[block.Labels[0]] = v
}
return vs, diags
}
// decodeVariableBlock validates each part of the variable block,
// building out a defined *Variable
func decodeVariableBlock(
ctx *hcl.EvalContext,
block *hcl.Block,
) (*Variable, hcl.Diagnostics) {
name := block.Labels[0]
v := Variable{
Name: name,
Range: block.DefRange,
}
content, diags := block.Body.Content(variableBlockSchema)
if !hclsyntax.ValidIdentifier(name) {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid variable name",
Detail: "A name must start with a letter or underscore and may contain only letters, digits, underscores, and dashes.",
Subject: &block.LabelRanges[0],
})
}
if attr, exists := content.Attributes["description"]; exists {
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &v.Description)
diags = append(diags, valDiags...)
if diags.HasErrors() {
return nil, diags
}
}
if attr, ok := content.Attributes["type"]; ok {
t, moreDiags := typeexpr.Type(attr.Expr)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
return nil, diags
}
v.Type = t
}
if attr, exists := content.Attributes["env"]; exists {
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &v.Env)
diags = append(diags, valDiags...)
if diags.HasErrors() {
return nil, diags
}
}
if attr, exists := content.Attributes["default"]; exists {
defaultCtx := ctx.NewChild()
defaultCtx.Functions = dynamic.Register(map[string]function.Function{})
val, valDiags := attr.Expr.Value(defaultCtx)
diags = append(diags, valDiags...)
if diags.HasErrors() {
return nil, diags
}
// Depending on the value type, we behave differently.
switch val.Type() {
case dynamic.Type:
// For dynamic types we don't do conversion because we don't yet
// have the value. For now we require v.Type to be string so that
// the user isn't surprised by anything. Users can use explicit
// type conversion such as `tonumber`.
if v.Type != cty.String {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("type for variable %q must be string for dynamic values", name),
Detail: "When using dynamically sourced configuration values, " +
"the variable type must be string. You may use explicit " +
"type conversion functions such as `tonumber` when using " +
"the variable.",
Subject: attr.Expr.Range().Ptr(),
})
val = cty.DynamicVal
}
default:
// Convert the default to the expected type so we can catch invalid
// defaults early and allow later code to assume validity.
// Note that this depends on us having already processed any "type"
// attribute above.
if v.Type != cty.NilType {
var err error
val, err = convert.Convert(val, v.Type)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Invalid default value for variable %q", name),
Detail: fmt.Sprintf("This default value is not compatible with the variable's type constraint: %s.", err),
Subject: attr.Expr.Range().Ptr(),
})
val = cty.DynamicVal
}
}
}
v.Default = &Value{
Source: sourceDefault,
Value: val,
}
// It's possible no type attribute was assigned so lets make sure we
// have a valid type otherwise there could be issues parsing the value.
if v.Type == cty.NilType {
v.Type = val.Type()
}
}
return &v, diags
}
// LoadVariableValues collects values set via the CLI (-var, -varfile) and
// local env vars (WP_VAR_*) and translates those values to pb.Variables. These
// pb.Variables can then be set on the job for eventual parsing on the runner,
// after the runner has decoded the variables defined in the waypoint.hcl.
// All values are set as protobuf strings, with the expectation that later
// evaluation will convert them to their defined types.
func LoadVariableValues(vars map[string]string, files []string) ([]*pb.Variable, hcl.Diagnostics) {
var diags hcl.Diagnostics
ret := []*pb.Variable{}
// The order here is important, as the order in which values are evaluated
// dictate their precedence. We therefore evalute these three sources in order
// of env, file, cli.
// process env values ("env" source)
{
env := os.Environ()
for _, raw := range env {
if !strings.HasPrefix(raw, varEnvPrefix) {
continue
}
raw = raw[len(varEnvPrefix):] // trim the prefix
eq := strings.Index(raw, "=")
if eq == -1 {
// Seems invalid, so we'll ignore it.
continue
}
name := raw[:eq]
rawVal := raw[eq+1:]
ret = append(ret, &pb.Variable{
Name: name,
Value: &pb.Variable_Str{Str: rawVal},
Source: &pb.Variable_Env{},
})
}
}
// process -var-file args ("file" source)
for _, file := range files {
if file != "" {
pbv, diags := parseFileValues(file, sourceFile)
if diags.HasErrors() {
return nil, diags
}
ret = append(ret, pbv...)
}
}
// process -var args ("cli" source)
for name, val := range vars {
ret = append(ret, &pb.Variable{
Name: name,
Value: &pb.Variable_Str{Str: val},
Source: &pb.Variable_Cli{},
})
}
return ret, diags
}
// LoadEnvValues loads the variable values from environment variables
// specified via the `env` field on the `variable` stanza.
func LoadEnvValues(vars map[string]*Variable) ([]*pb.Variable, hcl.Diagnostics) {
var diags hcl.Diagnostics
var ret []*pb.Variable
for _, variable := range vars {
// First we check for the WP_VAR_ value cause that always wins.
v := os.Getenv(varEnvPrefix + variable.Name)
// If we didn't find one and we have other sources, check those.
if v == "" && len(variable.Env) > 0 {
for _, env := range variable.Env {
v = os.Getenv(env)
if v != "" {
break
}
}
}
// If we still have no value, then we set nothing.
if v == "" {
continue
}
ret = append(ret, &pb.Variable{
Name: variable.Name,
Value: &pb.Variable_Str{Str: v},
Source: &pb.Variable_Env{},
})
}
return ret, diags
}
// NeedsDynamicDefaults returns true if there are variables with a dynamic
// default value set that must be evaluated (because the value is not
// overridden).
func NeedsDynamicDefaults(
pbvars []*pb.Variable,
vars map[string]*Variable,
) bool {
// Get all our variables with dynamic defaults
dynamicVars := map[string]*Variable{}
for k, v := range vars {
if v.Default != nil {
val := v.Default.Value
if val.Type() == dynamic.Type {
dynamicVars[k] = v
}
}
}
// Go through our variable values and delete any dynamic vars we have
// values for already; we do not need to fetch those.
for _, pbv := range pbvars {
delete(dynamicVars, pbv.Name)
}
return len(dynamicVars) > 0
}
// LoadDynamicDefaults will load the default values for variables that have
// dynamic configurations. This will only load the values if there isn't an
// existing variable set in pbvars. Therefore, it is recommended that this is
// called last and the values are _prepended_ to pbvars for priority.
func LoadDynamicDefaults(
ctx context.Context,
log hclog.Logger,
pbvars []*pb.Variable,
vars map[string]*Variable,
dynamicOpts ...appconfig.Option,
) ([]*pb.Variable, hcl.Diagnostics) {
var diags hcl.Diagnostics
// Get all our variables with dynamic defaults
dynamicVars := map[string]*Variable{}
for k, v := range vars {
if v.Default != nil {
val := v.Default.Value
if val.Type() == dynamic.Type {
dynamicVars[k] = v
}
}
}
// If we have no dynamic vars, we do nothing.
if len(dynamicVars) == 0 {
return nil, diags
}
// Go through our variable values and delete any dynamic vars we have
// values for already; we do not need to fetch those.
for _, pbv := range pbvars {
delete(dynamicVars, pbv.Name)
}
// If we have no dynamic vars we need values for, also do nothing.
if len(dynamicVars) == 0 {
return nil, diags
}
// Build our watcher
ch := make(chan *appconfig.UpdatedConfig)
w, err := appconfig.NewWatcher(append(
append([]appconfig.Option{}, dynamicOpts...),
appconfig.WithLogger(log),
appconfig.WithNotify(ch),
)...)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Error initializing configuration watcher",
Detail: err.Error(),
Subject: &hcl.Range{
Filename: "waypoint.hcl",
},
})
return nil, diags
}
defer w.Close()
// We have some variables we need to fetch dynamic values for.
configVars := make([]*pb.ConfigVar, 0, len(dynamicVars))
for k, v := range dynamicVars {
configVars = append(configVars, &pb.ConfigVar{
Name: k,
Value: &pb.ConfigVar_Dynamic{
Dynamic: v.Default.Value.EncapsulatedValue().(*pb.ConfigVar_DynamicVal),
},
// This is set to true on purpose because it forces the appconfig
// watcher to give us an easier to consume format (struct vs
// array of key=value strings for env vars).
NameIsPath: true,
})
}
// Send our variables. Purposely ignore the error return value because
// it can only ever be a context cancellation which we pick up in the
// select later.
w.UpdateVars(ctx, configVars)
// Wait for values.
select {
case <-ctx.Done():
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Cancellation while waiting for configuration.",
Detail: ctx.Err().Error(),
Subject: &hcl.Range{
Filename: "waypoint.hcl",
},
})
return nil, diags
case config := <-ch:
var result []*pb.Variable
for _, f := range config.Files {
result = append(result, &pb.Variable{
Name: f.Path,
Value: &pb.Variable_Str{
Str: string(f.Data),
},
Source: &pb.Variable_Dynamic{},
})
}
return result, diags
}
}
// EvaluateVariables evaluates the provided variable values and validates their
// types per the type declared in the waypoint.hcl for that variable name.
// The order in which values are evaluated corresponds to their precedence, with
// higher precedence values overwriting lower precedence values.
//
// The supplied map of *Variable should be all defined variables (currently
// comes from decoding all variable blocks within the waypoint.hcl), and
// is used to validate types and that all variables have at least one
// assigned value.
func EvaluateVariables(
log hclog.Logger,
pbvars []*pb.Variable,
vs map[string]*Variable,
) (Values, hcl.Diagnostics) {
var diags hcl.Diagnostics
iv := Values{}
for v, def := range vs {
// Do not allow dynamic values as default values since they aren't valid.
// Dynamic values should be evaluated and overridden by LoadDynamicDefaults
// and provided via pbvars. If not, then an unset error will be created.
if def.Default != nil && def.Default.Value.Type() == dynamic.Type {
continue
}
iv[v] = def.Default
}
for _, pbv := range pbvars {
variable, found := vs[pbv.Name]
if !found {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Undefined variable",
Detail: fmt.Sprintf("A %q variable value was set, "+
"but was not found in known variables. To declare variable "+
"%q, place a variable definition block in your waypoint.hcl file.",
pbv.Name, pbv.Name),
Subject: &hcl.Range{
Filename: "waypoint.hcl",
},
})
continue
}
// set our source for error messaging
source := fromSource[reflect.TypeOf(pbv.Source)]
if source == "" {
source = "unknown"
log.Debug("No source found for value given for variable %q", pbv.Name)
}
// We have to specify the three different simple types we support -- string,
// bool, number -- when doing the below evaluation of hcl expressions
// because of our translation to-and-from protobuf format.
// While cty allows us to parse all simple types as LiteralValueExpr, we
// have to first translate the pb values back into cty values, thus
// necessitating a separate case statement for each simple type
var expr hclsyntax.Expression
switch sv := pbv.Value.(type) {
case *pb.Variable_Hcl:
value := sv.Hcl
fakeFilename := fmt.Sprintf("<value for var.%s from source %q>", pbv.Name, source)
expr, diags = hclsyntax.ParseExpression([]byte(value), fakeFilename, hcl.Pos{Line: 1, Column: 1})
case *pb.Variable_Str:
value := sv.Str
expr = &hclsyntax.LiteralValueExpr{Val: cty.StringVal(value)}
case *pb.Variable_Bool:
value := sv.Bool
expr = &hclsyntax.LiteralValueExpr{Val: cty.BoolVal(value)}
case *pb.Variable_Num:
value := sv.Num
expr = &hclsyntax.LiteralValueExpr{Val: cty.NumberIntVal(value)}
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid value type for variable",
Detail: "The variable type was not set as a string, number, bool, or hcl expression",
Subject: &variable.Range,
})
return nil, diags
}
val, valDiags := expr.Value(nil)
if valDiags.HasErrors() {
diags = append(diags, valDiags...)
return nil, diags
}
if variable.Type != cty.NilType {
var err error
// store the current cty.Value type before attempting the conversion
valType := val.Type().FriendlyName()
// If the value came from the cli or an env var, it was stored as
// a raw string value; however it could be a complex value, such as a
// map/list/etc.
// Now that we know the expected type, we'll check here for that
// and, if necessary, repeat the expression parsing for HCL syntax
if source == sourceCLI || source == sourceEnv {
if !variable.Type.IsPrimitiveType() {
fakeFilename := fmt.Sprintf("<value for var.%s from source %q>", pbv.Name, source)
expr, diags = hclsyntax.ParseExpression([]byte(val.AsString()), fakeFilename, hcl.Pos{Line: 1, Column: 1})
val, valDiags = expr.Value(nil)
if valDiags.HasErrors() {
diags = append(diags, valDiags...)
return nil, diags
}
}
}
val, err = convert.Convert(val, variable.Type)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid value for variable",
Detail: fmt.Sprintf(
"The value set for variable %q from source %q is of value type %q and is not compatible with the variable's type constraint: %s.",
pbv.Name,
source,
valType,
err,
),
Subject: &variable.Range,
})
val = cty.DynamicVal
}
}
iv[pbv.Name] = &Value{
Source: source,
Value: val,
Expr: expr,
}
}
// check that all variables have a set value, including default of null
for name, variable := range vs {
v, ok := iv[name]
if !ok || v == nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Unset variable %q", name),
Detail: "A variable must be set or have a default value; see " +
"https://www.waypointproject.io/docs/waypoint-hcl/variables/input " +
"for details.",
Subject: &variable.Range,
})
}
}
return iv, diags
}
// LoadAutoFiles loads any *.auto.wpvars(.json) files in the source repo
func LoadAutoFiles(wd string) ([]*pb.Variable, hcl.Diagnostics) {
var pbv []*pb.Variable
var diags hcl.Diagnostics
// Check working directory (vcs or local) for *.auto.wpvars(.json) files
var varFiles []string
if files, err := ioutil.ReadDir(wd); err == nil {
for _, f := range files {
name := f.Name()
if !isAutoVarFile(name) {
continue
}
varFiles = append(varFiles, filepath.Join(wd, name))
}
}
for _, f := range varFiles {
if f != "" {
pbv, diags = parseFileValues(f, sourceVCS)
if diags.HasErrors() {
return nil, diags
}
}
}
return pbv, nil
}
// parseFileValues is a helper function to extract variable values from the
// provided file, using the provided source to set the pb.Variable.Source value.
func parseFileValues(filename string, source string) ([]*pb.Variable, hcl.Diagnostics) {
var pbv []*pb.Variable
f, diags := readFileValues(filename)
if diags.HasErrors() {
return nil, diags
}
attrs, moreDiags := f.Body.JustAttributes()
diags = append(diags, moreDiags...)
// We grab all variables here; we'll later check set variables against the
// known variables defined in the waypoint.hcl on the runner when we
// consolidate values from local + server
for name, attr := range attrs {
val, moreDiags := attr.Expr.Value(nil)
diags = append(diags, moreDiags...)
v := &pb.Variable{
Name: name,
}
// Set type
switch val.Type() {
case cty.String:
v.Value = &pb.Variable_Str{Str: val.AsString()}
case cty.Bool:
v.Value = &pb.Variable_Bool{Bool: val.True()}
case cty.Number:
var num int64
err := gocty.FromCtyValue(val, &num)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid number",
Detail: err.Error(),
Subject: &attr.Range,
})
return nil, diags
}
v.Value = &pb.Variable_Num{Num: num}
default:
// if it's not a primitive/simple type, we set as bytes here to be later
// parsed as an hcl expression; any errors at evaluating the hcl type will
// be handled at that time
bv := hclwrite.TokensForValue(val).Bytes()
buf := bytes.NewBuffer(bv)
v.Value = &pb.Variable_Hcl{Hcl: buf.String()}
}
// Set source
switch source {
case sourceFile:
v.Source = &pb.Variable_File_{}
case sourceVCS:
v.Source = &pb.Variable_Vcs{}
}
pbv = append(pbv, v)
}
return pbv, diags
}
// readFileValues is a helper function that loads a file, parses if it is
// hcl or json, and checks for any errant variable definition blocks. It returns
// the files contents for further evaluation.
func readFileValues(filename string) (*hcl.File, hcl.Diagnostics) {
var diags hcl.Diagnostics
// load the file
src, err := ioutil.ReadFile(filename)
if err != nil {
var errStr string
if os.IsNotExist(err) {
errStr = fmt.Sprintf("Given variables file %s does not exist.", filename)
} else {
errStr = fmt.Sprintf("Error while reading %s: %s.", filename, err)
}
return nil, append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Failed to read variable values from file",
Detail: errStr,
Subject: &hcl.Range{
Filename: filename,
},
})
}
// parse the file, whether it's hcl or json
var f *hcl.File
if strings.HasSuffix(filename, ".json") {
var hclDiags hcl.Diagnostics
f, hclDiags = hcljson.Parse(src, filename)
diags = append(diags, hclDiags...)
if f == nil || f.Body == nil {
return nil, diags
}
} else {
var hclDiags hcl.Diagnostics
f, hclDiags = hclsyntax.ParseConfig(src, filename, hcl.Pos{Line: 1, Column: 1})
diags = append(diags, hclDiags...)
if f == nil || f.Body == nil {
return nil, diags
}
}
// Before we do our real decode, we'll probe to see if there are any
// blocks of type "variable" in this body, since it's a common mistake
// for new users to put variable declarations in wpvars rather than
// variable value definitions.
{
content, _, _ := f.Body.PartialContent(&hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "variable",
LabelNames: []string{"name"},
},
},
})
for _, block := range content.Blocks {
name := block.Labels[0]
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Variable declaration in a wpvars file",
Detail: fmt.Sprintf("A wpvars file is used to assign "+
"values to variables that have already been declared "+
"in the waypoint.hcl, not to declare new variables. To "+
"declare variable %q, place this block in your "+
"waypoint.hcl file.\n\nTo set a value for this variable "+
"in %s, use the definition syntax instead:\n %s = <value>",
name, block.TypeRange.Filename, name),
Subject: &block.TypeRange,
})
}
if diags.HasErrors() {
return nil, diags
}
}
return f, diags
}
// values creates a map of cty.values from the map of InputValues, for use
// in creating hcl contexts
func (iv Values) values() map[string]cty.Value {
res := map[string]cty.Value{}
for k, v := range iv {
res[k] = v.Value
}
return res
}
// AddInputVariables adds the InputValues to the provided hcl context
func AddInputVariables(ctx *hcl.EvalContext, vs Values) {
vars := vs.values()
variables := map[string]cty.Value{
"var": cty.ObjectVal(vars),
}
ctx.Variables = variables
}
// isAutoVarFile determines if the file ends with .auto.wpvars or .auto.wpvars.json
func isAutoVarFile(path string) bool {
return strings.HasSuffix(path, ".auto.wpvars") ||
strings.HasSuffix(path, ".auto.wpvars.json")
}