-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.go
85 lines (68 loc) · 2.18 KB
/
env.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
package main
import (
"encoding/base64"
"fmt"
"os"
"strconv"
"github.com/alyyousuf7/lunch-lambda/notification"
"github.com/pkg/errors"
)
// EnvCOGS returns a COGS instance with username and password set using
// environment variables
// Password can also be base64 encoded value in environment variable
func EnvCOGS() (COGS, error) {
username := os.Getenv("COGS_USERNAME")
if username == "" {
return COGS{}, fmt.Errorf("environment variable COGS_USERNAME is missing")
}
passwordb64 := os.Getenv("COGS_PASSWORD")
if passwordb64 == "" {
return COGS{}, fmt.Errorf("environment variable COGS_PASSWORD is missing")
}
password, err := base64.StdEncoding.DecodeString(passwordb64)
if err != nil {
// probably the input is not base64 at all, so let's use it directly
return COGS{username, passwordb64}, nil
}
// remove the last character if it is a linefeed
if password[len(password)-1] == '\n' {
password = password[:len(password)-1]
}
return COGS{username, string(password)}, nil
}
// EnvNotifiers returns relevant Notifier array using environment variable
func EnvNotifiers() ([]notification.Notifier, error) {
clientNum, err := strconv.Atoi(os.Getenv("CLIENT_NUM"))
if err != nil || clientNum < 1 {
return nil, fmt.Errorf("invalid environment variable CLIENT_NUM value")
}
notifiers := []notification.Notifier{}
for i := 1; i <= clientNum; i++ {
notifier, err := EnvNotifier(i)
if err != nil {
return nil, errors.Wrapf(err, "Client %d", i)
}
notifiers = append(notifiers, notifier)
}
return notifiers, nil
}
// EnvNotifier returns Notifier instance using environment variable
func EnvNotifier(index int) (notification.Notifier, error) {
envPrefix := fmt.Sprintf("CLIENT_%d", index)
switch os.Getenv(envPrefix) {
case "console":
return ¬ification.Console{}, nil
case "pushbullet":
accessToken := os.Getenv(fmt.Sprintf("%s_TOKEN", envPrefix))
if accessToken == "" {
return nil, fmt.Errorf("token not provided")
}
channelTag := os.Getenv(fmt.Sprintf("%s_CHANNEL", envPrefix))
return ¬ification.PushBullet{
AccessToken: accessToken,
ChannelTag: channelTag,
}, nil
default:
return nil, fmt.Errorf("invalid notification service")
}
}