/
function.go
111 lines (89 loc) · 3.38 KB
/
function.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"github.com/Odania-IT/terraless/schema"
"github.com/Odania-IT/terraless/support"
"github.com/Odania-IT/terraless/templates"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"runtime"
)
const (
currentEnvironmentTemplate = `#!/usr/bin/env bash
{{ range $key, $val := . }}export {{ $key }}={{ $val }}
{{ end }}
`
helperTemplate = `#!/usr/bin/env bash
terraless $@
source {{.TerralessDirectory}}/currentEnvironment.sh
`
helperFunctionBaseTemplate = `#!/usr/bin/env bash
# This file is generated by terraless
`
helperFunctionTemplate = `
function terraless-auth-{{.AliasName}}() {
{{ .Command }}
}
`
)
func initHelperFunction(arguments schema.Arguments) {
logrus.Debug("Verifying helper bash function exists")
if runtime.GOOS == "windows" {
logrus.Info("Can currently not create helper function for Windows")
return
}
helperFile := filepath.Join(arguments.TerralessDirectory, "terraless-env")
buffer := bytes.Buffer{}
buffer = templates.RenderTemplateToBuffer(arguments, buffer, helperTemplate, "helper-template")
err := os.MkdirAll(arguments.TerralessDirectory, 0755)
if err != nil {
logrus.Fatalf("Could not create plugin directory: %s\n", arguments.TerralessDirectory)
}
logrus.Infof("Creating or updating helper file %s\n", helperFile)
support.WriteToFile(helperFile, buffer)
err = os.Chmod(helperFile, 0755)
if err != nil {
logrus.Warnf("Can not make helper script executable: %s\n", helperFile)
}
writeEnvironmentFile(arguments, map[string]string{})
}
func writeEnvironmentFile(arguments schema.Arguments, environmentVariables map[string]string) {
buffer := bytes.Buffer{}
buffer = templates.RenderTemplateToBuffer(environmentVariables, buffer, currentEnvironmentTemplate, "current-environment-template")
currentEnvironmentFile := filepath.Join(arguments.TerralessDirectory, "currentEnvironment.sh")
logrus.Debugf("Writting environment to %s file\n", currentEnvironmentFile)
support.WriteToFile(currentEnvironmentFile, buffer)
}
type HelperFunctionArgs struct {
AliasName string
Command string
}
func createHelperFunctions(terralessData schema.TerralessData, terralessProviders []schema.Provider) {
templateHelperFunctionsFile := filepath.Join(terralessData.Arguments.TerralessDirectory, "helper-functions.sh")
logrus.Infof("Writing helper functions to %s file\n", templateHelperFunctionsFile)
buffer := bytes.Buffer{}
buffer = templates.RenderTemplateToBuffer(terralessData.Config, buffer, helperFunctionBaseTemplate, "helper-function-base-template")
for _, team := range terralessData.GlobalConfig.Teams {
logrus.Debugf("Processing team %s\n", team.Name)
for _, provider := range team.Providers {
logrus.Debugf("Processing provider %s\n", provider.Name)
for _, role := range provider.Roles {
logrus.Debugf("Processing role %s\n", role)
aliasName := provider.Name + "-" + role
command := "echo 'No provider generated the auth-helper code!'"
for _, terralessProvider := range terralessProviders {
if terralessProvider.CanHandle(provider.Type) {
command = terralessProvider.GenerateHelperFunctionCommand(team.Name, provider.Name, role)
}
}
data := HelperFunctionArgs{
AliasName: aliasName,
Command: command,
}
buffer = templates.RenderTemplateToBuffer(data, buffer, helperFunctionTemplate, "helper-function-templates")
}
}
}
support.WriteToFile(templateHelperFunctionsFile, buffer)
}