-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanager_options.go
139 lines (104 loc) · 3.21 KB
/
manager_options.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
package gorabbit
import "github.com/Netflix/go-env"
// ManagerOptions holds all necessary properties to launch a successful connection with an MQTTManager.
type ManagerOptions struct {
// Host is the RabbitMQ server host name.
Host string
// Port is the RabbitMQ server port number.
Port uint
// Username is the RabbitMQ server allowed username.
Username string
// Password is the RabbitMQ server allowed password.
Password string
// Vhost is used for CloudAMQP connections to set the specific vhost.
Vhost string
// UseTLS defines whether we use amqp or amqps protocol.
UseTLS bool
// Mode will specify whether logs are enabled or not.
Mode string
// Marshaller defines the content type used for messages and how they're marshalled (default: JSON).
Marshaller Marshaller
}
// DefaultManagerOptions will return a ManagerOptions with default values.
func DefaultManagerOptions() *ManagerOptions {
return &ManagerOptions{
Host: defaultHost,
Port: defaultPort,
Username: defaultUsername,
Password: defaultPassword,
Vhost: defaultVhost,
UseTLS: defaultUseTLS,
Mode: defaultMode,
Marshaller: defaultMarshaller,
}
}
// NewManagerOptions is the exported builder for a ManagerOptions and will offer setter methods for an easy construction.
// Any non-assigned field will be set to default through DefaultManagerOptions.
func NewManagerOptions() *ManagerOptions {
return DefaultManagerOptions()
}
// NewManagerOptionsFromEnv will generate a ManagerOptions from environment variables. Empty values will be taken as default
// through the DefaultManagerOptions.
func NewManagerOptionsFromEnv() *ManagerOptions {
defaultOpts := DefaultManagerOptions()
fromEnv := new(RabbitMQEnvs)
_, err := env.UnmarshalFromEnviron(fromEnv)
if err != nil {
return defaultOpts
}
if fromEnv.Host != "" {
defaultOpts.Host = fromEnv.Host
}
if fromEnv.Port > 0 {
defaultOpts.Port = fromEnv.Port
}
if fromEnv.Username != "" {
defaultOpts.Username = fromEnv.Username
}
if fromEnv.Password != "" {
defaultOpts.Password = fromEnv.Password
}
if fromEnv.Vhost != "" {
defaultOpts.Vhost = fromEnv.Vhost
}
defaultOpts.UseTLS = fromEnv.UseTLS
return defaultOpts
}
// SetHost will assign the host.
func (m *ManagerOptions) SetHost(host string) *ManagerOptions {
m.Host = host
return m
}
// SetPort will assign the port.
func (m *ManagerOptions) SetPort(port uint) *ManagerOptions {
m.Port = port
return m
}
// SetCredentials will assign the username and password.
func (m *ManagerOptions) SetCredentials(username, password string) *ManagerOptions {
m.Username = username
m.Password = password
return m
}
// SetVhost will assign the Vhost.
func (m *ManagerOptions) SetVhost(vhost string) *ManagerOptions {
m.Vhost = vhost
return m
}
// SetUseTLS will assign the UseTLS status.
func (m *ManagerOptions) SetUseTLS(use bool) *ManagerOptions {
m.UseTLS = use
return m
}
// SetMode will assign the mode if valid.
func (m *ManagerOptions) SetMode(mode string) *ManagerOptions {
if isValidMode(mode) {
m.Mode = mode
}
return m
}
// SetMarshaller will assign the Marshaller.
func (m *ManagerOptions) SetMarshaller(marshaller Marshaller) *ManagerOptions {
m.Marshaller = marshaller
return m
}