-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
117 lines (109 loc) · 2.52 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
package lib
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/savaki/aiven/kafka"
"gopkg.in/urfave/cli.v1"
)
var opts = struct {
Email string
Password string
OTP string
Project string
Service string
Topic struct {
Name string
CleanupPolicy string
Partitions int
Replication int
RetentionHours int
}
}{}
var (
flagEmail = cli.StringFlag{
Name: "email",
Usage: "aiven email",
EnvVar: "AIVEN_EMAIL",
Destination: &opts.Email,
}
flagPassword = cli.StringFlag{
Name: "password",
Usage: "aiven password",
EnvVar: "AIVEN_PASSWORD",
Destination: &opts.Password,
}
flagOTP = cli.StringFlag{
Name: "otp",
Usage: "aiven one time password (otp)",
EnvVar: "AIVEN_OTP",
Destination: &opts.OTP,
}
flagProject = cli.StringFlag{
Name: "project",
Usage: "aiven project",
EnvVar: "AIVEN_PROJECT",
Destination: &opts.Project,
}
flagService = cli.StringFlag{
Name: "service",
Usage: "aiven service",
EnvVar: "AIVEN_SERVICE",
Destination: &opts.Service,
}
// kafka specific
//
flagName = cli.StringFlag{
Name: "name",
Usage: "name of topic",
EnvVar: "TOPIC_NAME",
Destination: &opts.Topic.Name,
}
flagCleanupPolicy = cli.StringFlag{
Name: "cleanup-policy",
Value: kafka.CleanupPolicyDelete,
Usage: "cleanup policy",
EnvVar: "TOPIC_CLEANUP_POLICY",
Destination: &opts.Topic.CleanupPolicy,
}
flagPartitions = cli.IntFlag{
Name: "partitions",
Value: 1,
Usage: "partitions",
EnvVar: "TOPIC_PARTITIONS",
Destination: &opts.Topic.Partitions,
}
flagReplication = cli.IntFlag{
Name: "replication",
Value: 3,
Usage: "replication factor",
EnvVar: "TOPIC_REPLICATION",
Destination: &opts.Topic.Replication,
}
flagRetentionHours = cli.IntFlag{
Name: "retention-hours",
Value: 36,
Usage: "hours to retain content",
EnvVar: "TOPIC_REPLICATION_HOURS",
Destination: &opts.Topic.RetentionHours,
}
)
func Do(fn func(ctx context.Context) (interface{}, error)) cli.ActionFunc {
return func(*cli.Context) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
out, err := fn(ctx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if out != nil {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
encoder.Encode(out)
}
return nil
}
}