-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc-main.go
201 lines (156 loc) · 4.55 KB
/
func-main.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
196
197
198
199
200
201
package main
import (
"bufio"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"fyne.io/fyne/v2/widget"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
func CloneMecchi() LoaderResult[string] {
currentDir, _err := os.Getwd()
if _err != nil {
return LoaderResult[string]{success: false, error: _err.Error()}
}
fullPath := filepath.Join(currentDir, MecchiFolder)
git.PlainClone(fullPath, false, &git.CloneOptions{
URL: MecchiRepo,
})
return LoaderResult[string]{success: true, result: fullPath}
}
func InstallMecchi(mecchiFolder string, firstTimeInstall bool, input *widget.Entry) LoaderResult[string] {
packageJSON := GetScripts(mecchiFolder)
if !packageJSON.success {
return LoaderResult[string]{success: false, error: packageJSON.error}
}
var shell string
var shellArgs []string
if os.Getenv("SHELL") != "" {
shell = os.Getenv("SHELL")
shellArgs = []string{"-c", packageJSON.result.InstallLinux}
} else {
shell = "cmd"
shellArgs = []string{"/c", packageJSON.result.Install}
}
cmd := exec.Command(shell, shellArgs...)
cmd.Dir = mecchiFolder
stdout, err := cmd.StdoutPipe()
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
err = cmd.Start()
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
scanner := bufio.NewScanner(stdout)
go func() {
for scanner.Scan() {
OutputResult(input, scanner.Text())
}
}()
err = cmd.Wait()
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
return LoaderResult[string]{success: true}
}
func LaunchMecchi(mecchiFolder string, input *widget.Entry) LoaderResult[int] {
packageJSON := GetScripts(mecchiFolder)
if !packageJSON.success {
return LoaderResult[int]{success: false, error: packageJSON.error}
}
var shell string
var shellArgs []string
if os.Getenv("SHELL") != "" {
shell = os.Getenv("SHELL")
shellArgs = []string{"-c", packageJSON.result.LaunchLinux}
} else {
shell = "cmd"
shellArgs = []string{"/c", packageJSON.result.Launch}
}
cmd := exec.Command(shell, shellArgs...)
cmd.Dir = mecchiFolder
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_err := cmd.Start()
if _err != nil {
return LoaderResult[int]{success: false, error: _err.Error()}
}
OpenInBrowser(MecchiUrl)
return LoaderResult[int]{success: true, result: cmd.Process.Pid}
}
func KillMecchi(pid int, input *widget.Entry) error {
if runtime.GOOS == "linux" {
OutputResult(input, "Not implemented yet for Linux.")
} else if runtime.GOOS == "windows" {
process, _ := os.FindProcess(pid)
killCmd := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(process.Pid))
killCmd.Stderr = os.Stderr
killCmd.Stdout = os.Stdout
return killCmd.Run()
} else {
OutputResult(input, "Unsupported operating system:")
}
return nil
}
func UpdateMecchi(input *widget.Entry) LoaderResult[string] {
mecchiFolder := MecchiFolderExists()
OutputResult(input, "Trying to locate existing Mecchi folder...")
if !mecchiFolder.success {
return LoaderResult[string]{success: false, error: "Mecchi folder does not exist"}
} else {
OutputResult(input, "Found Mecchi folder at "+mecchiFolder.result)
}
repo, err := git.PlainOpen(mecchiFolder.result)
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
worktree, err := repo.Worktree()
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
err = worktree.Pull(&git.PullOptions{
ReferenceName: plumbing.Main,
})
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
packageJSON := GetScripts(mecchiFolder.result)
if !packageJSON.success {
return LoaderResult[string]{success: false, error: packageJSON.error}
}
var shell string
var shellArgs []string
if os.Getenv("SHELL") != "" {
shell = os.Getenv("SHELL")
shellArgs = []string{"-c", packageJSON.result.UpdateLinux}
} else {
shell = "cmd"
shellArgs = []string{"/c", packageJSON.result.Update}
}
cmd := exec.Command(shell, shellArgs...)
cmd.Dir = mecchiFolder.result
stdout, err := cmd.StdoutPipe()
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
err = cmd.Start()
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
scanner := bufio.NewScanner(stdout)
go func() {
for scanner.Scan() {
OutputResult(input, scanner.Text())
}
}()
err = cmd.Wait()
if err != nil {
return LoaderResult[string]{success: false, error: err.Error()}
}
return LoaderResult[string]{success: true}
}