Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit c70391f

Browse files
author
Christian Häusler
authored
Make config file optional (#11)
1 parent 56d3909 commit c70391f

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Got executed
2+
ZW52QW1xcFVybE5vQ29uZmln
3+
envAmqpUrlNoConfig

fixtures/TestEndToEnd/envAmqpUrlNoConfigError.golden

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Connecting RabbitMQ...
2+
Connected.
3+
Opening channel...
4+
Done.
5+
Setting QoS...
6+
Succeeded setting QoS.
7+
Declaring queue "test"...
8+
Registering consumer...
9+
Succeeded registering consumer.
10+
Waiting for messages...
11+
Processing message...
12+
Processed!

integration_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ var tests = []struct {
9696
amqp.Publishing{ContentType: "text/plain", Body: []byte("envAmqpUrl")},
9797
[]string{"AMQP_URL=amqp://guest:guest@localhost"},
9898
},
99+
{
100+
"envAmqpUrlNoConfig",
101+
[]string{"-V", "-no-datetime", "-e", command, "-q", "test"},
102+
"test",
103+
amqp.Publishing{ContentType: "text/plain", Body: []byte("envAmqpUrlNoConfig")},
104+
[]string{"AMQP_URL=amqp://guest:guest@localhost"},
105+
},
99106
{
100107
"pipe",
101108
[]string{"-V", "-no-datetime", "-pipe", "-e", command + "-pipe", "-c", "fixtures/default.conf"},

main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,24 +194,35 @@ func loggerFlags(noDateTime bool) int {
194194

195195
// LoadConfiguration checks the configuration flags, loads the config from file and updates the config according the flags.
196196
func LoadConfiguration(c *cli.Context) (*config.Config, error) {
197-
if c.String("configuration") == "" && c.String("executable") == "" {
197+
file := c.String("configuration")
198+
url := c.String("url")
199+
queue := c.String("queue-name")
200+
201+
if file == "" && url == "" && queue == "" && c.String("executable") == "" {
198202
cli.ShowAppHelp(c)
199203
return nil, cli.NewExitError("", 1)
200204
}
201205

202-
cfg, err := config.LoadAndParse(c.String("configuration"))
206+
cfg, err := configuration(file)
203207
if err != nil {
204208
return nil, fmt.Errorf("failed parsing configuration: %s", err)
205209
}
206210

207-
url := c.String("url")
208211
if len(url) > 0 {
209212
cfg.RabbitMq.AmqpUrl = url
210213
}
211214

212-
if c.String("queue-name") != "" {
213-
cfg.RabbitMq.Queue = c.String("queue-name")
215+
if queue != "" {
216+
cfg.RabbitMq.Queue = queue
214217
}
215218

216219
return cfg, nil
217220
}
221+
222+
func configuration(file string) (*config.Config, error) {
223+
if file == "" {
224+
return config.CreateFromString("")
225+
}
226+
227+
return config.LoadAndParse(file)
228+
}

0 commit comments

Comments
 (0)