-
Notifications
You must be signed in to change notification settings - Fork 18
/
install.go
195 lines (181 loc) Β· 4.82 KB
/
install.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
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/mritd/gitflow-toolkit/v2/ui"
tea "github.com/charmbracelet/bubbletea"
"github.com/mitchellh/go-homedir"
)
func install(dir string, withHook bool) error {
home, err := homedir.Dir()
if err != nil {
return err
}
toolKitHome := filepath.Join(home, ".gitflow-toolkit")
toolKitPath := filepath.Join(dir, "gitflow-toolkit")
toolKitHooks := filepath.Join(toolKitHome, "hooks")
links := linkPath(dir)
m := ui.NewMultiTaskModelWithTasks([]ui.Task{
{
Title: "Clean install dir...",
Func: func() error { return os.RemoveAll(toolKitHome) },
},
{
Title: "Clean symlinks...",
Func: func() error {
for _, link := range links {
if _, err := os.Lstat(link); err == nil {
err := os.RemoveAll(link)
if err != nil {
return fmt.Errorf("π failed to remove symlink: %s: %s", link, err)
}
} else if !os.IsNotExist(err) {
return fmt.Errorf("π failed to get symlink info: %s: %s", link, err)
}
}
return nil
},
},
{
Title: "Unset commit hooks...",
Func: func() error {
_, _ = git("config", "--global", "--unset", "core.hooksPath")
return nil
},
},
{
Title: "Create toolkit home...",
Func: func() error {
return os.MkdirAll(toolKitHome, 0755)
},
},
{
Title: "Install executable file...",
Func: func() error {
binPath, err := exec.LookPath(os.Args[0])
if err != nil {
return fmt.Errorf("π failed to get bin file info: %s: %s", os.Args[0], err)
}
currentFile, err := os.Open(binPath)
if err != nil {
return fmt.Errorf("π failed to get bin file info: %s: %s", binPath, err)
}
defer func() { _ = currentFile.Close() }()
installFile, err := os.OpenFile(filepath.Join(dir, "gitflow-toolkit"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("π failed to create bin file: %s: %s", filepath.Join(toolKitHome, "gitflow-toolkit"), err)
}
defer func() { _ = installFile.Close() }()
_, err = io.Copy(installFile, currentFile)
if err != nil {
return fmt.Errorf("π failed to copy file: %s: %s", filepath.Join(toolKitHome, "gitflow-toolkit"), err)
}
return nil
},
},
{
Title: "Create symlink...",
Func: func() error {
for _, link := range links {
err := os.Symlink(toolKitPath, link)
if err != nil {
return fmt.Errorf("π failed to create symlink: %s: %s", link, err)
}
}
return nil
},
},
{
Title: "Set commit hooks...",
Func: func() error {
if !withHook {
return ui.WarnErr{Message: "Hook is not installed!"}
}
err := os.MkdirAll(toolKitHooks, 0755)
if err != nil {
return fmt.Errorf("π failed to create hooks dir: %s: %s", toolKitHooks, err)
}
err = os.Symlink(toolKitPath, filepath.Join(toolKitHooks, "commit-msg"))
if err != nil {
return fmt.Errorf("π failed to create commit hook synlink: %s: %s", filepath.Join(toolKitHooks, "commit-msg"), err)
}
_, _ = git("config", "--global", "core.hooksPath", toolKitHooks)
return nil
},
},
{
Title: "Install success...",
Func: func() error { return nil },
},
})
return tea.NewProgram(m).Start()
}
func uninstall(dir string) error {
home, err := homedir.Dir()
if err != nil {
return err
}
toolKitHome := filepath.Join(home, ".gitflow-toolkit")
toolKitPath := filepath.Join(dir, "gitflow-toolkit")
links := linkPath(dir)
m := ui.NewMultiTaskModelWithTasks([]ui.Task{
{
Title: "Clean install dir...",
Func: func() error { return os.RemoveAll(toolKitHome) },
},
{
Title: "Clean symlinks...",
Func: func() error {
for _, link := range links {
if _, err := os.Lstat(link); err == nil {
err := os.RemoveAll(link)
if err != nil {
return fmt.Errorf("π failed to remove symlink: %s: %s", link, err)
}
} else if !os.IsNotExist(err) {
return fmt.Errorf("π failed to get symlink info: %s: %s", link, err)
}
}
return nil
},
},
{
Title: "Clean bin file...",
Func: func() error {
return os.Remove(toolKitPath)
},
},
{
Title: "Unset commit hooks...",
Func: func() error {
_, _ = git("config", "--global", "--unset", "core.hooksPath")
return nil
},
},
{
Title: "UnInstall success...",
Func: func() error {
return nil
},
},
})
return tea.NewProgram(m).Start()
}
func linkPath(dir string) []string {
return []string{
filepath.Join(dir, "git-ci"),
filepath.Join(dir, "git-feat"),
filepath.Join(dir, "git-fix"),
filepath.Join(dir, "git-docs"),
filepath.Join(dir, "git-style"),
filepath.Join(dir, "git-refactor"),
filepath.Join(dir, "git-test"),
filepath.Join(dir, "git-chore"),
filepath.Join(dir, "git-perf"),
filepath.Join(dir, "git-hotfix"),
filepath.Join(dir, "git-ps"),
}
}