-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.go
95 lines (81 loc) · 2.35 KB
/
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Harry Randazzo
package vai
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
// CommandOutputs is a map of step IDs to their outputs.
type CommandOutputs map[string]map[string]any
// ParseOutput parses the output file of a step
//
// Matches behavior of GitHub Actions.
//
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
func ParseOutput(r io.ReadSeeker) (map[string]string, error) {
if f, ok := r.(*os.File); ok {
fi, err := f.Stat()
if err != nil {
return nil, err
}
// error if larger than 50MB, same limits as GitHub Actions
if fi.Size() > 50*1024*1024 {
return nil, fmt.Errorf("output file too large")
}
}
if _, err := r.Seek(0, io.SeekStart); err != nil {
return nil, err
}
scanner := bufio.NewScanner(r)
result := make(map[string]string)
var currentKey, currentDelimiter string
var multiLineValue []string
var collecting bool
for scanner.Scan() {
line := scanner.Text()
if collecting {
if line == currentDelimiter {
// End of multiline value
value := strings.Join(multiLineValue, "\n")
result[currentKey] = value
collecting = false
multiLineValue = []string{}
currentKey = ""
currentDelimiter = ""
} else {
multiLineValue = append(multiLineValue, line)
}
continue
}
if idx := strings.Index(line, "="); idx != -1 {
// Split the line at the first '=' to handle the key-value pair
key := line[:idx]
value := line[idx+1:]
result[key] = value
} else if idx := strings.Index(line, "<<"); idx != -1 {
// Split the line at the first '<<' to handle the key-value pair
key := line[:idx]
delimiter := strings.TrimSpace(line[idx+2:])
if delimiter == "" {
return nil, fmt.Errorf("invalid syntax: missing delimiter after '<<'")
}
currentKey = key
currentDelimiter = delimiter
collecting = true
} else if strings.TrimSpace(line) != "" {
// Non-empty line without '=' while not collecting a multiline value
return nil, fmt.Errorf("invalid syntax: non-delimited multiline value")
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
// Handle case where file ends but multiline was being collected
if collecting {
return nil, fmt.Errorf("invalid syntax: multiline value not terminated")
}
return result, nil
}