Add support for auth in client config

This commit is contained in:
lrabane 2022-02-17 19:16:01 +01:00
parent f4f5edb230
commit b89c18e83d
4 changed files with 18 additions and 5 deletions

View file

@ -16,6 +16,10 @@
# command: 'echo "$message"' # command: 'echo "$message"'
# if: # if:
# priority: high,urgent # priority: high,urgent
# - topic: secret
# command: 'notify-send "$m"'
# user: phill
# password: mypass
# #
# Variables: # Variables:
# Variable Aliases Description # Variable Aliases Description

View file

@ -15,6 +15,8 @@ type Config struct {
DefaultHost string `yaml:"default-host"` DefaultHost string `yaml:"default-host"`
Subscribe []struct { Subscribe []struct {
Topic string `yaml:"topic"` Topic string `yaml:"topic"`
User string `yaml:"user"`
Password string `yaml:"password"`
Command string `yaml:"command"` Command string `yaml:"command"`
If map[string]string `yaml:"if"` If map[string]string `yaml:"if"`
} `yaml:"subscribe"` } `yaml:"subscribe"`

View file

@ -13,7 +13,9 @@ func TestConfig_Load(t *testing.T) {
require.Nil(t, os.WriteFile(filename, []byte(` require.Nil(t, os.WriteFile(filename, []byte(`
default-host: http://localhost default-host: http://localhost
subscribe: subscribe:
- topic: no-command - topic: no-command-with-auth
user: phil
password: mypass
- topic: echo-this - topic: echo-this
command: 'echo "Message received: $message"' command: 'echo "Message received: $message"'
- topic: alerts - topic: alerts
@ -26,8 +28,10 @@ subscribe:
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, "http://localhost", conf.DefaultHost) require.Equal(t, "http://localhost", conf.DefaultHost)
require.Equal(t, 3, len(conf.Subscribe)) require.Equal(t, 3, len(conf.Subscribe))
require.Equal(t, "no-command", conf.Subscribe[0].Topic) require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
require.Equal(t, "", conf.Subscribe[0].Command) require.Equal(t, "", conf.Subscribe[0].Command)
require.Equal(t, "phil", conf.Subscribe[0].User)
require.Equal(t, "mypass", conf.Subscribe[0].Password)
require.Equal(t, "echo-this", conf.Subscribe[1].Topic) require.Equal(t, "echo-this", conf.Subscribe[1].Topic)
require.Equal(t, `echo "Message received: $message"`, conf.Subscribe[1].Command) require.Equal(t, `echo "Message received: $message"`, conf.Subscribe[1].Command)
require.Equal(t, "alerts", conf.Subscribe[2].Topic) require.Equal(t, "alerts", conf.Subscribe[2].Topic)

View file

@ -162,6 +162,9 @@ func doSubscribe(c *cli.Context, cl *client.Client, conf *client.Config, topic,
for filter, value := range s.If { for filter, value := range s.If {
topicOptions = append(topicOptions, client.WithFilter(filter, value)) topicOptions = append(topicOptions, client.WithFilter(filter, value))
} }
if s.User != "" && s.Password != "" {
topicOptions = append(topicOptions, client.WithBasicAuth(s.User, s.Password))
}
subscriptionID := cl.Subscribe(s.Topic, topicOptions...) subscriptionID := cl.Subscribe(s.Topic, topicOptions...)
commands[subscriptionID] = s.Command commands[subscriptionID] = s.Command
} }