forked from blang/expenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpenv.go
144 lines (122 loc) · 2.84 KB
/
expenv.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)
func main() {
argFile := flag.String("f", "", "Input file, otherwise stdin")
argInPlace := flag.Bool("i", false, "Replace file inplace")
flag.Parse()
var input io.ReadCloser
var output io.WriteCloser
var err error
defer func() {
if output != nil {
if err := output.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
}
}()
defer func() {
if input != nil {
if err := input.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
}
}()
if *argFile != "" {
input, err = os.Open(*argFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error input file: %s\n", err)
os.Exit(1)
}
} else {
input = ReadNopCloser(os.Stdin)
}
if *argInPlace && *argFile != "" {
file, err := ioutil.TempFile("", "expenv")
output = FileReplaceWriteCloser(file, *argFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error output file: %s\n", err)
os.Exit(1)
}
} else {
output = WriteNopCloser(os.Stdout)
}
err = process(input, output)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
// process reads line by line from reader, expands environment variables and writes lines to writer
func process(r io.Reader, w io.Writer) error {
bufr := bufio.NewReader(r)
for {
line, err := bufr.ReadString('\n')
line = expandEnv(line)
_, writererr := io.WriteString(w, line)
if writererr != nil {
return err
}
if err != nil {
break
}
}
return nil
}
// Nop Closer for io.Reader
type readNopCloser struct {
io.Reader
}
func (r readNopCloser) Close() error {
return nil
}
func ReadNopCloser(r io.Reader) io.ReadCloser {
return readNopCloser{r}
}
// Nop Closer for io.Writer
type writeNopCloser struct {
io.Writer
}
func (r writeNopCloser) Close() error {
return nil
}
func WriteNopCloser(w io.Writer) io.WriteCloser {
return writeNopCloser{w}
}
// fileReplaceWriteCloser replaces the replacePath with given file on Close()
type fileReplaceWriteCloser struct {
*os.File
replacePath string
}
// Close closes the file and renames it to replace path. If an error occurs the file is removed.
func (f fileReplaceWriteCloser) Close() error {
err := f.File.Close()
if err != nil {
os.Remove(f.File.Name())
return err
}
err = os.Rename(f.File.Name(), f.replacePath)
if err != nil {
os.Remove(f.File.Name())
return err
}
return nil
}
func FileReplaceWriteCloser(f *os.File, replacePath string) io.WriteCloser {
return &fileReplaceWriteCloser{f, replacePath}
}
// Follow os.ExpandEnv's contract except for `$$` which is transformed to `$`
func expandEnv(s string) string {
os.Setenv("EXPENV_DOLLAR", "$")
os.Setenv("EXPENV_PARENTHESIS", "$(")
s = strings.Replace(s, "$$", "${EXPENV_DOLLAR}", -1)
s = strings.Replace(s, "$(", "${EXPENV_PARENTHESIS}", -1)
return os.ExpandEnv(s)
}