-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
51 lines (47 loc) · 1.38 KB
/
file.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
package starenv
import (
"errors"
"io/ioutil"
"os/user"
"path/filepath"
"strings"
)
// File derefs a file path to the content of the file. Default tag for this
// derefer is "file".
type File struct {
// ExpandHome when set to true will replace "~/" with the current user's home
// directory just like a standard POSIX shell would do.
ExpandHome bool
}
// Deref treats ref as path to a file and returns the content of the file.
func (f *File) Deref(ref string) (string, error) {
if f.ExpandHome && strings.HasPrefix(ref, "~/") {
u, err := user.Current()
if err != nil {
return "", errors.New("failed to get current user: " + err.Error())
}
ref = filepath.Join(u.HomeDir, ref[2:])
}
b, err := ioutil.ReadFile(ref)
if err != nil {
return "", err
}
return string(b), nil
}
// TempFile creates a temporary file and stores the content of ref in it and
// returns its path. This is useful for storing secrets in files without
// having their content in the env var. Under most OSes, temp directory content
// is held in memory and isn't written to disk, this ensures a further layer of
// security for secrets.
// Note that the file is NOT deleted automatically.
func TempFile(v string) (string, error) {
f, err := ioutil.TempFile("", "starenv-*")
if err != nil {
return "", err
}
defer f.Close()
if _, err := f.WriteString(v); err != nil {
return "", err
}
return f.Name(), nil
}