forked from abiosoft/colima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirs.go
103 lines (86 loc) · 2.46 KB
/
dirs.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package config
import (
"fmt"
"os"
"path/filepath"
"sync"
"github.com/abiosoft/colima/util/fsutil"
"github.com/abiosoft/colima/util/osutil"
"github.com/abiosoft/colima/util/shautil"
"github.com/sirupsen/logrus"
)
// requiredDir is a directory that must exist on the filesystem
type requiredDir struct {
once sync.Once
// dir is a func to enable deferring the value of the directory
// until execution time.
// if dir() returns an error, a fatal error is triggered.
dir func() (string, error)
}
// Dir returns the directory path.
// It ensures the directory is created on the filesystem by calling
// `mkdir` prior to returning the directory path.
func (r *requiredDir) Dir() string {
dir, err := r.dir()
if err != nil {
logrus.Fatal(fmt.Errorf("cannot fetch required directory: %w", err))
}
r.once.Do(func() {
if err := fsutil.MkdirAll(dir, 0755); err != nil {
logrus.Fatal(fmt.Errorf("cannot make required directory: %w", err))
}
})
return dir
}
var (
configDir = requiredDir{
dir: func() (string, error) {
dir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ".colima", profile.ShortName), nil
},
}
cacheDir = requiredDir{
dir: func() (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "colima"), nil
},
}
templatesDir = requiredDir{
dir: func() (string, error) {
dir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ".colima", "_templates"), nil
},
}
wrapperDir = requiredDir{
dir: func() (string, error) {
dir, err := os.UserHomeDir()
if err != nil {
return "", err
}
// generate unique directory for the current binary
uniqueDir := shautil.SHA1(osutil.Executable())
return filepath.Join(dir, ".colima", "_wrapper", uniqueDir.String()), nil
},
}
)
// Dir returns the configuration directory.
func Dir() string { return configDir.Dir() }
// File returns the path to the config file.
func File() string { return configFile() }
// CacheDir returns the cache directory.
func CacheDir() string { return cacheDir.Dir() }
// TemplatesDir returns the templates' directory.
func TemplatesDir() string { return templatesDir.Dir() }
// WrapperDir returns the qemu wrapper directory.
func WrapperDir() string { return wrapperDir.Dir() }
const ConfigFileName = "colima.yaml"
func configFile() string { return filepath.Join(configDir.Dir(), ConfigFileName) }