/
file_operations.go
59 lines (44 loc) · 1.03 KB
/
file_operations.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
package cmd
import (
"os"
"path/filepath"
)
// getSecretPath use it to get the file location, the key is also the file name
func getSecretPath(key string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
p := filepath.Join(home, dirSecure, key)
return p, nil
}
func createSecretDirectory() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
return os.Mkdir(filepath.Join(home, dirSecure), os.ModePerm)
}
func getSecretDirectory() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
p := filepath.Join(home, dirSecure)
return p, nil
}
func getOrCreateSecureFile(key string) (*os.File, error) {
p, err := getSecretPath(key)
if err != nil {
return nil, err
}
// check if the file exists. But not append anything, just overwrite.
return os.OpenFile(p, os.O_CREATE|os.O_WRONLY, os.ModePerm)
}
func readSecureFile(key string) ([]byte, error) {
p, err := getSecretPath(key)
if err != nil {
return nil, err
}
return os.ReadFile(p)
}