-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathhost.go
60 lines (52 loc) · 1.15 KB
/
host.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
package host
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/cnabio/cnab-go/secrets"
)
const (
SourceEnv = "env"
SourceCommand = "command"
SourcePath = "path"
SourceValue = "value"
)
var _ secrets.Store = &SecretStore{}
type SecretStore struct{}
func (h *SecretStore) Resolve(keyName string, keyValue string) (string, error) {
// Precedence is command, path, env, value
switch strings.ToLower(keyName) {
case SourceCommand:
data, err := execCmd(keyValue)
if err != nil {
return "", err
}
return string(data), nil
case SourcePath:
data, err := ioutil.ReadFile(os.ExpandEnv(keyValue))
if err != nil {
return "", err
}
return string(data), nil
case SourceEnv:
var ok bool
data, ok := os.LookupEnv(keyValue)
if !ok {
return "", fmt.Errorf("environment variable %s is not defined", keyName)
}
return data, nil
case SourceValue:
return keyValue, nil
default:
return "", fmt.Errorf("invalid value source: %s", keyName)
}
}
func execCmd(cmd string) ([]byte, error) {
parts := strings.Split(cmd, " ")
c := parts[0]
args := parts[1:]
run := exec.Command(c, args...)
return run.CombinedOutput()
}