-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
atlantis_utils.go
52 lines (42 loc) · 2.04 KB
/
atlantis_utils.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
package exec
import (
"reflect"
"strings"
"github.com/mitchellh/mapstructure"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
)
// BuildAtlantisProjectName builds an Atlantis project name from the provided context and project name pattern
func BuildAtlantisProjectName(context schema.Context, projectNameTemplate string) string {
return cfg.ReplaceContextTokens(context, projectNameTemplate)
}
// BuildAtlantisProjectNameFromComponentConfig builds an Atlantis project name from the component config
func BuildAtlantisProjectNameFromComponentConfig(
cliConfig schema.CliConfiguration,
componentName string,
componentSettingsSection map[any]any,
componentVarsSection map[any]any,
) (string, error) {
var atlantisProjectTemplate schema.AtlantisProjectConfig
var atlantisProjectName string
if atlantisSettingsSection, ok := componentSettingsSection["atlantis"].(map[any]any); ok {
// 'settings.atlantis.project_template' has higher priority than 'settings.atlantis.project_template_name'
if atlantisSettingsProjectTemplate, ok := atlantisSettingsSection["project_template"].(map[any]any); ok {
err := mapstructure.Decode(atlantisSettingsProjectTemplate, &atlantisProjectTemplate)
if err != nil {
return "", err
}
} else if atlantisSettingsProjectTemplateName, ok := atlantisSettingsSection["project_template_name"].(string); ok && atlantisSettingsProjectTemplateName != "" {
if pt, ok := cliConfig.Integrations.Atlantis.ProjectTemplates[atlantisSettingsProjectTemplateName]; ok {
atlantisProjectTemplate = pt
}
}
context := cfg.GetContextFromVars(componentVarsSection)
context.Component = strings.Replace(componentName, "/", "-", -1)
// If Atlantis project template is defined and has a name, replace tokens in the name and add the Atlantis project to the output
if !reflect.ValueOf(atlantisProjectTemplate).IsZero() && atlantisProjectTemplate.Name != "" {
atlantisProjectName = BuildAtlantisProjectName(context, atlantisProjectTemplate.Name)
}
}
return atlantisProjectName, nil
}