-
Notifications
You must be signed in to change notification settings - Fork 118
/
add.go
123 lines (104 loc) · 4.1 KB
/
add.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
// Copyright 2019 Percona LLC
//
// Licensed 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 management
import (
"net"
"strconv"
)
// AddCommand is used by Kong for CLI flags and commands.
type AddCommand struct {
External AddExternalCommand `cmd:"" help:"Add External source of data (like a custom exporter running on a port) to the monitoring"`
ExternalServerless AddExternalServerlessCommand `cmd:"" help:"Add External Service on Remote node to monitoring"`
HAProxy AddHAProxyCommand `cmd:"" name:"haproxy" help:"Add HAProxy to monitoring"`
MongoDB AddMongoDBCommand `cmd:"" name:"mongodb" help:"Add MongoDB to monitoring"`
MySQL AddMySQLCommand `cmd:"" name:"mysql" help:"Add MySQL to monitoring"`
PostgreSQL AddPostgreSQLCommand `cmd:"" name:"postgresql" help:"Add PostgreSQL to monitoring"`
ProxySQL AddProxySQLCommand `cmd:"" name:"proxysql" help:"Add ProxySQL to monitoring"`
}
// AddCommonFlags is used by Kong for CLI flags and commands.
type AddCommonFlags struct {
AddServiceNameFlag string `name:"service-name" placeholder:"NAME" help:"Service name (overrides positional argument)"`
AddHostFlag string `name:"host" placeholder:"HOST" help:"Service hostname or IP address (overrides positional argument)"`
AddPortFlag uint16 `name:"port" placeholder:"PORT" help:"Service port number (overrides positional argument)"`
}
// AddLogLevelFatalFlags contains log level flag with "fatal" option.
type AddLogLevelFatalFlags struct {
AddLogLevel string `name:"log-level" enum:"debug,info,warn,error,fatal" default:"warn" help:"Service logging level. One of: [debug, info, warn, error, fatal]"`
}
// AddLogLevelNoFatalFlags contains log level flag without "fatal" option.
type AddLogLevelNoFatalFlags struct {
AddLogLevel string `name:"log-level" enum:"debug,info,warn,error" default:"warn" help:"Service logging level. One of: [debug, info, warn, error]"`
}
type connectionGetter interface {
GetServiceName() string
GetAddress() string
GetDefaultAddress() string
GetSocket() string
}
// Types implementing the getter interface:
// - addMySQLCommand
// - addProxySQLCommand
// - addPostgreSQLCommand
// - addMongoDBCommand
// Returns service name, socket, host, port, error.
func processGlobalAddFlagsWithSocket(cmd connectionGetter, opts AddCommonFlags) (serviceName string, socket string, host string, port uint16, err error) {
serviceName = cmd.GetServiceName()
if opts.AddServiceNameFlag != "" {
serviceName = opts.AddServiceNameFlag
}
socket = cmd.GetSocket()
address := cmd.GetAddress()
if socket == "" && address == "" {
address = cmd.GetDefaultAddress()
}
var portI int
if address != "" {
var portS string
host, portS, err = net.SplitHostPort(address)
if err != nil {
return "", "", "", 0, err
}
portI, err = strconv.Atoi(portS)
if err != nil {
return "", "", "", 0, err
}
}
if opts.AddHostFlag != "" {
host = opts.AddHostFlag
}
if opts.AddPortFlag != 0 {
portI = int(opts.AddPortFlag)
}
return serviceName, socket, host, uint16(portI), nil
}
// Determine if parameters were passed.
var (
usernameParameterSpecified = false
passwordParameterSpecified = false
)
// Types for username and password
type (
username string
password string
)
// AfterApply Change usernameParameterSpecified flag if argument was passed
func (u username) AfterApply() error {
usernameParameterSpecified = true
return nil
}
// AfterApply Change passwordParameterSpecified flag if argument was passed
func (p password) AfterApply() error {
passwordParameterSpecified = true
return nil
}