-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathenv.go
More file actions
89 lines (67 loc) · 2.17 KB
/
env.go
File metadata and controls
89 lines (67 loc) · 2.17 KB
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
package executor
import (
"fmt"
"strconv"
"strings"
"github.com/lets-cli/lets/internal/checksum"
)
func makeEnvEntry(k, v string) string {
return fmt.Sprintf("%s=%s", k, v)
}
func normalizeEnvKey(origKey string) string {
key := strings.ReplaceAll(origKey, "-", "_")
key = strings.ToUpper(key)
return key
}
func convertEnvMapToList(envMap map[string]string) []string {
var envList []string
for name, value := range envMap {
envList = append(envList, makeEnvEntry(name, value))
}
return envList
}
func getChecksumEnvMap(checksumMap map[string]string) map[string]string {
envMap := make(map[string]string)
for name, value := range checksumMap {
envKey := "LETS_CHECKSUM_" + normalizeEnvKey(name)
if name == checksum.DefaultChecksumKey {
envKey = "LETS_CHECKSUM"
}
envMap[envKey] = value
}
return envMap
}
func isChecksumChanged(persistedChecksum string, persistedChecksumExists bool, newChecksum string) bool {
if !persistedChecksumExists {
// We set true here because if there was no persisted checksum that means that its a brand new checksum.
// Hence it was changed from none to some value.
return true
}
// But if we have persisted checksum - we check for checksum change below.
return persistedChecksum != newChecksum
}
// persistedChecksumMap can be empty, and if so, we set env var LETS_CHECKSUM_[NAME]_CHANGED to false for all checksums.
func getChangedChecksumEnvMap(
checksumMap map[string]string,
persistedChecksumMap map[string]string,
) map[string]string {
envMap := make(map[string]string)
for checksumName, checksumValue := range checksumMap {
normalizedKey := normalizeEnvKey(checksumName)
envKey := fmt.Sprintf("LETS_CHECKSUM_%s_CHANGED", normalizedKey)
if checksumName == checksum.DefaultChecksumKey {
envKey = "LETS_CHECKSUM_CHANGED"
}
persistedChecksum, persistedChecksumExists := persistedChecksumMap[checksumName]
checksumChanged := isChecksumChanged(persistedChecksum, persistedChecksumExists, checksumValue)
envMap[envKey] = strconv.FormatBool(checksumChanged)
}
return envMap
}
func fmtEnv(env []string) string {
buf := ""
for _, entry := range env {
buf = fmt.Sprintf("%s\n %s", buf, entry)
}
return buf
}