-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
65 lines (54 loc) · 1.44 KB
/
config.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
package sicher
import (
"fmt"
"log"
"os"
)
// configure reads the credentials file and sets the environment variables
func (s *sicher) configure() {
if s.Environment == "" {
fmt.Println("Environment not set")
return
}
// read the encryption key
strKey, err := s.getEncryptionKey(fmt.Sprintf("%s%s.key", s.Path, s.Environment))
if err != nil {
fmt.Println(err)
return
}
// read the encrypted credentials file
credFile, err := os.ReadFile(fmt.Sprintf("%s%s.enc", s.Path, s.Environment))
if err != nil {
fmt.Printf("encrypted credentials file (%s.enc) is not available. Create one by running the cli with init flag.\n", s.Environment)
return
}
encFile := string(credFile)
// if file already exists, decode and decrypt it
nonce, fileText, err := decodeFile(encFile)
if err != nil {
fmt.Printf("Error decoding encryption file: %s\n", err)
return
}
if nonce == nil || fileText == nil {
fmt.Println("Error decoding encryption file: encrypted file is invalid")
return
}
plaintext, err := decrypt(strKey, nonce, fileText)
if err != nil {
fmt.Println("Error decrypting file:", err)
return
}
err = parseConfig(plaintext, s.data, s.envStyle)
if err != nil {
fmt.Printf("Error parsing env file: %s\n", err)
return
}
}
func (s *sicher) setEnv() {
for k, v := range s.data {
err := os.Setenv(k, fmt.Sprintf("%v", v))
if err != nil {
log.Fatalf("Error setting environment variable key %s: %s\n", k, err)
}
}
}