-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
153 lines (129 loc) · 3.14 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"os/exec"
"github.com/pkg/errors"
"github.com/slack-go/slack"
)
// Config contains parsed user configuration
type Config struct {
Channel string
AttachmentsFile string
TimestampFile string
Timestamp string
Fields []slack.AttachmentField
Client *slack.Client
}
// GetConfig returns validated config params
func GetConfig(args []string) (*Config, error) {
conf := new(Config)
// arguments
sep := os.Getenv("SEPARATOR")
if sep == "" {
sep = "=="
}
fields := make([]slack.AttachmentField, 0)
for _, arg := range args {
if len(strings.Split(arg, "\n")) > 0 {
for _, f := range strings.Split(arg, "\n") {
if len(strings.Split(f, sep)) == 2 {
field := slack.AttachmentField{
Title: strings.TrimSpace(strings.Split(f, sep)[0]),
Value: strings.TrimSpace(strings.Split(f, sep)[1]),
Short: true,
}
fields = append(fields, field)
}
}
}
}
// env.vars
timestampFile := os.Getenv("TIMESTAMP_FILE")
var timestamp string
if timestampFile != "" {
err := os.MkdirAll(filepath.Dir(timestampFile), os.ModePerm)
if err != nil {
return conf, errors.New("error creating timestamp file")
}
t, err := os.ReadFile(os.Getenv("TIMESTAMP_FILE"))
if err != nil && strings.Contains(err.Error(), "no such file or directory") {
timestamp = ""
} else if err != nil {
return conf, errors.New("error reading timestamp file")
} else {
timestamp = string(t)
}
} else {
timestamp = os.Getenv("TIMESTAMP")
}
channel := os.Getenv("CHANNEL")
if channel == "" {
return conf, errors.New("missing Slack channel")
}
attachmentsFile := os.Getenv("ATTACHMENTS_FILE")
t := os.Getenv("TOKEN")
if t == "" {
return conf, errors.New("missing Slack token")
}
conf.Channel = channel
conf.AttachmentsFile = attachmentsFile
conf.TimestampFile = timestampFile
conf.Timestamp = timestamp
conf.Fields = fields
conf.Client = slack.New(t)
return conf, nil
}
func main() {
vars := []string{
"GITHUB_ACTOR",
"GITHUB_REPOSITORY",
"STATUS",
"GITHUB_WORKFLOW",
}
for _, v := range vars {
if os.Getenv(v) == "" {
fmt.Printf("missing required env.var: '%s'", v)
os.Exit(1)
}
}
conf, err := GetConfig(os.Args[1:])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s := Slack{
Channel: conf.Channel,
Context: ctx,
Timestamp: conf.Timestamp,
}
var ts string
if conf.AttachmentsFile == "" {
ts, err = s.SendTemplate(conf.Client, conf.Fields)
} else {
ts, err = s.SendAttachmentFromFile(conf.Client, conf.AttachmentsFile, conf.Fields)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if conf.TimestampFile != "" {
err = os.WriteFile(conf.TimestampFile, []byte(ts), 0644)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
cmd := exec.Command("/bin/sh","-c",fmt.Sprintf("echo \"TIMESTAMP=%s\" >> $GITHUB_OUTPUT",ts))
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Printf("error executing shell command: %v", err.Error())
os.Exit(1)
}
}