Skip to content

Commit 73d6347

Browse files
committed
Load message from file or url
Adds the capability to have the message be a local or remote file. Allows more dynamic messaging that can be generated by a previous step or a centralized message.
1 parent 3ae6609 commit 73d6347

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

plugin.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ func (p Plugin) Exec() error {
140140
}
141141

142142
func templateMessage(t string, plugin Plugin) (string, error) {
143-
return template.RenderTrim(t, plugin)
143+
c, err := contents(t)
144+
if err != nil {
145+
return "", fmt.Errorf("could not read template: %w", err)
146+
}
147+
148+
return template.RenderTrim(c, plugin)
144149
}
145150

146151
func message(repo Repo, build Build) string {

util.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
)
10+
11+
func contents(str string) (string, error) {
12+
// Check for the empty string
13+
if str == "" {
14+
return str, nil
15+
}
16+
17+
isFilePath := false
18+
19+
// See if the string is referencing a URL
20+
if u, err := url.Parse(str); err == nil {
21+
switch u.Scheme {
22+
case "http", "https":
23+
res, err := http.Get(str)
24+
if err != nil {
25+
return "", err
26+
}
27+
28+
defer res.Body.Close()
29+
b, err := io.ReadAll(res.Body)
30+
if err != nil {
31+
return "", fmt.Errorf("could not read response: %w", err)
32+
}
33+
34+
return string(b), nil
35+
36+
case "file":
37+
// Fall through to file loading
38+
str = u.Path
39+
isFilePath = true
40+
}
41+
}
42+
43+
// See if the string is referencing a file
44+
_, err := os.Stat(str)
45+
if err == nil {
46+
b, err := os.ReadFile(str)
47+
if err != nil {
48+
return "", fmt.Errorf("could not load file %s: %w", str, err)
49+
}
50+
51+
return string(b), nil
52+
}
53+
54+
if isFilePath {
55+
return "", fmt.Errorf("could not load file %s: %w", str, err)
56+
}
57+
58+
// Its a regular string
59+
return str, nil
60+
}

0 commit comments

Comments
 (0)