-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias.go
54 lines (47 loc) · 997 Bytes
/
alias.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type alias struct {
name, value string
}
func (a alias) persist() error {
aliases, err := readAll()
if err != nil {
return err
}
if aliases.exists(a.name) {
if aliases[a.name] == a.value {
return nil
}
old := alias{name: a.name, value: aliases[a.name]}
if yes := old.shouldReplaceWith(a); yes {
return aliases.modify(a)
}
return nil
}
return aliases.append(a)
}
func (a alias) remove() error {
aliases, err := readAll()
if err != nil {
return err
}
return aliases.remove(a.name)
}
func (a alias) string() string {
return fmt.Sprintf(`alias %s="%s"`, a.name, a.value)
}
func (a alias) shouldReplaceWith(new alias) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("old value: %s\nnew value: %s\ndo you want to update? [y/n] ", a.value, new.value)
update, _ := reader.ReadString('\n')
update = strings.Replace(update, "\n", "", -1) // TODO
if update == "n" {
return false
}
return true
}