-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathshell.go
198 lines (176 loc) · 3.89 KB
/
shell.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package penv
import (
"bufio"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type (
shell struct {
configFileName string
commentSigil string
quote func(string) string
mkSet func(*shell, NameValue) string
mkUnset func(*shell, NameValue) string
mkAppend func(*shell, NameValue) string
}
shellOp struct {
op string
nameValue NameValue
}
)
const shellSectionSigil = "#========[ github.com/golang-book/penv ]========="
func (sh *shell) encodeOp(sop shellOp) string {
return sop.op + ":" +
hex.EncodeToString([]byte(sop.nameValue.Name)) + ":" +
hex.EncodeToString([]byte(sop.nameValue.Value))
}
func (sh *shell) decodeOp(ln string) (shellOp, error) {
var sop shellOp
i := strings.LastIndex(ln, sh.commentSigil)
if i < 0 {
return sop, fmt.Errorf("expected comment")
}
args := strings.Split(ln[i+len(sh.commentSigil):], ":")
if len(args) < 3 {
return sop, fmt.Errorf("expected 3 arguments")
}
sop.op = args[0]
bs, err := hex.DecodeString(args[1])
if err != nil {
return sop, err
}
sop.nameValue.Name = string(bs)
bs, err = hex.DecodeString(args[2])
if err != nil {
return sop, err
}
sop.nameValue.Value = string(bs)
return sop, nil
}
func (sh *shell) Load() (*Environment, error) {
env := &Environment{
Appenders: make([]NameValue, 0),
Setters: make([]NameValue, 0),
Unsetters: make([]NameValue, 0),
}
f, err := os.Open(sh.configFileName)
if err != nil {
return env, nil
}
defer f.Close()
s := bufio.NewScanner(f)
inCode := false
for s.Scan() {
if s.Text() == shellSectionSigil {
inCode = !inCode
} else if inCode {
sop, err := sh.decodeOp(s.Text())
if err != nil {
continue
}
switch sop.op {
case "SET":
env.Setters = append(env.Setters, sop.nameValue)
case "UNSET":
env.Unsetters = append(env.Unsetters, sop.nameValue)
case "APPEND":
env.Appenders = append(env.Appenders, sop.nameValue)
}
}
}
return env, s.Err()
}
func (sh *shell) Save(env *Environment) error {
inName := filepath.Join(sh.configFileName)
outName := "/tmp/penv.tmp"
// generate the new file
err := func() error {
fi, err := os.Stat(inName)
if err != nil {
os.MkdirAll(filepath.Dir(inName), 0755)
ioutil.WriteFile(inName, []byte{}, 0755)
fi, err = os.Stat(inName)
}
if err != nil {
return err
}
in, err := os.Open(inName)
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(outName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fi.Mode())
if err != nil {
in.Close()
return err
}
defer out.Close()
w := bufio.NewWriter(out)
s := bufio.NewScanner(in)
inCode := false
for s.Scan() {
if s.Text() == shellSectionSigil {
inCode = !inCode
} else if !inCode {
_, err = w.WriteString(s.Text() + "\n")
if err != nil {
out.Close()
in.Close()
os.Remove(outName)
}
}
}
if s.Err() != nil {
return s.Err()
}
_, err = w.WriteString(shellSectionSigil + "\n")
if err != nil {
return err
}
for _, nv := range env.Setters {
_, err = w.WriteString(sh.mkSet(sh, nv) +
sh.commentSigil + sh.encodeOp(shellOp{
op: "SET",
nameValue: nv,
}) + "\n")
if err != nil {
return err
}
}
for _, nv := range env.Appenders {
_, err = w.WriteString(sh.mkAppend(sh, nv) +
sh.commentSigil + sh.encodeOp(shellOp{
op: "APPEND",
nameValue: nv,
}) + "\n")
if err != nil {
return err
}
}
for _, nv := range env.Unsetters {
_, err = w.WriteString(sh.mkUnset(sh, nv) +
sh.commentSigil + sh.encodeOp(shellOp{
op: "UNSET",
nameValue: nv,
}) + "\n")
if err != nil {
return err
}
}
_, err = w.WriteString(shellSectionSigil + "\n")
if err != nil {
return err
}
return w.Flush()
}()
if err != nil {
os.Remove(outName)
return err
}
// if everything is ok overwrite the file
return os.Rename(outName, inName)
}