-
Notifications
You must be signed in to change notification settings - Fork 3
/
workspace_profile.go
61 lines (52 loc) · 1.66 KB
/
workspace_profile.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
package modconfig
import (
"fmt"
"reflect"
"github.com/hashicorp/hcl/v2"
"github.com/spf13/cobra"
"github.com/turbot/pipe-fittings/v2/hclhelpers"
"github.com/turbot/pipe-fittings/v2/options"
"github.com/zclconf/go-cty/cty"
)
type WorkspaceProfile interface {
SetOptions(opts options.Options, block *hcl.Block) hcl.Diagnostics
Name() string
ShortName() string
CtyValue() (cty.Value, error)
OnDecoded() hcl.Diagnostics
ConfigMap(cmd *cobra.Command) map[string]interface{}
GetDeclRange() *hcl.Range
GetOptionsForBlock(*hcl.Block) (options.Options, hcl.Diagnostics)
GetInstallDir() *string
// IsNil implements slightly hacky way of doing nil checks with generic types
IsNil() bool
}
func NewWorkspaceProfile[T WorkspaceProfile](block *hcl.Block) (T, hcl.Diagnostics) {
var diags hcl.Diagnostics
var empty T
var res any
profileName := block.Labels[0]
declRange := hclhelpers.BlockRange(block)
switch any(empty).(type) {
case *FlowpipeWorkspaceProfile:
res = &FlowpipeWorkspaceProfile{ProfileName: profileName, DeclRange: declRange}
case *PowerpipeWorkspaceProfile:
res = &PowerpipeWorkspaceProfile{ProfileName: profileName, DeclRange: declRange}
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Unsupported WorkspaceProfile type '%s'", reflect.TypeOf(empty).Name()),
Subject: hclhelpers.BlockRangePointer(block),
})
return empty, diags
}
return res.(T), nil
}
func NewDefaultWorkspaceProfile[T WorkspaceProfile]() (T, hcl.Diagnostics) {
return NewWorkspaceProfile[T](&hcl.Block{
Type: "workspace",
Labels: []string{"default"},
DefRange: hcl.Range{},
TypeRange: hcl.Range{},
})
}