-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathcontainer_output.go
240 lines (199 loc) · 5.38 KB
/
container_output.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package utils
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-errors/errors"
)
func ProcessPullOutput(out io.ReadCloser, p Program) error {
dec := json.NewDecoder(out)
downloads := make(map[string]struct{ current, total int64 })
for {
var progress jsonmessage.JSONMessage
if err := dec.Decode(&progress); err == io.EOF {
break
} else if err != nil {
return err
}
if strings.HasPrefix(progress.Status, "Pulling from") {
p.Send(StatusMsg(progress.Status + "..."))
} else if progress.Status == "Pulling fs layer" || progress.Status == "Waiting" {
downloads[progress.ID] = struct{ current, total int64 }{
current: 0,
total: 0,
}
} else if progress.Status == "Downloading" {
downloads[progress.ID] = struct{ current, total int64 }{
current: progress.Progress.Current,
total: progress.Progress.Total,
}
var overallProgress float64
for _, percentage := range downloads {
if percentage.total > 0 {
progress := float64(percentage.current) / float64(percentage.total)
overallProgress += progress / float64(len(downloads))
}
}
p.Send(ProgressMsg(&overallProgress))
}
}
p.Send(ProgressMsg(nil))
return nil
}
type DiffStream struct {
o bytes.Buffer
r *io.PipeReader
w *io.PipeWriter
p Program
}
func NewDiffStream(p Program) *DiffStream {
r, w := io.Pipe()
go func() {
if err := ProcessDiffProgress(p, r); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()
return &DiffStream{r: r, w: w, p: p}
}
func (c DiffStream) Stdout() io.Writer {
return &c.o
}
func (c DiffStream) Stderr() io.Writer {
return c.w
}
func (c DiffStream) Collect() ([]byte, error) {
if err := c.w.Close(); err != nil {
fmt.Fprintln(os.Stderr, "Failed to close stream:", err)
}
return ProcessDiffOutput(c.o.Bytes())
}
func ProcessDiffProgress(p Program, out io.Reader) error {
scanner := bufio.NewScanner(out)
re := regexp.MustCompile(`(.*)([[:digit:]]{2,3})%`)
for scanner.Scan() {
line := scanner.Text()
if line == "Starting schema diff..." {
percentage := 0.0
p.Send(ProgressMsg(&percentage))
}
matches := re.FindStringSubmatch(line)
if len(matches) != 3 {
// TODO: emit actual error statements
continue
}
p.Send(StatusMsg(matches[1]))
percentage, err := strconv.ParseFloat(matches[2], 64)
if err != nil {
continue
}
percentage = percentage / 100
p.Send(ProgressMsg(&percentage))
}
p.Send(ProgressMsg(nil))
return scanner.Err()
}
type DiffDependencies struct {
Type string `json:"type"`
}
type DiffEntry struct {
Type string `json:"type"`
Status string `json:"status"`
DiffDdl string `json:"diff_ddl"`
GroupName string `json:"group_name"`
Dependencies []DiffDependencies `json:"dependencies"`
SourceSchemaName *string `json:"source_schema_name"`
}
const diffHeader = `-- This script was generated by the Schema Diff utility in pgAdmin 4
-- For the circular dependencies, the order in which Schema Diff writes the objects is not very sophisticated
-- and may require manual changes to the script to ensure changes are applied in the correct order.
-- Please report an issue for any failure with the reproduction steps.`
func ProcessDiffOutput(diffBytes []byte) ([]byte, error) {
// TODO: Remove when https://github.com/supabase/pgadmin4/issues/24 is fixed.
diffBytes = bytes.TrimPrefix(diffBytes, []byte("NOTE: Configuring authentication for DESKTOP mode.\n"))
if len(diffBytes) == 0 {
return diffBytes, nil
}
var diffJson []DiffEntry
if err := json.Unmarshal(diffBytes, &diffJson); err != nil {
return nil, err
}
var filteredDiffDdls []string
for _, diffEntry := range diffJson {
if diffEntry.Status == "Identical" || diffEntry.DiffDdl == "" {
continue
}
switch diffEntry.Type {
case "extension", "function", "mview", "table", "trigger_function", "type", "view":
// skip
default:
continue
}
{
doContinue := false
for _, dep := range diffEntry.Dependencies {
if dep.Type == "extension" {
doContinue = true
break
}
}
if doContinue {
continue
}
}
isSchemaIgnored := func(schema string) bool {
for _, s := range InternalSchemas {
if s == schema {
return true
}
}
return false
}
if isSchemaIgnored(diffEntry.GroupName) ||
// Needed at least for trigger_function
(diffEntry.SourceSchemaName != nil && isSchemaIgnored(*diffEntry.SourceSchemaName)) {
continue
}
trimmed := strings.TrimSpace(diffEntry.DiffDdl)
if len(trimmed) > 0 {
filteredDiffDdls = append(filteredDiffDdls, trimmed)
}
}
if len(filteredDiffDdls) == 0 {
return nil, nil
}
return []byte(diffHeader + "\n\n" + strings.Join(filteredDiffDdls, "\n\n") + "\n"), nil
}
func ProcessPsqlOutput(out io.Reader, p Program) error {
r, w := io.Pipe()
doneCh := make(chan struct{}, 1)
go func() {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
select {
case <-doneCh:
return
default:
}
line := scanner.Text()
p.Send(PsqlMsg(&line))
}
}()
var errBuf bytes.Buffer
if _, err := stdcopy.StdCopy(w, &errBuf, out); err != nil {
return err
}
if errBuf.Len() > 0 {
return errors.New("Error running SQL: " + errBuf.String())
}
doneCh <- struct{}{}
p.Send(PsqlMsg(nil))
return nil
}