|
7 | 7 | "io/ioutil" |
8 | 8 | "os" |
9 | 9 | "reflect" |
10 | | - "strings" |
11 | 10 | "text/template" |
12 | 11 |
|
13 | 12 | "github.com/caarlos0/env/v6" |
@@ -56,18 +55,10 @@ func run() error { |
56 | 55 | return fmt.Errorf("failed to render template: %v", err) |
57 | 56 | } |
58 | 57 |
|
59 | | - githubOutput := fmt.Sprintf("%s=%s", "result", escape(output)) |
60 | | - if os.Getenv("GITHUB_OUTPUT") != "" { |
61 | | - githubOutput = fmt.Sprintln(os.Getenv("GITHUB_OUTPUT")) + githubOutput |
62 | | - } |
63 | | - |
64 | | - err = os.Setenv("GITHUB_OUTPUT", githubOutput) |
65 | | - if err != nil { |
66 | | - return fmt.Errorf("failed to set GITHUB_OUTPUT: %v", err) |
| 58 | + if err = writeOutput(output); err != nil { |
| 59 | + return err |
67 | 60 | } |
68 | 61 |
|
69 | | - fmt.Printf("::debug::%s", os.Getenv("GITHUB_OUTPUT")) |
70 | | - |
71 | 62 | if len(c.ResultPath) != 0 { |
72 | 63 | err := ioutil.WriteFile(c.ResultPath, []byte(output), 0644) |
73 | 64 | if err != nil { |
@@ -126,16 +117,36 @@ func renderTemplate(templateFilePath string, vars vars) (string, error) { |
126 | 117 | return result.String(), nil |
127 | 118 | } |
128 | 119 |
|
129 | | -func escape(str string) string { |
130 | | - /* |
131 | | - set-output truncates multiline strings. |
132 | | - % and \n and \r can be escaped, the runner will unescape in reverse: |
133 | | - https://github.community/t/set-output-truncates-multiline-strings/16852 |
134 | | - */ |
135 | | - |
136 | | - return strings.NewReplacer( |
137 | | - "%", "%25", |
138 | | - "\n", "%0A", |
139 | | - "\r", "%0D", |
140 | | - ).Replace(str) |
| 120 | +func writeOutput(output string) error { |
| 121 | + githubOutput := formatOutput("result", output) |
| 122 | + if githubOutput == "" { |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + path := os.Getenv("GITHUB_OUTPUT") |
| 127 | + |
| 128 | + f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("failed to open result file %q: %v", path, err) |
| 131 | + } |
| 132 | + defer f.Close() |
| 133 | + |
| 134 | + if _, err = f.WriteString(githubOutput); err != nil { |
| 135 | + return fmt.Errorf("failed to write result to file %q: %v", path, err) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func formatOutput(name, value string) string { |
| 142 | + if value == "" { |
| 143 | + return "" |
| 144 | + } |
| 145 | + |
| 146 | + // if value contains new line, use multiline format |
| 147 | + if bytes.ContainsRune([]byte(value), '\n') { |
| 148 | + return fmt.Sprintf("%s<<OUTPUT\n%s\nOUTPUT", name, value) |
| 149 | + } |
| 150 | + |
| 151 | + return fmt.Sprintf("%s=%s", name, value) |
141 | 152 | } |
0 commit comments