-
Notifications
You must be signed in to change notification settings - Fork 1
/
flags.go
194 lines (171 loc) · 4.82 KB
/
flags.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2024 The Mothership Authors
// SPDX-License-Identifier: Apache-2.0
package base
import (
"log/slog"
"os"
"github.com/urfave/cli/v2"
"go.temporal.io/sdk/client"
)
type EnvVar string
const (
EnvVarGRPCPort EnvVar = "GRPC_PORT"
EnvVarGatewayPort EnvVar = "GATEWAY_PORT"
EnvVarDatabaseURI EnvVar = "DATABASE_URI"
EnvVarTemporalNamespace EnvVar = "TEMPORAL_NAMESPACE"
EnvVarTemporalAddress EnvVar = "TEMPORAL_ADDRESS"
EnvVarTemporalTaskQueue EnvVar = "TEMPORAL_TASK_QUEUE"
EnvVarStorageEndpoint EnvVar = "STORAGE_ENDPOINT"
EnvVarStorageConnectionString EnvVar = "STORAGE_CONNECTION_STRING"
EnvVarStorageRegion EnvVar = "STORAGE_REGION"
EnvVarStorageSecure EnvVar = "STORAGE_SECURE"
EnvVarStoragePathStyle EnvVar = "STORAGE_PATH_STYLE"
)
func WithDatabaseFlags(appName string) []cli.Flag {
if appName == "" {
appName = "root"
}
return []cli.Flag{
&cli.StringFlag{
Name: "database-uri",
Aliases: []string{"d"},
Usage: "database uri",
EnvVars: []string{string(EnvVarDatabaseURI)},
Value: "postgres://postgres:postgres@localhost:5432/" + appName + "?sslmode=disable",
},
}
}
func WithTemporalFlags(defaultNamespace string, defaultTaskQueue string) []cli.Flag {
if defaultNamespace == "" {
defaultNamespace = "default"
}
return []cli.Flag{
&cli.StringFlag{
Name: "temporal-namespace",
Aliases: []string{"n"},
Usage: "temporal namespace",
EnvVars: []string{string(EnvVarTemporalNamespace)},
Value: defaultNamespace,
},
&cli.StringFlag{
Name: "temporal-address",
Aliases: []string{"a"},
Usage: "temporal address",
EnvVars: []string{string(EnvVarTemporalAddress)},
Value: "localhost:7233",
},
&cli.StringFlag{
Name: "temporal-task-queue",
Aliases: []string{"q"},
Usage: "temporal task queue",
EnvVars: []string{string(EnvVarTemporalTaskQueue)},
Value: defaultTaskQueue,
},
}
}
func WithGrpcFlags(defaultPort int) []cli.Flag {
if defaultPort == 0 {
defaultPort = 8080
}
return []cli.Flag{
&cli.IntFlag{
Name: "grpc-port",
Usage: "gRPC port",
EnvVars: []string{string(EnvVarGRPCPort)},
Value: defaultPort,
},
}
}
func WithGatewayFlags(defaultPort int) []cli.Flag {
if defaultPort == 0 {
defaultPort = 8081
}
return []cli.Flag{
&cli.IntFlag{
Name: "gateway-port",
Usage: "gRPC gateway port",
EnvVars: []string{string(EnvVarGatewayPort)},
Value: defaultPort,
},
}
}
// WithStorageFlags adds the storage flags to the app.
func WithStorageFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "storage-endpoint",
Usage: "storage endpoint",
EnvVars: []string{string(EnvVarStorageEndpoint)},
Value: "",
},
&cli.StringFlag{
Name: "storage-connection-string",
Usage: "storage connection string",
EnvVars: []string{string(EnvVarStorageConnectionString)},
},
&cli.StringFlag{
Name: "storage-region",
Usage: "storage region",
EnvVars: []string{string(EnvVarStorageRegion)},
// RESF default region
Value: "us-east-2",
},
&cli.BoolFlag{
Name: "storage-secure",
Usage: "storage secure",
EnvVars: []string{string(EnvVarStorageSecure)},
Value: true,
},
&cli.BoolFlag{
Name: "storage-path-style",
Usage: "storage path style",
EnvVars: []string{string(EnvVarStoragePathStyle)},
Value: false,
},
}
}
func WithFlags(flags ...[]cli.Flag) []cli.Flag {
var result []cli.Flag
for _, f := range flags {
result = append(result, f...)
}
return result
}
// FlagsToGRPCServerOptions converts the cli flags to gRPC server options.
func FlagsToGRPCServerOptions(ctx *cli.Context) []GRPCServerOption {
return []GRPCServerOption{
WithGRPCPort(ctx.Int("grpc-port")),
WithGatewayPort(ctx.Int("gateway-port")),
}
}
// GetDBFromFlags gets the database from the cli flags.
func GetDBFromFlags(ctx *cli.Context) *DB {
// Create database.
db, err := NewDB(ctx.String("database-uri"))
if err != nil {
slog.Error("failed to create database", "error", err)
os.Exit(1)
}
return db
}
// GetTemporalClientFromFlags gets the temporal client from the cli flags.
func GetTemporalClientFromFlags(ctx *cli.Context, opts client.Options) (client.Client, error) {
return NewTemporalClient(
ctx.String("temporal-address"),
ctx.String("temporal-namespace"),
ctx.String("temporal-task-queue"),
opts,
)
}
// RareUseChangeDefault changes the default value of an arbitrary environment variable.
func RareUseChangeDefault(envVar string, newDefault string) {
// Check if the environment variable is set.
if _, ok := os.LookupEnv(envVar); ok {
return
}
// Change the default value.
if err := os.Setenv(envVar, newDefault); err != nil {
slog.Error("failed to set environment variable", "error", err, "envVar", envVar)
os.Exit(1)
}
}