forked from okteto/okteto
/
storage.go
151 lines (128 loc) · 3.07 KB
/
storage.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
package storage
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
yaml "gopkg.in/yaml.v2"
)
const (
version = "1.0"
)
var (
stPath string
ErrAlreadyRunning = fmt.Errorf("up-already-running")
)
//Storage represents the cli state
type Storage struct {
path string
Version string `yaml:"version,omitempty"`
Services map[string]Service `yaml:"services,omitempty"`
}
//Service represents the information about a dev mode service
type Service struct {
Folder string `yaml:"folder,omitempty"`
Syncthing string `yaml:"syncthing,omitempty"`
Container string `yaml:"container,omitempty"`
}
func init() {
stPath = path.Join(os.Getenv("HOME"), ".cnd", ".state")
}
func load() (*Storage, error) {
var s Storage
s.path = stPath
s.Version = version
s.Services = map[string]Service{}
if _, err := os.Stat(stPath); os.IsNotExist(err) {
return &s, nil
}
bytes, err := ioutil.ReadFile(stPath)
if err != nil {
return nil, fmt.Errorf("error reading the storage file: %s", err.Error())
}
err = yaml.Unmarshal(bytes, &s)
if err != nil {
return nil, fmt.Errorf("error unmarshalling the storage file: %s", err.Error())
}
return &s, nil
}
//Insert inserts a new service entry
func Insert(namespace, deployment, container, folder, host string) error {
s, err := load()
if err != nil {
return err
}
fullName := fmt.Sprintf("%s/%s", namespace, deployment)
svc, err := newService(folder, container, host)
if err != nil {
return err
}
if svc2, ok := s.Services[fullName]; ok {
if svc2 == svc {
return nil
}
return ErrAlreadyRunning
}
s.Services[fullName] = svc
return s.save()
}
//Get gets a service entry
func Get(namespace, deployment string) (*Service, error) {
s, err := load()
if err != nil {
return nil, err
}
fullName := fmt.Sprintf("%s/%s", namespace, deployment)
svc, ok := s.Services[fullName]
if !ok {
return nil, fmt.Errorf("there is no active dev mode for '%s'", fullName)
}
return &svc, nil
}
//Delete deletes a service entry
func Delete(namespace, deployment string) error {
s, err := load()
if err != nil {
return err
}
fullName := fmt.Sprintf("%s/%s", namespace, deployment)
delete(s.Services, fullName)
return s.save()
}
//All returns the active dev mode services
func All() map[string]Service {
s, err := load()
if err != nil {
return nil
}
return s.Services
}
func (s *Storage) save() error {
bytes, err := yaml.Marshal(s)
if err != nil {
return fmt.Errorf("error marshalling storage: %s", err.Error())
}
err = ioutil.WriteFile(s.path, bytes, 0644)
if err != nil {
return fmt.Errorf("error writing storage: %s", err.Error())
}
return nil
}
func fixPath(originalPath string) (string, error) {
if filepath.IsAbs(originalPath) {
return originalPath, nil
}
folder, err := os.Getwd()
if err != nil {
return "", err
}
return path.Join(folder, originalPath), nil
}
func newService(folder, container, host string) (Service, error) {
absFolder, err := fixPath(folder)
if err != nil {
return Service{}, err
}
return Service{Folder: absFolder, Syncthing: host, Container: container}, nil
}