/
util.go
195 lines (159 loc) · 3.72 KB
/
util.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 (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"github.com/kr/pty"
"github.com/mikkeloscar/maze/model"
"github.com/mikkeloscar/maze/repo"
)
// add repo entry to the pacman.conf file specified by path.
func addRepoEntry(path string, r *Repo) error {
arch := "/$arch"
if r.url[len(r.url)-1] == '/' {
arch = arch[1:]
}
// TODO: better handling of SigLevel
entry := fmt.Sprintf("[%s]\nSigLevel = PackageOptional\nServer = %s%s\n",
r.local.Name,
r.url, arch)
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
lines := strings.Split(string(content), "\n")
inserted := false
for _, line := range lines {
if line == "# :INSERT_REPO:" && !inserted {
inserted = true
_, err = w.WriteString(entry + "\n")
if err != nil {
return err
}
}
_, err = w.WriteString(line + "\n")
if err != nil {
return err
}
}
err = w.Flush()
if err != nil {
return err
}
if !inserted {
return fmt.Errorf("No place to insert repo")
}
return nil
}
// add pacman.conf from src.
func addPacmanConf(src string) error {
return exec.Command("sudo", "mv", src, "/etc/pacman.conf").Run()
}
// Add custom mirror to /etc/pacman.d/mirrorlist.
func addMirror(mirror, tmpFile string) error {
entry := fmt.Sprintf("Server = %s\n", mirror)
f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
_, err = w.WriteString(entry)
if err != nil {
return err
}
err = w.Flush()
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return exec.Command("sudo", "mv", tmpFile, "/etc/pacman.d/mirrorlist").Run()
}
func splitRepoDef(repo string) (string, string, error) {
split := strings.Split(repo, "=")
if len(split) != 2 || split[0] == "" || split[1] == "" {
return "", "", fmt.Errorf("invalid repo defination: '%s'", repo)
}
return split[0], split[1], nil
}
// run command from basedir and print output to stdout.
func runCmd(baseDir string, env []string, command string, args ...string) error {
cmd := exec.Command(command, args...)
if baseDir != "" {
cmd.Dir = baseDir
}
cmd.Env = env
// print command being run
fmt.Println("$", strings.Join(cmd.Args, " "))
tty, err := pty.Start(cmd)
if err != nil {
return err
}
scanner := bufio.NewScanner(tty)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for scanner.Scan() {
out := scanner.Text()
fmt.Printf("%s\n", fmtOutput(out))
}
}()
err = cmd.Wait()
if err != nil {
return err
}
wg.Wait()
return nil
}
// handles progress bar output (only shows the last status line)..
func fmtOutput(output string) string {
sections := strings.Split(output, "\r")
return sections[len(sections)-1]
}
// Clone git repository from url to dst
func gitClone(src, dst string) error {
err := runCmd("", nil, "git", "clone", "-q", src, dst)
if err != nil {
return err
}
return nil
}
var repoPattReg = regexp.MustCompile(`([a-z\d][a-z\d@._+-]*)=http(s)?://.+`)
var repoPatt = regexp.MustCompile(`http(s)?://.+/([a-z\d][a-z\d@._+-]*)/([a-z\d][a-z\d@._+-]*)`)
func parseRepo(uri, basePath string) (*Repo, error) {
matches := repoPattReg.FindStringSubmatch(uri)
if len(matches) > 0 {
return &Repo{
local: *repo.NewRepo(&model.Repo{
Name: matches[1],
}, basePath),
url: uri,
}, nil
}
matches = repoPatt.FindStringSubmatch(uri)
if len(matches) == 0 {
return nil, fmt.Errorf("invalid repo uri: %s", uri)
}
return &Repo{
local: *repo.NewRepo(&model.Repo{
Owner: matches[2],
Name: matches[3],
}, basePath),
url: uri,
}, nil
}