musala/cmd/musala-push/config.go

110 lines
4 KiB
Go
Raw Normal View History

2018-08-21 08:36:54 +00:00
package main
import (
// "errors"
2018-08-21 08:36:54 +00:00
"fmt"
2019-08-23 10:23:33 +00:00
// "github.com/davecgh/go-spew/spew"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2018-08-21 08:36:54 +00:00
"os"
2018-11-23 11:41:54 +00:00
"strings"
2018-08-21 08:36:54 +00:00
)
const (
2020-01-25 17:55:53 +00:00
programBinary string = "musala-push"
)
var (
ALLOWED_AUTH_TYPES = []string{"none", "plain", "login"}
ALLOWED_SECURITY_TYPES = []string{"none", "tls", "starttls"}
)
2020-12-05 17:24:53 +00:00
// Config
2018-08-21 08:36:54 +00:00
type Config struct {
EmailFrom string `mapstructure:"email-from"`
EmailTo []string `mapstructure:"email-to"`
EmailSubject string `mapstructure:"email-subject"`
2020-12-06 22:17:34 +00:00
SMTPHostname string `mapstructure:"smtp-hostname"`
SMTPPort uint16 `mapstructure:"smtp-port"`
SMTPUsername string `mapstructure:"smtp-username"`
SMTPPassword string `mapstructure:"smtp-password"`
SMTPAuthType string `mapstructure:"smtp-auth-type"`
SMTPSecurityType string `mapstructure:"smtp-security-type"`
TrelloURL string `mapstructure:"trello-url"`
TrelloAPIKey string `mapstructure:"trello-api-key"`
2019-09-04 05:35:49 +00:00
TrelloToken string `mapstructure:"trello-token"`
Parser *cobra.Command `mapstructure:"-"`
2018-08-21 08:36:54 +00:00
}
2020-12-06 22:17:34 +00:00
// NewConfig : create configuration object
2018-08-21 08:36:54 +00:00
func NewConfig() *Config {
2020-12-06 22:17:34 +00:00
config := &Config{}
cmd := &cobra.Command{
Use: programBinary,
Run: func(cmd *cobra.Command, args []string) { /* placeholder */ },
}
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
2020-12-06 22:17:34 +00:00
cmd.PersistentFlags().StringVarP(&config.EmailFrom, "email-from", "", "", "address of sender")
cmd.PersistentFlags().StringArrayVarP(&config.EmailTo, "email-to", "", []string{}, "address(es) of recipient(s)")
cmd.PersistentFlags().StringVarP(&config.EmailSubject, "email-subject", "", "", "email subject")
2020-12-06 22:17:34 +00:00
cmd.PersistentFlags().StringVarP(&config.TrelloURL, "trello-url", "", "", "url of trello board")
cmd.PersistentFlags().StringVarP(&config.TrelloURL, "trello-api-key", "", "", "API KEY for trello access")
cmd.PersistentFlags().StringVarP(&config.TrelloToken, "trello-token", "", "", "TOKEN for trello access")
2020-12-06 22:17:34 +00:00
cmd.PersistentFlags().StringVarP(&config.SMTPHostname, "smtp-hostname", "", "", "address of smtp server")
cmd.PersistentFlags().StringVarP(&config.SMTPUsername, "smtp-username", "", "", "username for smtp server")
cmd.PersistentFlags().StringVarP(&config.SMTPPassword, "smtp-password", "", "", "password for smtp server")
cmd.PersistentFlags().Uint16VarP(&config.SMTPPort, "smtp-port", "", 25, "port for smtp server")
cmd.PersistentFlags().StringVarP(&config.SMTPAuthType, "smtp-auth-type", "", "", "authentication type for smtp server")
cmd.PersistentFlags().StringVarP(&config.SMTPSecurityType, "smtp-security-type", "", "", "security type for smtp server")
2019-08-23 10:23:33 +00:00
viper.BindPFlag("email-from", cmd.PersistentFlags().Lookup("email-from"))
viper.BindPFlag("email-to", cmd.PersistentFlags().Lookup("email-to"))
viper.BindPFlag("email-subject", cmd.PersistentFlags().Lookup("email-subject"))
viper.BindPFlag("trello-url", cmd.PersistentFlags().Lookup("trello-url"))
viper.BindPFlag("trello-token", cmd.PersistentFlags().Lookup("trello-token"))
2019-09-04 05:35:49 +00:00
viper.BindPFlag("trello-api-key", cmd.PersistentFlags().Lookup("trello-api-key"))
viper.BindPFlag("smtp-hostname", cmd.PersistentFlags().Lookup("smtp-hostname"))
viper.BindPFlag("smtp-username", cmd.PersistentFlags().Lookup("smtp-username"))
viper.BindPFlag("smtp-password", cmd.PersistentFlags().Lookup("smtp-password"))
viper.BindPFlag("smtp-port", cmd.PersistentFlags().Lookup("smtp-port"))
viper.BindPFlag("smtp-auth-type", cmd.PersistentFlags().Lookup("smtp-auth-type"))
viper.BindPFlag("smtp-security-type", cmd.PersistentFlags().Lookup("smtp-security-type"))
2020-12-06 22:17:34 +00:00
config.Parser = cmd
return config
2018-08-21 08:36:54 +00:00
}
2020-12-06 22:17:34 +00:00
// Parse : handle command line options
func (config *Config) Parse() error {
// set config defaults
// persistent flags
// environment & config
// viper.SetEnvPrefix("")
2020-12-06 22:17:34 +00:00
if err := config.Parser.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
2018-08-21 08:36:54 +00:00
}
2020-12-06 22:17:34 +00:00
showHelp, _ := config.Parser.Flags().GetBool("help")
2018-12-27 12:35:57 +00:00
if showHelp {
os.Exit(0)
}
2020-12-06 22:17:34 +00:00
if err := viper.Unmarshal(&config); err != nil {
panic("Unable to unmarshal config")
2018-08-21 08:36:54 +00:00
}
2020-12-06 22:17:34 +00:00
// spew.Dump(config)
return nil
2018-08-21 08:36:54 +00:00
}