-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
77 lines (59 loc) · 1.26 KB
/
result.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
package envdef
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/fatih/color"
)
type Result struct {
InsertSlice InsertSlice
UpdateSlice UpdateSlice
DeleteSlice DeleteSlice
NoChangeSlice NoChangeSlice
}
func (r *Result) Write() error {
result := append(r.InsertSlice, r.UpdateSlice...)
result = append(result, r.DeleteSlice...)
result = append(result, r.NoChangeSlice...)
sort.Strings(result)
b := []byte(strings.Join(result, "\n"))
return ioutil.WriteFile(".env.new", b, os.ModePerm)
}
func (r *Result) Print() {
r.InsertSlice.Print()
r.UpdateSlice.Print()
r.DeleteSlice.Print()
}
type InsertSlice []string
func (es InsertSlice) Print() {
iconInsert := "+"
for _, e := range es {
color.Yellow(iconFormat(iconInsert, e))
}
}
type UpdateSlice []string
func (es UpdateSlice) Print() {
iconUpdate := "~"
for _, e := range es {
color.Cyan(iconFormat(iconUpdate, e))
}
}
type DeleteSlice []string
func (es DeleteSlice) Print() {
iconDelete := "-"
for _, e := range es {
color.Red(iconFormat(iconDelete, e))
}
}
type NoChangeSlice []string
func (es NoChangeSlice) Print() {
for _, e := range es {
color.White(e)
}
}
func iconFormat(icon, msg string) string {
iconFormat := "%v %v"
return fmt.Sprintf(iconFormat, icon, msg)
}