-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
206 lines (174 loc) · 3.67 KB
/
client.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
202
203
204
205
206
package gosvn
import (
"encoding/xml"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
)
type client struct {
username string
password string
svnUrl string
svnDir string
Env []string
}
// NewClient
func NewClient(username, password, url, workDir string) *client {
path, err := filepath.Abs(workDir)
if err != nil {
panic(err)
}
fmt.Println(path)
return &client{username: username, password: password, svnUrl: url, svnDir: path}
}
// Cleanup ...
func (this *client) Cleanup() error {
_, err := this.run("cleanup")
if err != nil {
return err
}
return nil
}
// Update ...
func (this *client) Update() (string, error) {
out, err := this.run("update")
if err != nil {
return "", err
}
return string(out), err
}
// Commit ...
func (this *client) Commit(msg string) (string, error) {
out, err := this.run("commit", "-m", msg)
if err != nil {
return "", err
}
return string(out), nil
}
// Add ...
func (this *client) Add(file string) (string, error) {
out, err := this.run("add", file)
if err != nil {
return "", err
}
return string(out), nil
}
// Status ...
func (this *client) Status() (*status, error) {
out, err := this.run("status", "--xml")
if err != nil {
return nil, err
}
s := new(status)
err = xml.Unmarshal(out, s)
if err != nil {
return nil, err
}
return s, nil
}
// Cat ...
func (this *client) Cat(file string) (string, error) {
out, err := this.run("cat", this.svnUrl+"/"+file)
if err != nil {
return "", err
}
return string(out), nil
}
// Export ...
func (this *client) Export(dir string) error {
_, err := this.run("export", this.svnUrl, dir)
if err != nil {
return err
}
return nil
}
// Log ...
func (this *client) Log() (*SvnLog, error) {
out, err := this.run("log", this.svnUrl, "--xml", "-v")
if err != nil {
return nil, err
}
l := new(SvnLog)
err = xml.Unmarshal(out, l)
if err != nil {
return nil, err
}
return l, nil
}
// list ...
func (this *client) List() (*lists, error) {
cmd := []string{"list", this.svnUrl, "--xml"}
out, err := this.run(cmd...)
if err != nil {
return nil, err
}
l := new(lists)
err = xml.Unmarshal(out, l)
if err != nil {
return nil, err
}
return l, nil
}
// Checkout
func (this *client) Checkout() (string, error) {
cmd := []string{"checkout", this.svnUrl, this.svnDir}
out, err := this.run(cmd...)
if err != nil {
return "", err
}
return string(out), nil
}
// client Checkout from specific revision
func (this *client) CheckoutWithRevision(revision string) (string, error) {
cmd := []string{"checkout", this.svnUrl, this.svnDir}
cmd = append(cmd, "-r", revision)
out, err := this.run(cmd...)
if err != nil {
return "", err
}
return string(out), nil
}
// info ...
func (this *client) Info() (*Info, error) {
cmd := []string{"info", this.svnUrl, "--xml"}
out, err := this.run(cmd...)
if err != nil {
return nil, err
}
info := new(Info)
err = xml.Unmarshal(out, info)
if err != nil {
return nil, err
}
return info, nil
}
// run 运行命令
func (this *client) run(args ...string) ([]byte, error) {
ops := []string{"--non-interactive", "--trust-server-cert"}
if this.username != "" {
args = append(args, "--username", this.username)
}
if this.password != "" {
args = append(args, "--password", this.password)
}
args = append(args, ops...)
log.Println("cmd args:", args)
cmd := exec.Command("svn", args...)
if len(this.Env) > 0 {
cmd.Env = append(os.Environ(), this.Env...)
}
if this.svnDir != "" {
if _, err := os.Stat(this.svnDir); err == nil {
cmd.Dir = this.svnDir
log.Printf("cmd dir %s \n", cmd.Dir)
}
}
out, err := cmd.Output()
if err != nil {
log.Println("cmd err:", string(out), err)
return nil, err
}
return out, nil
}