Repository navigation
Expand file tree
/
Copy pathexportfunctions.go
More file actions
82 lines (70 loc) · 2.23 KB
/
Copy pathexportfunctions.go
File metadata and controls
82 lines (70 loc) · 2.23 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
package ejson2env
import (
"fmt"
"io"
"os"
"sort"
"strings"
"unicode"
"al.essio.dev/pkg/shellescape"
)
// ExportEnv writes the passed environment values to the passed
// io.Writer as POSIX shell code. When using eval, pass the output as a single,
// double-quoted argument to prevent word splitting and filename expansion.
func ExportEnv(w io.Writer, values map[string]string) {
export(w, "export ", values)
}
// ExportQuiet writes the passed environment values to the passed
// io.Writer in %s=%s format, with the same shell quoting requirements as ExportEnv.
func ExportQuiet(w io.Writer, values map[string]string) {
export(w, "", values)
}
func TrimLeadingUnderscoreExportWrapper(exportfunc ExportFunction) ExportFunction {
return func(w io.Writer, values map[string]string) {
newValues := make(map[string]string, len(values))
for key, value := range values {
newValues[strings.TrimLeft(key, "_")] = value
}
exportfunc(w, newValues)
}
}
func export(w io.Writer, prefix string, values map[string]string) {
keys := make([]string, 0, len(values))
for k := range values {
if !validKey(k) {
fmt.Fprintln(os.Stderr, "ejson2env blocked invalid key")
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
value := filteredValue(values[k])
fmt.Fprintf(w, "%s%s=%s\n", prefix, k, value)
}
}
// validKey accepts only POSIX shell names. Keys are validated when the file is
// read, but options such as --trim-underscore rewrite them afterwards, and an
// empty or otherwise invalid name here would change what the shell executes.
func validKey(k string) bool {
return validIdentifierPattern.MatchString(k)
}
func filteredValue(v string) string {
printable := strings.Map(func(r rune) rune {
// Tabs and newlines are safe inside the single quotes the exporter emits.
if unicode.IsControl(r) && r != '\n' && r != '\t' {
return -1
}
return r
}, v)
if printable != v {
fmt.Fprintln(os.Stderr, "ejson2env trimmed control characters from value")
}
quoted := shellescape.Quote(printable)
// shellescape leaves '=' unquoted, but zsh expands a leading '=' in an
// assignment value to the corresponding command path.
if strings.HasPrefix(quoted, "=") {
return "'" + quoted + "'"
}
return quoted
}