-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdependson.go
46 lines (38 loc) · 1.07 KB
/
dependson.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
// Copyright 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package terra
import (
"fmt"
tkihcl "github.com/golingon/lingon/pkg/internal/hcl"
"github.com/hashicorp/hcl/v2/hclwrite"
)
// Dependency represents a Terraform dependency using the depends_on
// meta-argument
type Dependency interface {
DependOn() Reference
}
// DependsOn returns a list of dependencies
func DependsOn(dependencies ...Dependency) []Dependency {
return dependencies
}
var _ tkihcl.Tokenizer = (*Dependencies)(nil)
type Dependencies []Dependency
func (d Dependencies) InternalTokens() (hclwrite.Tokens, error) {
if len(d) == 0 {
return nil, nil
}
tokens := hclwrite.TokensForIdentifier("[")
length := len(d)
for i, dep := range d {
t, err := dep.DependOn().InternalTokens()
if err != nil {
return nil, fmt.Errorf("getting tokens for dependency: %w", err)
}
tokens = append(tokens, t...)
if i < (length - 1) {
tokens = append(tokens, hclwrite.TokensForIdentifier(",")...)
}
}
tokens = append(tokens, hclwrite.TokensForIdentifier("]")...)
return tokens, nil
}