forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias_config.go
More file actions
60 lines (46 loc) · 1003 Bytes
/
alias_config.go
File metadata and controls
60 lines (46 loc) · 1003 Bytes
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
package config
import (
"fmt"
)
type AliasConfig struct {
ConfigMap
Parent Config
}
func (a *AliasConfig) Get(alias string) (string, bool) {
if a.Empty() {
return "", false
}
value, _ := a.GetStringValue(alias)
return value, value != ""
}
func (a *AliasConfig) Add(alias, expansion string) error {
err := a.SetStringValue(alias, expansion)
if err != nil {
return fmt.Errorf("failed to update config: %w", err)
}
err = a.Parent.Write()
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
return nil
}
func (a *AliasConfig) Delete(alias string) error {
a.RemoveEntry(alias)
err := a.Parent.Write()
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
return nil
}
func (a *AliasConfig) All() map[string]string {
out := map[string]string{}
if a.Empty() {
return out
}
for i := 0; i < len(a.Root.Content)-1; i += 2 {
key := a.Root.Content[i].Value
value := a.Root.Content[i+1].Value
out[key] = value
}
return out
}