-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
96 lines (88 loc) · 1.92 KB
/
main.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 (
"bufio"
"fmt"
"github.com/crumbhole/argocd-vault-replacer/src/bwvaluesource"
"github.com/crumbhole/argocd-vault-replacer/src/substitution"
"github.com/crumbhole/argocd-vault-replacer/src/vaultvaluesource"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
)
type scanner struct {
source substitution.ValueSource
}
func (s *scanner) process(input []byte) error {
subst := substitution.Substitutor{Source: s.source}
modifiedcontents, err := subst.Substitute(input)
if err != nil {
return err
}
fmt.Printf("---\n%s\n", modifiedcontents)
return nil
}
func (s *scanner) scanFile(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fileRegexp := regexp.MustCompile(`\.ya?ml$`)
if fileRegexp.MatchString(path) {
filecontents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = s.process(filecontents)
if err != nil {
return err
}
}
return nil
}
func (s *scanner) scanDir(path string) error {
return filepath.Walk(path, s.scanFile)
}
func selectValueSource() substitution.ValueSource {
// This would be better with a factory pattern
if bwvaluesource.BwSession() {
return bwvaluesource.BitwardenValueSource{}
}
return vaultvaluesource.VaultValueSource{}
}
func copyEnv() {
for _, envEntry := range []string{`VAULT_ADDR`, `VAULT_TOKEN`} {
val, got := os.LookupEnv(`ARGOCD_ENV_` + envEntry)
if got {
os.Setenv(envEntry, val)
}
}
}
func main() {
copyEnv()
stat, _ := os.Stdin.Stat()
s := scanner{source: selectValueSource()}
if (stat.Mode() & os.ModeCharDevice) == 0 {
reader := bufio.NewReader(os.Stdin)
filecontents, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
}
err = s.process(filecontents)
if err != nil {
log.Fatal(err)
}
} else {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
err = s.scanDir(dir)
if err != nil {
log.Fatal(err)
}
}
}