-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
95 lines (79 loc) · 2.34 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
package main
import (
"cloud.google.com/go/pubsub"
"context"
"fmt"
flags "github.com/jessevdk/go-flags"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
gcp "pubsubroller/adapters/google"
config "pubsubroller/config"
)
type appOptions struct {
IsDryRun bool
Variables map[string]string
}
var opts struct {
ProjectID string `short:"p" long:"projectId" description:"target GCP project ID"`
ConfigFilePath string `short:"c" long:"config" description:"configuration file path" required:"true"`
Endpoint string `short:"e" long:"endpoint" description:"service endpoint"`
DryRun bool `long:"dry" description:"dry run"`
Delete bool `long:"delete" description:"delete all topics and their subscriptions"`
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
return
}
configuration, err := config.Load(opts.ConfigFilePath)
if err != nil {
fmt.Printf("Error: %s", err.Error())
return
}
ctx := context.Background()
projectID := opts.ProjectID
if projectID == "" {
credentials, err := google.FindDefaultCredentials(ctx)
if err != nil {
panic(err)
}
if credentials == nil {
fmt.Println("Error: invalid credential")
return
}
projectID = credentials.ProjectID
if projectID == "" {
fmt.Println("Error: Failed retrieving project ID from gcloud credentials.")
fmt.Println("Could you manually provide your project ID with --projectID option?")
return
}
}
var internalClient *pubsub.Client
var cerr error
if opts.Endpoint != "" {
internalClient, cerr = pubsub.NewClient(ctx, projectID, option.WithEndpoint(opts.Endpoint))
} else {
internalClient, cerr = pubsub.NewClient(ctx, projectID)
}
if cerr != nil {
fmt.Println("Error on initializing pubsub client:", err.Error())
return
}
client := gcp.PubsubClient{
Client: internalClient,
Ctx: ctx,
}
fmt.Printf("Target project ID: %s\n\n", projectID)
appOpts := appOptions{
IsDryRun: opts.DryRun,
Variables: configuration.Variables(projectID),
}
if opts.Delete {
deleteTopics(client, deleteTopicsLogger{}, ctx, configuration, appOpts)
deleteSubscriptions(client, deleteSubscriptionLogger{}, ctx, configuration, appOpts)
} else {
createTopics(client, createTopicsLogger{}, ctx, configuration, appOpts)
createSubscriptions(client, createSubscriptionsLogger{}, ctx, configuration, appOpts)
}
return
}