-
Notifications
You must be signed in to change notification settings - Fork 13
/
dirs.go
79 lines (71 loc) · 2.03 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
package e2e
import (
"os"
"path/filepath"
)
// Dirs represents directories that are temporarily created for this end-to-end testing session
type Dirs struct {
Base string
// Config is where configuration files are stored
Config string
// Cache is the directory where cached files including downloaded artifacts are stored
Cache string
// Bin is the directory where executables are stored
Bin string
// Work is the working directory where the activestate.yaml file would live, and that is the PWD for tested console processes
Work string
// DefaultBin is the bin directory for our default installation
DefaultBin string
// SockRoot is the directory for the state service's socket file
SockRoot string
// HomeDir is used as the test user's home directory
HomeDir string
// TempDir is the directory where temporary files are stored
TempDir string
}
// NewDirs creates all temporary directories
func NewDirs(base string) (*Dirs, error) {
if base == "" {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return nil, err
}
base = tmpDir
}
cache := filepath.Join(base, "cache")
config := filepath.Join(base, "config")
bin := filepath.Join(base, "bin")
work := filepath.Join(base, "work")
defaultBin := filepath.Join(base, "cache", "bin")
sockRoot := filepath.Join(base, "sock")
homeDir := filepath.Join(base, "home")
tempDir := filepath.Join(base, "temp")
subdirs := []string{config, cache, bin, work, defaultBin, sockRoot, homeDir, tempDir}
for _, subdir := range subdirs {
if err := os.MkdirAll(subdir, 0700); err != nil {
return nil, err
}
}
dirs := Dirs{
Base: base,
Config: config,
Cache: cache,
Bin: bin,
Work: work,
DefaultBin: defaultBin,
SockRoot: sockRoot,
HomeDir: homeDir,
TempDir: tempDir,
}
return &dirs, nil
}
// Close removes the temporary directories
func (d *Dirs) Close() error {
subdirs := []string{d.Bin, d.Config, d.Work, d.Cache}
for _, subdir := range subdirs {
if err := os.RemoveAll(subdir); err != nil {
return err
}
}
return nil
}