/
config.go
102 lines (82 loc) · 2.84 KB
/
config.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package cmdutils
import (
"log"
"github.com/streamnative/pulsarctl/pkg/pulsar"
"github.com/spf13/pflag"
)
var PulsarCtlConfig = ClusterConfig{}
// the configuration of the cluster that pulsarctl connects to
type ClusterConfig struct {
// the web service url that pulsarctl connects to. Default is http://localhost:8080
WebServiceURL string
// Set the path to the trusted TLS certificate file
TLSTrustCertsFilePath string
// Configure whether the Pulsar client accept untrusted TLS certificate from broker (default: false)
TLSAllowInsecureConnection bool
AuthParams string
}
func (c *ClusterConfig) FlagSet() *pflag.FlagSet {
flags := pflag.NewFlagSet(
"PulsarCtl Config",
pflag.ContinueOnError)
flags.StringVarP(
&c.WebServiceURL,
"admin-service-url",
"s",
pulsar.DefaultWebServiceURL,
"The admin web service url that pulsarctl connects to.")
flags.StringVar(
&c.AuthParams,
"auth-params",
"",
"Authentication parameters are used to configure the public and private key files required by tls\n"+
" For example: \"tlsCertFile:val1,tlsKeyFile:val2\"")
flags.BoolVar(
&c.TLSAllowInsecureConnection,
"tls-allow-insecure",
false,
"Allow TLS insecure connection")
flags.StringVar(
&c.TLSTrustCertsFilePath,
"tls-trust-cert-pat",
"",
"Allow TLS trust cert file path")
return flags
}
func (c *ClusterConfig) Client(version pulsar.APIVersion) pulsar.Client {
config := pulsar.DefaultConfig()
if len(c.WebServiceURL) > 0 && c.WebServiceURL != config.WebServiceURL {
config.WebServiceURL = c.WebServiceURL
}
if len(c.TLSTrustCertsFilePath) > 0 && c.TLSTrustCertsFilePath != config.TLSOptions.TrustCertsFilePath {
config.TLSOptions.TrustCertsFilePath = c.TLSTrustCertsFilePath
}
if c.TLSAllowInsecureConnection {
config.TLSOptions.AllowInsecureConnection = true
}
if len(c.AuthParams) > 0 && c.AuthParams != config.AuthParams {
config.AuthParams = c.AuthParams
}
config.APIVersion = version
client, err := pulsar.New(config)
if err != nil {
log.Fatalf("create pulsar client error: %s", err.Error())
}
return client
}