-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchange.go
219 lines (200 loc) · 4.74 KB
/
change.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
207
208
209
210
211
212
213
214
215
216
217
218
219
package p4
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"strconv"
"strings"
"text/template"
)
type Change struct {
Desc string
User string
Status string
Change uint64
Time int
Path string
Code string
ChangeType string
Client string
}
func (c *Change) String() string {
l := len(c.Desc)
if l > 250 {
l = 250
}
return fmt.Sprintf("change %d by %s - %s", c.Change, c.User, strings.Trim(c.Desc[:l], " "))
}
// Changes path格式: //Stream_Root/...
func (conn *Conn) Changes(paths []string) ([]Result, error) {
return conn.RunMarshaled("changes", append([]string{"-l"}, paths...))
}
// Shelved path格式: //Stream_Root/...
func (conn *Conn) Shelved(path string) (shelved []*Change, err error) {
var (
result []Result
)
if result, err = conn.RunMarshaled("changes", append([]string{"-s", "shelved"}, path)); err != nil {
return
}
for idx := range result {
if r, ok := result[idx].(*Change); !ok {
continue
} else {
shelved = append(shelved, r)
}
}
return
}
type ChangeList struct {
Change uint64
Date string
Client string
User string
Status string
Type string
Description string
ImportedBy string
Identity string
Jobs []string
Stream string
Files []string
}
type NewChangeList struct {
Change string // 固定传"new"
Date string
Client string
User string
Status string
Type string
Description string
ImportedBy string
Identity string
Jobs []string
Stream string
Files []string
}
func (cl *ChangeList) String() string {
var (
contentBuf = bytes.NewBuffer(nil)
)
if _, err := _changeTemplate.Parse(_changeTemplateTxt); err != nil {
return ""
}
if err := _changeTemplate.Execute(contentBuf, cl); err != nil {
return ""
}
return contentBuf.String()
}
var (
_changeTemplate = template.New("change template")
)
var _changeTemplateTxt = `Change: {{.Change}}
Date: {{.Date}}
Client: {{.Client}}
User: {{.User}}
Status: {{.Status}}
Type: {{.Type}}
Description: {{.Description}}
ImportedBy: {{.ImportedBy}}
Identity: {{.Identity}}
Jobs: {{- range .Jobs }}
{{.}}
{{- end }}
Stream: {{.Stream}}
Files: {{- range .Files }}
{{.}}
{{- end }}
`
func (conn *Conn) ChangeList(change uint64) (cl *ChangeList, err error) {
var (
result []Result
)
if result, err = conn.RunMarshaled("change", append([]string{"-o", strconv.FormatUint(change, 10)})); err != nil {
return
}
if len(result) == 0 {
return
}
if cl, _ = result[0].(*ChangeList); cl == nil {
err = errors.New("Type not match")
return
}
return
}
func (conn *Conn) UpdateChangeList(cl ChangeList) (message string, err error) {
var (
out []byte
contentBuf = bytes.NewBuffer(nil)
)
if _, err = _changeTemplate.Parse(_changeTemplateTxt); err != nil {
return
}
if err = _changeTemplate.Execute(contentBuf, cl); err != nil {
return
}
if out, err = conn.Input([]string{"change", "-f", "-i"}, contentBuf.Bytes()); err != nil {
return
}
message = strings.TrimSpace(string(out))
return
}
func (conn *Conn) ChangeListStream(change uint64) (stream string, err error) {
var (
changeList *ChangeList
client *Client
)
// 通过CL号,拿到Client(workspace)
if changeList, err = conn.ChangeList(change); err != nil {
return
}
if changeList == nil {
err = errors.Errorf("change '%d' can NOT found", change)
return
}
// 查看Client(workspace)里面配置的stream
if client, err = conn.Client(changeList.Client); err != nil {
return
}
if client == nil {
err = errors.Errorf("client '%s' for change '%d' can NOT found", changeList.Client, change)
return
}
stream = client.Stream
return
}
func (conn *Conn) NewChangeList(cl NewChangeList) (change uint64, err error) {
var (
out []byte
contentBuf = bytes.NewBuffer(nil)
)
if _, err = _changeTemplate.Parse(_changeTemplateTxt); err != nil {
return
}
if err = _changeTemplate.Execute(contentBuf, cl); err != nil {
return
}
if out, err = conn.Input([]string{"change", "-i"}, contentBuf.Bytes()); err != nil {
return
}
message := strings.TrimSpace(string(out))
if strings.HasPrefix(message, "Change") {
_, err = fmt.Sscanf(message, "Change %d created.", &change)
if err != nil {
return 0, fmt.Errorf("failed to parse change list number: %v", err)
}
}
return
}
func (conn *Conn) DeleteChange(cl uint64) (message string, err error) {
var out []byte
out, err = conn.Output([]string{"change", "-d", strconv.FormatUint(cl, 10)})
message = strings.TrimSpace(string(out))
return
}
func (conn *Conn) ForceDeleteChange(cl uint64) (message string, err error) {
var out []byte
out, err = conn.Output([]string{"change", "-d", "-f", strconv.FormatUint(cl, 10)})
message = strings.TrimSpace(string(out))
return
}