-
Notifications
You must be signed in to change notification settings - Fork 7
/
pipedream.go
62 lines (53 loc) · 1.4 KB
/
pipedream.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
package pd
import (
"encoding/json"
"os"
)
var (
// Steps provides access to previous step data
// Unmarshal should be preferred with an explicit type
Steps map[string]interface{}
// StepsEnv is the environment variable name for previous pipedream steps
StepsEnv = "PIPEDREAM_STEPS"
// ExportsEnv is the environment variable name for exporting data to a file
ExportsEnv = "PIPEDREAM_EXPORTS"
)
// MustUnmarshal unmarshals the previous step data, panicking on error
func MustUnmarshal(in interface{}) {
if err := Unmarshal(in); err != nil {
panic(err)
}
}
// Unmarshal unmarshals the previous step data, returning any errors encountered
func Unmarshal(in interface{}) error {
pdSteps := getFromFile(StepsEnv, []byte("null"))
if err := json.Unmarshal(pdSteps, in); err != nil {
return err
}
return nil
}
// Export exports data for subsequent steps to use
func Export(name string, value interface{}) {
export, err := json.Marshal(value)
if err != nil {
panic(err)
}
f, err := os.OpenFile(os.Getenv(ExportsEnv), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.WriteString(name + ":json=" + string(export) + "\n"); err != nil {
panic(err)
}
}
func getFromFile(key string, fallback []byte) []byte {
value, _ := os.ReadFile(os.Getenv(key))
if len(value) == 0 {
return fallback
}
return value
}
func init() {
MustUnmarshal(&Steps)
}