-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelpers.go
96 lines (84 loc) · 2.44 KB
/
helpers.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
package main
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"os/exec"
"slices"
"strings"
"github.com/joho/godotenv"
)
// mapToKeyValArray converts a map into a _sorted_ list of KEY=VAL entries.
func mapToKeyValArray(m map[string]string) []string {
var arr []string
for k, v := range m {
arr = append(arr, fmt.Sprintf("%s=%s", k, v))
}
slices.Sort(arr)
return arr
}
func mapToRepeatedKeyValFlag(flagName string, m map[string]string) []string {
arr := mapToKeyValArray(m)
for i, v := range arr {
arr[i] = fmt.Sprintf("%s=%s", flagName, v)
}
return arr
}
func sliceToStringArray(s []string) string {
b := strings.Builder{}
b.WriteString("[")
for i := range s {
// We purposefully do not use %q to avoid Go's built-in string escaping.
// Otherwise, we'd escape " characters a 2nd time for Nix.
s[i] = fmt.Sprintf(`"%s"`, s[i])
}
return fmt.Sprintf("[%s]", strings.Join(s, ", "))
}
// ReadEnvFiles reads the given set of env files into a list of KEY=VAL entries.
//
// If mergeWithEnv is set, the running env is merged with the provided env files. Any
// duplicate variables will be overridden by the running env.
//
// If ignoreMissing is set, any missing env files will be ignored. This is useful for cases
// where an env file is not available during conversion to Nix.
func ReadEnvFiles(envFiles []string, mergeWithEnv, ignoreMissing bool) (env []string, _ error) {
for _, p := range envFiles {
if strings.TrimSpace(p) == "" {
continue
}
envMap, err := godotenv.Read(p)
if err != nil {
if ignoreMissing && errors.Is(err, os.ErrNotExist) {
log.Printf("Ignoring missing env file %q...", p)
continue
}
return nil, fmt.Errorf("failed to parse env file %q: %w", p, err)
}
for k, v := range envMap {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
}
if mergeWithEnv {
env = append(env, os.Environ()...)
}
return env, nil
}
// formatNixCode will format Nix code by calling 'nixfmt' and passing in the
// given code via stdin.
func formatNixCode(contents []byte) ([]byte, error) {
// Check for existence of 'nixfmt' in $PATH.
nixfmtPath, err := exec.LookPath("nixfmt")
if err != nil {
return nil, fmt.Errorf("'nixfmt' not found in $PATH: %w", err)
}
cmd := exec.Command(nixfmtPath)
cmd.Stdin = bytes.NewBuffer(contents)
// Overwrite contents with formatted output.
contents, err = cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to run 'nixfmt' on contents: %w", err)
}
return contents, nil
}