/
config.go
234 lines (180 loc) · 3.86 KB
/
config.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package ssh
import (
"fmt"
"os"
"strings"
"github.com/eleven-sh/cli/internal/system"
"github.com/kevinburke/ssh_config"
)
const ConfigFilePerm os.FileMode = 0644
type Config struct {
configFilePath string
}
func NewConfig(configFilePath string) Config {
return Config{
configFilePath: configFilePath,
}
}
func NewConfigWithDefaultConfigFilePath() Config {
return NewConfig(system.DefaultSSHConfigFilePath())
}
func (c Config) AddOrReplaceHost(
hostKey string,
hostName string,
identityFile string,
user string,
port int64,
) error {
cfg, err := c.parse()
if err != nil {
return err
}
hostPattern, err := ssh_config.NewPattern(hostKey)
if err != nil {
return err
}
hostNodes := []ssh_config.Node{
&ssh_config.Empty{
Comment: " added by Eleven",
},
&ssh_config.KV{
Key: " HostName",
Value: hostName,
},
&ssh_config.KV{
Key: " IdentityFile",
Value: identityFile,
},
&ssh_config.KV{
Key: " User",
Value: user,
},
&ssh_config.KV{
Key: " Port",
Value: fmt.Sprint(port),
},
&ssh_config.KV{
Key: " ForwardAgent",
Value: "yes",
},
}
hostToAdd := &ssh_config.Host{
Patterns: []*ssh_config.Pattern{
hostPattern,
},
Nodes: hostNodes,
}
hostToAddIndex := c.lookupHostIndex(
cfg,
hostKey,
)
if hostToAddIndex == -1 {
cfg.Hosts = append(cfg.Hosts, hostToAdd)
} else {
cfg.Hosts[hostToAddIndex] = hostToAdd
}
return c.save(cfg)
}
func (c Config) UpdateHost(
hostKey string,
hostName *string,
identityFile *string,
user *string,
) error {
cfg, err := c.parse()
if err != nil {
return err
}
updatedHosts := []*ssh_config.Host{}
for _, host := range cfg.Hosts {
// We don't use "host.Matches()"
// here because we don't want the
// wildcard host ("Host *") to match
if len(host.Patterns) == 1 && host.Patterns[0].String() == hostKey {
for _, node := range host.Nodes {
switch t := node.(type) {
case *ssh_config.KV:
lowercasedKey := strings.ToLower(t.Key)
if lowercasedKey == "hostname" && hostName != nil {
t.Value = *hostName
}
if lowercasedKey == "identityfile" && identityFile != nil {
t.Value = *identityFile
}
if lowercasedKey == "user" && user != nil {
t.Value = *user
}
}
}
}
updatedHosts = append(updatedHosts, host)
}
cfg.Hosts = updatedHosts
return c.save(cfg)
}
func (c Config) RemoveHostIfExists(hostKey string) error {
cfg, err := c.parse()
if err != nil {
return err
}
updatedHosts := []*ssh_config.Host{}
for _, host := range cfg.Hosts {
// We don't use "host.Matches()"
// here because we don't want the
// wildcard host ("Host *") to match
if len(host.Patterns) == 1 && host.Patterns[0].String() == hostKey {
continue
}
updatedHosts = append(updatedHosts, host)
}
cfg.Hosts = updatedHosts
return c.save(cfg)
}
func (c Config) CountEntriesWithHostPrefix(hostPrefix string) (int, error) {
cfg, err := c.parse()
if err != nil {
return 0, err
}
count := 0
for _, host := range cfg.Hosts {
if len(host.Patterns) == 1 &&
strings.HasPrefix(host.Patterns[0].String(), hostPrefix) {
count++
}
}
return count, nil
}
func (c Config) lookupHostIndex(
cfg *ssh_config.Config,
hostKey string,
) int {
for hostIndex, host := range cfg.Hosts {
// We don't use "host.Matches()"
// here because we don't want the
// wildcard host ("Host *") to match
if len(host.Patterns) == 1 && host.Patterns[0].String() == hostKey {
return hostIndex
}
continue
}
return -1
}
func (c Config) parse() (*ssh_config.Config, error) {
f, err := os.OpenFile(
c.configFilePath,
os.O_CREATE|os.O_RDONLY,
ConfigFilePerm,
)
if err != nil {
return nil, err
}
defer f.Close()
return ssh_config.Decode(f)
}
func (c Config) save(cfg *ssh_config.Config) error {
return os.WriteFile(
c.configFilePath,
[]byte(cfg.String()),
ConfigFilePerm,
)
}