-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (77 loc) · 2.93 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
package main
import (
"golang.org/x/net/context"
"cloud.google.com/go/pubsub"
"flag"
"log"
"github.com/continuouspipe/message-puller/puller"
"os"
"encoding/base64"
"fmt"
"io/ioutil"
"google.golang.org/api/option"
"time"
)
func main() {
googleCloudProjectId := flag.String("google-project-id", "", "The Google Cloud project ID")
googleServiceAccountFilePath := flag.String("service-account-file-path", "", "the path of the service account to use, if any")
googleServiceAccount := flag.String("service-account", "", "the base64 encoded service account")
subscriptionIdentifier := flag.String("subscription", "", "The subscription identifier")
scriptPath := flag.String("script-path", "", "The path of the script to execute")
flag.Parse()
// Get service account from base64-encoded string
if *googleServiceAccountFilePath == "" && *googleServiceAccount != "" {
filePath, err := ServiceAccountFilePathFromBase64String(*googleServiceAccount)
if err != nil {
panic(err)
}
googleServiceAccountFilePath = &filePath
}
// Get PubSub client
client, err := NewPubSubClient(*googleCloudProjectId, *googleServiceAccountFilePath)
if err != nil {
log.Fatal(err)
return
}
handler := puller.NewAcknowledgeIfNoErrorHandler(
puller.NewExecuteMessageHandler(
puller.NewCommandExecuter(
log.New(os.Stderr, "", log.Ldate | log.Ltime),
log.New(os.Stdout, "", log.Ldate | log.Ltime),
),
puller.NewCommandFactory(*scriptPath),
),
)
subscription := client.Subscription(*subscriptionIdentifier)
subscription.ReceiveSettings.MaxExtension = 60 * time.Minute
fmt.Println("Receiving messages...")
err = subscription.Receive(context.Background(), func(ctx context.Context, msg *pubsub.Message) {
handler.Handle(msg)
})
if (err != nil) {
log.Fatal(err)
}
}
func NewPubSubClient(googleCloudProjectId string, googleServiceAccountFilePath string) (*pubsub.Client, error) {
options := []option.ClientOption{}
if (googleServiceAccountFilePath != "") {
options = append(options, option.WithServiceAccountFile(googleServiceAccountFilePath))
}
ctx := context.Background()
return pubsub.NewClient(ctx, googleCloudProjectId, options...)
}
func ServiceAccountFilePathFromBase64String(base64EncodedServiceAccount string) (string, error) {
file, err := ioutil.TempFile(os.TempDir(), "prefix")
if err != nil {
return "", err
}
decoded, err := base64.StdEncoding.DecodeString(base64EncodedServiceAccount)
if err != nil {
return "", fmt.Errorf("Base64 encoded service account seems invalid: %s", err.Error())
}
_, err = file.Write(decoded)
if err != nil {
return "", fmt.Errorf("Can't write service accunt: %s", err.Error())
}
return file.Name(), nil
}