-
Notifications
You must be signed in to change notification settings - Fork 45
/
reader.go
67 lines (59 loc) · 1.59 KB
/
reader.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
package parse
import (
"fmt"
"path/filepath"
"strings"
"github.com/anz-bank/sysl/pkg/env"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/anz-bank/golden-retriever/reader"
"github.com/anz-bank/golden-retriever/reader/filesystem"
"github.com/anz-bank/golden-retriever/reader/remotefs"
"github.com/anz-bank/golden-retriever/retriever/git"
"github.com/spf13/afero"
)
const SyslRootMarker = ".sysl"
func NewReader(fs afero.Fs) (reader.Reader, error) {
root := "."
if v, is := fs.(*syslutil.ChrootFs); is {
root = v.Root()
}
tokens := make(map[string]string)
if tokensStr := env.SYSL_TOKENS.Value(); tokensStr != "" {
hostTokens := strings.Split(tokensStr, ",")
for _, t := range hostTokens {
arr := strings.Split(t, ":")
if len(arr) != 2 {
return nil, fmt.Errorf(
"envvar %s is invalid, should be in format `hosta:<tokena>,hostb:<tokenb>`",
env.SYSL_TOKENS,
)
}
tokens[arr[0]] = arr[1]
}
}
keys := make(map[string]git.SSHKey)
if keysStr := env.SYSL_SSH_KEYS.Value(); keysStr != "" {
hostKeys := strings.Split(keysStr, ",")
for _, k := range hostKeys {
arr := strings.Split(k, ":")
if len(arr) != 3 {
return nil, fmt.Errorf(
"envvar %s is invalid, should be in format `hosta:<keya>:<passphrasea>,hostb:<keyb>:<passphraseb>`",
env.SYSL_SSH_KEYS,
)
}
keys[arr[0]] = git.SSHKey{
PrivateKey: arr[1],
PrivateKeyPassword: arr[2],
}
}
}
return remotefs.NewWithGitRetriever(
filesystem.New(fs),
filepath.Join(root, SyslRootMarker, "modules.yaml"),
&git.AuthOptions{
Tokens: tokens,
SSHKeys: keys,
},
)
}