Refactor config.

change-name
Robert Jacob 5 years ago
parent 175bc9663c
commit b4c7d4d479
  1. 54
      cmd/spaceapi-server/main.go

@ -12,33 +12,39 @@ import (
)
type config struct {
Addr string
StatusFile string
WebPrefix string
XMPPJid string
XMPPPassword string
XMPPTarget string
XMPPHandle string
Addr string
StatusFile string
WebPrefix string
XMPP xmppConfig
}
type xmppConfig struct {
JID string
Password string
Room string
Nickname string
}
func readConfig() (config, error) {
c := config{
Addr: ":8080",
StatusFile: "./status.json",
WebPrefix: "",
XMPPJid: "",
XMPPPassword: "",
XMPPTarget: "",
XMPPHandle: "spacebot",
Addr: ":8080",
StatusFile: "./status.json",
WebPrefix: "",
XMPP: xmppConfig{
JID: "",
Password: "",
Room: "",
Nickname: "spacebot",
},
}
pflag.String("addr", c.Addr, "Network address to listen on.")
pflag.String("status-file", c.StatusFile, "Status file to modify.")
pflag.String("web-prefix", c.WebPrefix, "Prefix path for web endpoints.")
pflag.String("xmpp-jid", c.XMPPJid, "JID of XMPP user.")
pflag.String("xmpp-password", c.XMPPPassword, "Password of XMPP user.")
pflag.String("xmpp-target", c.XMPPTarget, "JID of target MUC to send XMPP messages to.")
pflag.String("xmpp-handle", c.XMPPHandle, "Nickname for MUC.")
pflag.String("xmpp-jid", c.XMPP.JID, "JID of XMPP user.")
pflag.String("xmpp-password", c.XMPP.Password, "Password of XMPP user.")
pflag.String("xmpp-target", c.XMPP.Room, "JID of target MUC to send XMPP messages to.")
pflag.String("xmpp-handle", c.XMPP.Nickname, "Nickname for MUC.")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
@ -53,10 +59,10 @@ func readConfig() (config, error) {
c.Addr = viper.GetString("addr")
c.StatusFile = viper.GetString("status-file")
c.WebPrefix = viper.GetString("web-prefix")
c.XMPPJid = viper.GetString("xmpp-jid")
c.XMPPPassword = viper.GetString("xmpp-password")
c.XMPPTarget = viper.GetString("xmpp-target")
c.XMPPHandle = viper.GetString("xmpp-handle")
c.XMPP.JID = viper.GetString("xmpp-jid")
c.XMPP.Password = viper.GetString("xmpp-password")
c.XMPP.Room = viper.GetString("xmpp-target")
c.XMPP.Nickname = viper.GetString("xmpp-handle")
return c, nil
}
@ -74,8 +80,8 @@ func main() {
log.Fatalf("Error opening data file: %s", err)
}
if c.XMPPJid != "" {
xmppListener, err := xmpp.Listener(storage, c.XMPPJid, c.XMPPPassword, c.XMPPTarget, c.XMPPHandle)
if c.XMPP.JID != "" {
xmppListener, err := xmpp.Listener(storage, c.XMPP.JID, c.XMPP.Password, c.XMPP.Room, c.XMPP.Nickname)
if err != nil {
log.Fatalf("Error creating XMPP listener: %s", err)
}

Loading…
Cancel
Save