-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
141 lines (117 loc) · 2.79 KB
/
state.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
package state
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/edgefarm/edgefarm/pkg/shared"
)
type CurrentState struct {
Netbird NetbirdState `json:"netbird"`
filePath string `json:"-"`
}
type NetbirdState struct {
FullyConfigured bool `json:"fully_configured"`
NetbirdToken string `json:"netbird_token"`
NetbirdSetupKey string `json:"netbird_setup_key"`
NetbirdSetupKeyID string `json:"netbird_setup_key_id"`
NetbirdGroupID string `json:"netbird_group_id"`
NetbirdRouteID string `json:"netbird_route_id"`
}
func GetState(path string) (*CurrentState, error) {
if path == "" {
return nil, fmt.Errorf("path not set")
}
realPath, err := shared.Expand(path)
if err != nil {
return nil, err
}
tmp := &CurrentState{
filePath: realPath,
}
currentState, err := importFromFile(tmp.filePath)
if err != nil {
currentState = tmp
}
currentState.filePath = realPath
dir := filepath.Dir(currentState.filePath)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, 0755)
if err != nil {
return nil, err
}
}
return currentState, nil
}
func (s *CurrentState) IsFullyConfigured() bool {
return s.Netbird.FullyConfigured
}
func (s *CurrentState) SetFullyConfigured() {
s.Netbird.FullyConfigured = true
s.Export()
}
func (s *CurrentState) SetNetbirdSetupKey(key string) {
s.Netbird.NetbirdSetupKey = key
s.Export()
}
func (s *CurrentState) GetNetbirdSetupKey() string {
return s.Netbird.NetbirdSetupKey
}
func (s *CurrentState) SetNetbirdToken(token string) {
s.Netbird.NetbirdToken = token
s.Export()
}
func (s *CurrentState) GetNetbirdToken() string {
return s.Netbird.NetbirdToken
}
func (s *CurrentState) SetNetbirdSetupKeyID(id string) {
s.Netbird.NetbirdSetupKeyID = id
s.Export()
}
func (s *CurrentState) GetNetbirdSetupKeyID() string {
return s.Netbird.NetbirdSetupKeyID
}
func (s *CurrentState) SetNetbirdGroupID(id string) {
s.Netbird.NetbirdGroupID = id
s.Export()
}
func (s *CurrentState) GetNetbirdGroupID() string {
return s.Netbird.NetbirdGroupID
}
func (s *CurrentState) SetNetbirdRouteID(id string) {
s.Netbird.NetbirdRouteID = id
s.Export()
}
func (s *CurrentState) GetNetbirdRouteID() string {
return s.Netbird.NetbirdRouteID
}
func (s *CurrentState) Export() error {
j, err := json.Marshal(s)
if err != nil {
return err
}
err = os.WriteFile(s.filePath, j, 0644)
if err != nil {
return err
}
return nil
}
func importFromFile(path string) (*CurrentState, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
c := &CurrentState{}
err = json.Unmarshal([]byte(content), c)
if err != nil {
return nil, err
}
return c, nil
}
func (s *CurrentState) Clear() {
s.Netbird = NetbirdState{}
s.Export()
}
func (s *CurrentState) Delete() error {
return os.Remove(s.filePath)
}