-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathshell.go
87 lines (78 loc) · 2.1 KB
/
shell.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
package models
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
const (
defaultEnabled = true
defaultHost = "localhost"
defaultPort = 22
defaultIdentity = "default"
defaultType = "ssh"
defaultHTTPS = true
defaultInsecure = false
defaultNamespace = "default"
defaultContainer = ""
defaultPod = ""
)
type Shell struct {
Name string `json:"name"`
Host string `json:"host"`
Port int `json:"port"`
IdentityName string `json:"identity"`
Type string `json:"type"`
Ciphers []string `json:"ciphers"`
HTTPS bool `json:"https"`
Insecure bool `json:"insecure"`
Enabled bool `json:"enabled"`
Groups []string `json:"groups"`
Proxy Proxy `json:"proxy"`
Tunnel Tunnel `json:"tunnel"`
Namespace string `json:"namespace"`
Container string `json:"container"`
Pod string `json:"pod"`
Identity *Identity `json:"-"`
Path string `json:"-"`
}
func LoadShell(path string, idents Identities) (err error, shell Shell) {
shell = Shell{
Enabled: defaultEnabled,
Path: path,
Host: defaultHost,
Port: defaultPort,
Type: defaultType,
IdentityName: defaultIdentity,
HTTPS: defaultHTTPS,
Insecure: defaultInsecure,
Namespace: defaultNamespace,
Container: defaultContainer,
Pod: defaultPod,
}
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
raw, err := ioutil.ReadAll(file)
if err != nil {
return
}
if err = json.Unmarshal(raw, &shell); err != nil {
return fmt.Errorf("error decoding '%s': %s", path, err), shell
} else if ident, found := idents[shell.IdentityName]; !found {
return fmt.Errorf("shell '%s' referenced an unknown identity '%s'", path, shell.IdentityName), shell
} else {
shell.Identity = &ident
}
return
}
func (sh Shell) Save() error {
if data, err := json.MarshalIndent(sh, "", " "); err != nil {
return err
} else if err = ioutil.WriteFile(sh.Path, data, 0644); err != nil {
return err
}
return nil
}