This repository has been archived by the owner on Jan 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
container.go
105 lines (93 loc) · 2.33 KB
/
container.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
104
105
package container
import (
"errors"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/AOSC-Dev/ciel/internal/abstract"
"github.com/AOSC-Dev/ciel/internal/container/instance"
"github.com/AOSC-Dev/ciel/internal/utils"
)
const (
DistDirName = "dist"
InstDirName = "instances"
)
var (
ErrInvalidInstName = errors.New("invalid instance name")
)
type Container struct {
Parent abstract.Ciel
BasePath string
}
func (i *Container) DistDir() string {
return path.Join(i.BasePath, DistDirName)
}
func (i *Container) InstDir() string {
return path.Join(i.BasePath, InstDirName)
}
func (i *Container) Init() {
utils.MustMkdir(i.BasePath)
utils.MustMkdir(i.DistDir())
utils.MustMkdir(i.InstDir())
}
func (i *Container) Instance(name string) *instance.Instance {
return &instance.Instance{Parent: i, BasePath: i.InstDir(), Name: name}
}
func (i *Container) AddInst(name string) error {
if strings.ContainsAny(name, "/\\ ") {
return ErrInvalidInstName
}
utils.MustMkdir(path.Join(i.InstDir(), name))
return i.Instance(name).Init()
}
func (i *Container) DelInst(name string) error {
i.Instance(name).RunLock().Remove()
i.Instance(name).FileSystemLock().Remove()
return os.RemoveAll(path.Join(i.InstDir(), name))
}
func (i *Container) InstExists(name string) bool {
if strings.ContainsAny(name, "/\\ ") {
return false
}
instDir := path.Join(i.InstDir(), name)
if instDir == path.Clean(i.InstDir()) {
return false
}
if stat, err := os.Stat(instDir); err != nil || !stat.IsDir() {
return false
}
return true
}
func (i *Container) CheckInst(name string) {
if name == "" {
log.Fatalln("you must specify a instance")
}
if !i.InstExists(name) {
log.Fatalln("instance '" + name + "' does not exist")
}
}
func (i *Container) GetAll() []*instance.Instance {
list := i.GetAllNames()
var instList []*instance.Instance
for _, name := range list {
instList = append(instList, i.Instance(name))
}
return instList
}
func (i *Container) GetAllNames() []string {
subDirs, err := ioutil.ReadDir(i.InstDir())
if err != nil {
log.Panic(err)
}
var subDirNames []string
for _, subDirs := range subDirs {
if subDirs.IsDir() {
subDirNames = append(subDirNames, subDirs.Name())
}
}
return subDirNames
}
func (i *Container) GetBasePath() string { return i.BasePath }
func (i *Container) GetCiel() abstract.Ciel { return i.Parent }