-
Notifications
You must be signed in to change notification settings - Fork 14
/
octants.go
81 lines (71 loc) · 1.68 KB
/
octants.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
package workspaces
import (
"path/filepath"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/jenkins-x/octant-jx/pkg/common/files"
)
type Octants struct {
Octants []Octant
fileName string
}
type Octant struct {
Name string `json:"name"`
Dir string `json:"dir"`
KubeConfigPath string `json:"kubeConfig"`
Port int `json:"port"`
Admin bool `json:"admin"`
}
func NewOctants() (*Octants, error) {
o := &Octants{
fileName: filepath.Join(files.JXOPSHomeDir(), "octants.yaml"),
}
err := yamls.LoadFile(o.fileName, &o.Octants)
if err != nil {
return nil, err
}
return o, nil
}
// Get gets the octant values
func (o *Octants) Get(workspace *Workspace) Octant {
name := workspace.Name
var answer Octant
for _, r := range o.Octants {
if r.Name == name {
answer = r
break
}
}
answer.Name = name
if workspace.Default {
answer.Admin = true
}
if workspace.Port > 0 && answer.Port <= 0 {
answer.Port = workspace.Port
}
return answer
}
// Set updates the octant values returning true if its a new octant
func (o *Octants) Set(values Octant) bool {
for i, r := range o.Octants {
if r.Name == values.Name {
o.Octants[i] = values
return false
}
}
o.Octants = append(o.Octants, values)
return true
}
func (o *Octants) Save() error {
return SaveOctants(o.Octants)
}
func LoadOctants() ([]Octant, error) {
answer := []Octant{}
fileName := filepath.Join(files.JXOPSHomeDir(), "octants.yaml")
err := yamls.LoadFile(fileName, &answer)
return answer, err
}
func SaveOctants(octants []Octant) error {
fileName := filepath.Join(files.JXOPSHomeDir(), "octants.yaml")
err := yamls.SaveFile(octants, fileName)
return err
}