-
Notifications
You must be signed in to change notification settings - Fork 0
/
envdo.go
169 lines (130 loc) · 2.95 KB
/
envdo.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
var passhroughEnvVars = []string{
"LANG",
"LC_CTYPE",
"TERM",
"SHELL",
"PATH",
"PWD",
"HOME",
"USER",
"LOGNAME",
}
func readFiles(path string) ([]string, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
var allFiles []string
for _, f := range files {
withoutSuffix := strings.TrimSuffix(f.Name(), ".gpg")
if f.Mode().IsDir() {
if strings.HasPrefix(f.Name(), ".") {
continue
}
subdirFiles, err := readFiles(path + "/" + withoutSuffix)
if err != nil {
return nil, err
}
allFiles = append(allFiles, subdirFiles...)
continue
}
allFiles = append(allFiles, path+"/"+withoutSuffix)
}
return allFiles, nil
}
func profileName(base, path string) string {
return strings.TrimPrefix(path, base+"/")
}
func absProfilePath(path, profileName string) (string, error) {
if !strings.HasSuffix(profileName, ".gpg") {
profileName += ".gpg"
}
stat, err := os.Lstat(path)
if err != nil {
return "", err
}
if stat.Mode() == os.ModeSymlink {
path, err = os.Readlink(path)
if err != nil {
return "", err
}
}
return fmt.Sprintf("%s/%s", path, profileName), nil
}
func passtroughEnvWithValues() []string {
var evs []string
for _, name := range passhroughEnvVars {
evs = append(evs, fmt.Sprintf("%s=%s", name, os.Getenv(name)))
}
return evs
}
func fetchEnv(path, profile string, recipient *string) ([]string, error) {
absPath, err := absProfilePath(path, profile)
if err != nil {
return nil, err
}
var reader io.Reader
ext := filepath.Ext(absPath)
if ext == ".gpg" {
var b []byte
var gpgArgs []string
if recipient != nil {
gpgArgs = append(gpgArgs, []string{"-r", *recipient}...)
}
gpgArgs = append(gpgArgs, []string{"-d", "--quiet", absPath}...)
buf := bytes.NewBuffer(b)
err := executeCmdWithWriter("gpg2", gpgArgs, true, nil, buf)
if err != nil {
return nil, err
}
reader = buf
} else {
f, err := os.Open(absPath)
if err != nil {
return nil, err
}
reader = f
defer f.Close()
}
var lines []string
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix("#", line) {
continue
}
lines = append(lines, strings.Replace(line, `"`, ``, -1))
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
func executeCmd(command string, args []string, preserveEnv bool, env []string) error {
return executeCmdWithWriter(command, args, preserveEnv, env, os.Stdout)
}
func executeCmdWithWriter(command string, args []string, preserveEnv bool, env []string, writer io.Writer) error {
cmd := exec.Command(command, args...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = writer
// Default variables to pass through.
cmd.Env = passtroughEnvWithValues()
if preserveEnv {
cmd.Env = os.Environ()
}
cmd.Env = append(cmd.Env, env...)
return cmd.Run()
}