-
Notifications
You must be signed in to change notification settings - Fork 42
/
reader.go
84 lines (74 loc) · 2.21 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package parse
import (
"fmt"
"path/filepath"
"strings"
"github.com/anz-bank/golden-retriever/retriever"
"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"
const GitRootMarker = ".git"
func NewReader(fs afero.Fs) (reader.Reader, error) {
pinner, err := NewPinner(fs)
if err != nil {
return nil, err
}
return remotefs.NewWithRetriever(
filesystem.New(fs),
pinner,
), nil
}
func NewPinner(fs afero.Fs) (retriever.Retriever, error) {
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],
}
}
}
auth := &git.AuthOptions{Tokens: tokens, SSHKeys: keys}
// If a modules.yaml file already exists, use it. Otherwise use a caching Git retriever without
// pinning remote imports.
pinnerPath := filepath.Join(SyslRootDir(fs), "modules.yaml")
if ok, err := afero.Exists(fs, pinnerPath); ok && err == nil {
return remotefs.NewPinnerGitRetriever(pinnerPath, auth)
}
return git.NewWithCache(auth, git.NewPlainFscache(remotefs.CacheDir)), nil
}
func SyslRootDir(fs afero.Fs) string {
root := "."
if v, is := fs.(*syslutil.ChrootFs); is {
root = v.Root()
}
return filepath.Join(root, SyslRootMarker)
}