Skip to content

Commit 40d64d2

Browse files
authored
feat: add helper functions for workspace configs (#34)
1 parent a0992b9 commit 40d64d2

File tree

2 files changed

+427
-0
lines changed

2 files changed

+427
-0
lines changed

pkg/util/workspace/util.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package workspace
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
v1 "kusionstack.io/kusion-api-go/api.kusion.io/v1"
8+
)
9+
10+
// GetProjectModuleConfigs returns the module configs of a specified project, whose key is the module name, should be called after ValidateModuleConfigs.
11+
// If got empty module configs, return nil config and nil error.
12+
func GetProjectModuleConfigs(configs v1.ModuleConfigs, projectName string) (map[string]v1.GenericConfig, error) {
13+
if len(configs) == 0 {
14+
return nil, nil
15+
}
16+
if projectName == "" {
17+
return nil, errors.New("empty project name")
18+
}
19+
20+
projectConfigs := make(map[string]v1.GenericConfig)
21+
for name, cfg := range configs {
22+
moduleConfig, err := getProjectModuleConfig(cfg, projectName)
23+
if moduleConfig == nil {
24+
continue
25+
}
26+
if err != nil {
27+
return nil, fmt.Errorf("%w, module name: %s", err, name)
28+
}
29+
if len(moduleConfig) != 0 {
30+
projectConfigs[name] = moduleConfig
31+
}
32+
}
33+
34+
return projectConfigs, nil
35+
}
36+
37+
// GetProjectModuleConfig returns the module config of a specified project, should be called after ValidateModuleConfig.
38+
// If got empty module config, return nil config and nil error.
39+
func GetProjectModuleConfig(config *v1.ModuleConfig, projectName string) (v1.GenericConfig, error) {
40+
if config == nil {
41+
return nil, nil
42+
}
43+
if projectName == "" {
44+
return nil, errors.New("empty project name")
45+
}
46+
47+
return getProjectModuleConfig(config, projectName)
48+
}
49+
50+
// getProjectModuleConfig gets the module config of a specified project without checking the correctness of project name.
51+
func getProjectModuleConfig(config *v1.ModuleConfig, projectName string) (v1.GenericConfig, error) {
52+
projectCfg := config.Configs.Default
53+
if len(projectCfg) == 0 {
54+
projectCfg = make(v1.GenericConfig)
55+
}
56+
57+
for name, cfg := range config.Configs.ModulePatcherConfigs {
58+
if name == v1.DefaultBlock {
59+
continue
60+
}
61+
// check the project is assigned in the block or not.
62+
var contain bool
63+
for _, project := range cfg.ProjectSelector {
64+
if projectName == project {
65+
contain = true
66+
break
67+
}
68+
}
69+
if contain {
70+
for k, v := range cfg.GenericConfig {
71+
if k == v1.ProjectSelectorField {
72+
continue
73+
}
74+
projectCfg[k] = v
75+
}
76+
break
77+
}
78+
}
79+
80+
return projectCfg, nil
81+
}
82+
83+
// GetInt32PointerFromGenericConfig returns the value of the key in config which should be of type int.
84+
// If exist but not int, return error. If not exist, return nil.
85+
func GetInt32PointerFromGenericConfig(config v1.GenericConfig, key string) (*int32, error) {
86+
value, ok := config[key]
87+
if !ok {
88+
return nil, nil
89+
}
90+
i, ok := value.(int)
91+
if !ok {
92+
return nil, fmt.Errorf("the value of %s is not int", key)
93+
}
94+
res := int32(i)
95+
return &res, nil
96+
}
97+
98+
// GetStringFromGenericConfig returns the value of the key in config which should be of type string.
99+
// If exist but not string, return error; If not exist, return "", nil.
100+
func GetStringFromGenericConfig(config v1.GenericConfig, key string) (string, error) {
101+
value, ok := config[key]
102+
if !ok {
103+
return "", nil
104+
}
105+
s, ok := value.(string)
106+
if !ok {
107+
return "", fmt.Errorf("the value of %s is not string", key)
108+
}
109+
return s, nil
110+
}
111+
112+
// GetMapFromGenericConfig returns the value of the key in config which should be of type map[string]any.
113+
// If exist but not map[string]any, return error; If not exist, return nil, nil.
114+
func GetMapFromGenericConfig(config v1.GenericConfig, key string) (map[string]any, error) {
115+
value, ok := config[key]
116+
if !ok {
117+
return nil, nil
118+
}
119+
m, ok := value.(v1.GenericConfig)
120+
if !ok {
121+
return nil, fmt.Errorf("the value of %s is not map", key)
122+
}
123+
return m, nil
124+
}
125+
126+
// GetStringMapFromGenericConfig returns the value of the key in config which should be of type map[string]string.
127+
// If exist but not map[string]string, return error; If not exist, return nil, nil.
128+
func GetStringMapFromGenericConfig(config v1.GenericConfig, key string) (map[string]string, error) {
129+
m, err := GetMapFromGenericConfig(config, key)
130+
if err != nil {
131+
return nil, err
132+
}
133+
stringMap := make(map[string]string)
134+
for k, v := range m {
135+
stringValue, ok := v.(string)
136+
if !ok {
137+
return nil, fmt.Errorf("the value of %s.%s is not string", key, k)
138+
}
139+
stringMap[k] = stringValue
140+
}
141+
return stringMap, nil
142+
}

0 commit comments

Comments
 (0)