/
git-id_hotdriver_xyz.go
141 lines (124 loc) · 3.41 KB
/
git-id_hotdriver_xyz.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 ssh_config
import (
"strings"
"github.com/jtagcat/git-id/pkg/encoding/ssh_config/pkg"
)
// WARN: made _only_ for git-id, may break
func (c *Config) GID_PreappendRootComment(s string) {
c.cfg = append([]RawTopLevel{{Comment: s}}, c.cfg...)
}
// WARN: made _only_ for git-id, may break
func (c *Config) GID_PreappendInclude(i string) {
c.cfg = append(
[]RawTopLevel{{Key: "Include", Values: []RawValue{{Value: i, Quoted: 2}}}},
c.cfg...)
}
// WARN: made _only_ for git-id, may break
type GIDTree struct {
Values []string
Children GitIDCommonChildren
}
// WARN: made _only_ for git-id, may break
// suffix: value is handled as a suffix
func (c *Config) GID_RootObjects(key string, values []string, suffix bool) (matches int, trees []GIDTree) {
for _, root := range c.cfg {
if strings.EqualFold(root.Key, key) &&
valuesMatch(root.Values, values, suffix) {
var foundValues []string
for _, v := range root.Values {
foundValues = append(foundValues, v.Value)
}
trees = append(trees, GIDTree{
Values: foundValues,
Children: childsDecode(root.Children),
})
matches++
}
}
return matches, trees
}
// order and len() matters, "" means ignore
// suffix: value is handled as a suffix
func valuesMatch(against []RawValue, values []string, suffix bool) bool {
if len(against) != len(values) {
return false
}
for i, a := range against {
if values[i] != "" {
if suffix {
if !strings.HasSuffix(strings.ToLower(a.Value), strings.ToLower(values[i])) {
return false
}
} else {
if !strings.EqualFold(a.Value, values[i]) {
return false
}
}
}
}
return true
}
// WARN: made _only_ for git-id, may break
// Brutal implementation of override and don't care anything for the sake of time
func (c *Config) GID_RootObjectSetFirst(key string, values []string, firstValueIsSubKey bool, childs GitIDCommonChildren) {
children := childsEncode(childs)
for _, root := range c.cfg {
if strings.EqualFold(root.Key, key) {
var valuesComparable []string
for _, v := range root.Values {
valuesComparable = append(valuesComparable, v.Value)
}
if pkg.EqualFoldSlice(valuesComparable, values) {
root.Children = children
return
}
}
}
var valuesInjectable []RawValue
for _, v := range values {
valuesInjectable = append(valuesInjectable, RawValue{Value: v})
}
c.cfg = append(c.cfg, RawTopLevel{
Key: key,
Values: valuesInjectable,
Children: children,
})
}
// WARN: made _only_ for git-id, may break
func (c *Config) GID_RootObjectRemoveFirst(key string, values []string) (ok bool) {
i := func(config []RawTopLevel) int {
for i, root := range config {
if strings.EqualFold(root.Key, key) {
var valuesComparable []string
for _, v := range root.Values {
valuesComparable = append(valuesComparable, v.Value)
}
if pkg.EqualFoldSlice(valuesComparable, values) {
return i
}
}
}
return -1
}(c.cfg)
if i == -1 {
return false
}
c.cfg = append(c.cfg[:i], c.cfg[i+1:]...)
return true
}
//
// Match OriginalHost github.com
// IdentityFile ~/.ssh/id_rsa
//
// Host *.gh.git-id
// Hostname github.com
// #XDescription "iz GitHub"
//
// Host jc.gh.git-id
// IdentityFile ~/.ssh/id_rsa # this is redundant with defaults, IdentityFile is used for matching the default to an ident
// #XGitConfig user.name jtagcat
// #XGitConfig user.email blah
// #XDescription uwu
// Host w.gh.git-id
// IdentityFile ~/.ssh/work_sk
//