This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
forked from dschaub/whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whisper.go
74 lines (54 loc) · 1.43 KB
/
whisper.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
package main
import (
"flag"
"fmt"
"go.mozilla.org/sops/decrypt"
"gopkg.in/yaml.v2"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
file := flag.String("f", "", "path to SOPS file")
passThroughEnv := flag.Bool("p", false, "pass the current environment through")
flag.Parse()
command := strings.Join(flag.Args(), " ")
if *file == "" || len(command) == 0 {
log.Fatal("usage: whisper -f <file> [-p] <command>")
}
path, err := filepath.Abs(*file)
if err != nil {
log.Fatalf("Couldn't determine absolute path to file: %v", err)
}
decryptedEnvironment, err := decryptEnvironmentVars(path)
if err != nil {
log.Fatalf("Failed to decrypt environment vars: %v", err)
}
cmd := exec.Command("sh", "-c", command)
if *passThroughEnv {
cmd.Env = os.Environ()
}
cmd.Env = append(cmd.Env, decryptedEnvironment...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func decryptEnvironmentVars(sopsFile string) ([]string, error) {
yamlBytes, err := decrypt.File(sopsFile, "yaml")
if err != nil {
return nil, err
}
secrets := make(map[string]interface{})
err = yaml.Unmarshal(yamlBytes, &secrets)
if err != nil {
return nil, err
}
environmentStrings := make([]string, 1)
environmentMap := secrets["environment"].(map[interface{}]interface{})
for k, v := range environmentMap {
environmentStrings = append(environmentStrings, fmt.Sprintf("%s=%v", k, v))
}
return environmentStrings, nil
}