-
Notifications
You must be signed in to change notification settings - Fork 34
/
outputs.go
66 lines (53 loc) · 1.63 KB
/
outputs.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"strings"
"github.com/bitrise-io/go-utils/log"
)
// SendMessageResponse is the response from Slack POST
type SendMessageResponse struct {
/// The Thread Timestamp
Timestamp string `json:"ts"`
}
/// Export the output variables after a successful response
func exportOutputs(conf *config, resp *http.Response) error {
if !isRequestingOutput(conf) {
log.Debugf("Not requesting any outputs")
return nil
}
isWebhook := strings.TrimSpace(conf.WebhookURL) != ""
// Slack webhooks do not return any useful response information
if isWebhook {
return fmt.Errorf("For output support, do not submit a WebHook URL")
}
var response SendMessageResponse
parseError := json.NewDecoder(resp.Body).Decode(&response)
if parseError != nil {
// here we want to fail, because the user is expecting an output
return fmt.Errorf("Failed to parse response: %s", parseError)
}
if string(conf.ThreadTsOutputVariableName) != "" {
log.Debugf("Exporting output: %s=%s\n", string(conf.ThreadTsOutputVariableName), response.Timestamp)
err := exportEnvVariable(string(conf.ThreadTsOutputVariableName), response.Timestamp)
if err != nil {
return err
}
}
return nil
}
/// Checks if we are requesting an output of anything
func isRequestingOutput(conf *config) bool {
return string(conf.ThreadTsOutputVariableName) != ""
}
/// Exports env using envman
func exportEnvVariable(variable string, value string) error {
c := exec.Command("envman", "add", "--key", variable, "--value", value)
err := c.Run()
if err != nil {
return fmt.Errorf("Failed to run envman %s", err)
}
return nil
}