@@ -493,41 +493,41 @@ type AtlasConfig struct {
493
493
// }
494
494
func (c * AtlasConfig ) InitBlock () schemahcl.Option {
495
495
return schemahcl .WithInitBlock ("atlas" , func (_ context.Context , ectx * hcl.EvalContext , block * hclsyntax.Block ) (cty.Value , error ) {
496
+ atlasVal , diags := (& hclsyntax.ScopeTraversalExpr {
497
+ Traversal : hcl.Traversal {hcl.TraverseRoot {Name : "atlas" , SrcRange : block .Range ()}},
498
+ }).Value (ectx )
499
+ if diags .HasErrors () {
500
+ if len (diags ) > 1 || diags [0 ].Summary != "Unknown variable" {
501
+ return cty .NilVal , fmt .Errorf ("atlas.cloud: getting config: %v" , diags )
502
+ }
503
+ // Create an empty object if the atlas object is not set.
504
+ atlasVal = cty .ObjectVal (map [string ]cty.Value {})
505
+ }
496
506
var args struct {
497
507
Cloud struct {
498
508
Token string `hcl:"token"`
499
509
URL string `hcl:"url,optional"`
500
510
Project string `hcl:"project,optional"`
501
511
} `hcl:"cloud,block"`
502
512
}
503
- if diags := gohcl .DecodeBody (block .Body , ectx , & args ); diags .HasErrors () {
513
+ switch diags := gohcl .DecodeBody (block .Body , ectx , & args ); {
514
+ case diags .HasErrors ():
504
515
return cty .NilVal , fmt .Errorf ("atlas.cloud: decoding body: %v" , diags )
505
- }
506
- if args .Cloud .Token == "" {
507
- return cty .NilVal , nil // If no token was set, the cloud is not initialized.
508
- }
509
- if args .Cloud .Project == "" {
510
- args .Cloud .Project = cloudapi .DefaultProjectName
511
- }
512
- c .Token = args .Cloud .Token
513
- c .Project = args .Cloud .Project
514
- c .Client = cloudapi .New (args .Cloud .URL , args .Cloud .Token )
515
- cloud := cty .ObjectVal (map [string ]cty.Value {
516
- "client" : cty .CapsuleVal (clientType , c .Client ),
517
- "project" : cty .StringVal (args .Cloud .Project ),
518
- })
519
- av , diags := (& hclsyntax.ScopeTraversalExpr {
520
- Traversal : hcl.Traversal {hcl.TraverseRoot {Name : "atlas" , SrcRange : block .Range ()}},
521
- }).Value (ectx )
522
- switch {
523
- case ! diags .HasErrors ():
524
- m := av .AsValueMap ()
525
- m ["cloud" ] = cloud
526
- return cty .ObjectVal (m ), nil
527
- case len (diags ) == 1 && diags [0 ].Summary == "Unknown variable" :
528
- return cty .ObjectVal (map [string ]cty.Value {"cloud" : cloud }), nil
516
+ case args .Cloud .Token == "" :
517
+ return atlasVal , nil // If no token was set, the cloud is not initialized.
529
518
default :
530
- return cty .NilVal , fmt .Errorf ("atlas.cloud: getting config: %v" , diags )
519
+ if args .Cloud .Project == "" {
520
+ args .Cloud .Project = cloudapi .DefaultProjectName
521
+ }
522
+ c .Token = args .Cloud .Token
523
+ c .Project = args .Cloud .Project
524
+ c .Client = cloudapi .New (args .Cloud .URL , args .Cloud .Token )
525
+ m := atlasVal .AsValueMap ()
526
+ m ["cloud" ] = cty .ObjectVal (map [string ]cty.Value {
527
+ "client" : cty .CapsuleVal (clientType , c .Client ),
528
+ "project" : cty .StringVal (args .Cloud .Project ),
529
+ })
530
+ return cty .ObjectVal (m ), nil
531
531
}
532
532
})
533
533
}
0 commit comments