forked from a8m/envsubst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·101 lines (94 loc) · 2.08 KB
/
Copy pathmain.go
File metadata and controls
executable file
·101 lines (94 loc) · 2.08 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// envsubst command line tool
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
)
var (
input = flag.String("i", "", "")
output = flag.String("o", "", "")
noUnset = flag.Bool("no-unset", false, "")
noEmpty = flag.Bool("no-empty", false, "")
)
var usage = `Usage: envsubst [options...] <input>
Options:
-i Specify file input, otherwise use last argument as input file.
If no input file is specified, read from stdin.
-o Specify file output. If none is specified, write to stdout.
-no-unset Fail if a variable is not set.
-no-empty Fail if a variable is set but empty.
`
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage))
}
flag.Parse()
// Reader
var reader *bufio.Reader
if *input != "" {
file, err := os.Open(*input)
if err != nil {
usageAndExit(fmt.Sprintf("Error to open file input: %s.", *input))
}
defer file.Close()
reader = bufio.NewReader(file)
} else {
stat, err := os.Stdin.Stat()
if err != nil || (stat.Mode()&os.ModeCharDevice) != 0 {
usageAndExit("")
}
reader = bufio.NewReader(os.Stdin)
}
// Collect data
var data string
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
data += line
break
}
usageAndExit("Failed to read input.")
}
data += line
}
// Writer
var file *os.File
var err error
if *output != "" {
file, err = os.Create(*output)
if err != nil {
usageAndExit("Error to create the wanted output file.")
}
} else {
file = os.Stdout
}
// Parse input string
result, err := StringRestricted(data, *noUnset, *noEmpty)
if err != nil {
errorAndExit(err)
}
if _, err := file.WriteString(result); err != nil {
filename := *output
if filename == "" {
filename = "STDOUT"
}
usageAndExit(fmt.Sprintf("Error writing output to: %s.", filename))
}
}
func usageAndExit(msg string) {
if msg != "" {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
func errorAndExit(e error) {
fmt.Fprintf(os.Stderr, "%v\n\n", e.Error())
os.Exit(1)
}