/
local.go
168 lines (138 loc) · 4.08 KB
/
local.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package storages
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
)
// LocalStorage is an implementation of workspace.Storage which uses local filesystem as storage.
type LocalStorage struct {
// The directory path to store the workspace files.
path string
meta *workspacesMetaData
}
// NewLocalStorage news local workspace storage and init default workspace.
func NewLocalStorage(path string) (*LocalStorage, error) {
s := &LocalStorage{path: path}
// create the workspace directory
if err := os.MkdirAll(s.path, os.ModePerm); err != nil {
return nil, fmt.Errorf("create workspace directory failed, %w", err)
}
// read workspaces metadata
if err := s.readMeta(); err != nil {
return nil, err
}
return s, s.initDefaultWorkspaceIf()
}
func (s *LocalStorage) Get(name string) (*v1.Workspace, error) {
if name == "" {
name = s.meta.Current
}
if !checkWorkspaceExistence(s.meta, name) {
return nil, ErrWorkspaceNotExist
}
content, err := os.ReadFile(filepath.Join(s.path, name+yamlSuffix))
if err != nil {
return nil, fmt.Errorf("read workspace file failed: %w", err)
}
ws := &v1.Workspace{}
if err = yaml.Unmarshal(content, ws); err != nil {
return nil, fmt.Errorf("yaml unmarshal workspace failed: %w", err)
}
ws.Name = name
return ws, nil
}
func (s *LocalStorage) Create(ws *v1.Workspace) error {
if checkWorkspaceExistence(s.meta, ws.Name) {
return ErrWorkspaceAlreadyExist
}
if err := s.writeWorkspace(ws); err != nil {
return err
}
addAvailableWorkspaces(s.meta, ws.Name)
return s.writeMeta()
}
func (s *LocalStorage) Update(ws *v1.Workspace) error {
if ws.Name == "" {
ws.Name = s.meta.Current
}
if !checkWorkspaceExistence(s.meta, ws.Name) {
return ErrWorkspaceNotExist
}
return s.writeWorkspace(ws)
}
func (s *LocalStorage) Delete(name string) error {
if name == "" {
name = s.meta.Current
}
if !checkWorkspaceExistence(s.meta, name) {
return nil
}
if err := os.Remove(filepath.Join(s.path, name+yamlSuffix)); err != nil {
return fmt.Errorf("remove workspace file failed: %w", err)
}
removeAvailableWorkspaces(s.meta, name)
return s.writeMeta()
}
func (s *LocalStorage) GetNames() ([]string, error) {
return s.meta.AvailableWorkspaces, nil
}
func (s *LocalStorage) GetCurrent() (string, error) {
return s.meta.Current, nil
}
func (s *LocalStorage) SetCurrent(name string) error {
if !checkWorkspaceExistence(s.meta, name) {
return ErrWorkspaceNotExist
}
s.meta.Current = name
return s.writeMeta()
}
func (s *LocalStorage) initDefaultWorkspaceIf() error {
if !checkWorkspaceExistence(s.meta, DefaultWorkspace) {
// if there is no default workspace, create one with empty workspace.
if err := s.writeWorkspace(&v1.Workspace{Name: DefaultWorkspace}); err != nil {
return err
}
addAvailableWorkspaces(s.meta, DefaultWorkspace)
}
if s.meta.Current == "" {
s.meta.Current = DefaultWorkspace
}
return s.writeMeta()
}
func (s *LocalStorage) readMeta() error {
content, err := os.ReadFile(filepath.Join(s.path, metadataFile))
if os.IsNotExist(err) {
s.meta = &workspacesMetaData{}
return nil
} else if err != nil {
return fmt.Errorf("read workspace metadata file failed: %w", err)
}
meta := &workspacesMetaData{}
if err = yaml.Unmarshal(content, meta); err != nil {
return fmt.Errorf("yaml unmarshal workspaces metadata failed: %w", err)
}
s.meta = meta
return nil
}
func (s *LocalStorage) writeMeta() error {
content, err := yaml.Marshal(s.meta)
if err != nil {
return fmt.Errorf("yaml marshal workspaces metadata failed: %w", err)
}
if err = os.WriteFile(filepath.Join(s.path, metadataFile), content, os.ModePerm); err != nil {
return fmt.Errorf("write workspaces metadata file failed: %w", err)
}
return nil
}
func (s *LocalStorage) writeWorkspace(ws *v1.Workspace) error {
content, err := yaml.Marshal(ws)
if err != nil {
return fmt.Errorf("yaml marshal workspace failed: %w", err)
}
if err = os.WriteFile(filepath.Join(s.path, ws.Name+yamlSuffix), content, os.ModePerm); err != nil {
return fmt.Errorf("write workspace file failed: %w", err)
}
return nil
}