2018-08-21 08:36:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-23 10:46:12 +00:00
|
|
|
// "errors"
|
2018-08-21 08:36:54 +00:00
|
|
|
"fmt"
|
2019-08-23 10:23:33 +00:00
|
|
|
// "github.com/davecgh/go-spew/spew"
|
2018-11-23 10:46:12 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2018-11-23 10:46:12 +00:00
|
|
|
const (
|
|
|
|
programBinary string = "trello2mail"
|
|
|
|
)
|
2018-08-21 17:21:46 +00:00
|
|
|
|
2018-11-23 10:46:12 +00:00
|
|
|
var (
|
|
|
|
ALLOWED_AUTH_TYPES = []string{"none", "plain", "login"}
|
|
|
|
ALLOWED_SECURITY_TYPES = []string{"none", "tls", "starttls"}
|
|
|
|
)
|
2018-08-21 17:21:46 +00:00
|
|
|
|
2018-08-21 08:36:54 +00:00
|
|
|
type Config struct {
|
2018-11-23 10:46:12 +00:00
|
|
|
EmailFrom string `mapstructure:"email-from"`
|
|
|
|
EmailTo []string `mapstructure:"email-to"`
|
|
|
|
EmailSubject string `mapstructure:"email-subject"`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
TrelloToken string `mapstructure:"trello-token"`
|
|
|
|
|
|
|
|
Parser *cobra.Command `mapstructure:"-"`
|
2018-08-21 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig() *Config {
|
2018-11-23 10:46:12 +00:00
|
|
|
self := &Config{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: programBinary,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) { /* placeholder */ },
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.EmailFrom, "email-from", "", "", "address of sender")
|
|
|
|
cmd.PersistentFlags().StringArrayVarP(&self.EmailTo, "email-to", "", []string{}, "address(es) of recipient(s)")
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.EmailSubject, "email-subject", "", "", "email subject")
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.TrelloUrl, "trello-url", "", "", "url of trello board")
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.TrelloToken, "trello-token", "", "", "url of trello token")
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.SmtpHostname, "smtp-hostname", "", "", "address of smtp server")
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.SmtpUsername, "smtp-username", "", "", "username for smtp server")
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.SmtpPassword, "smtp-password", "", "", "password for smtp server")
|
|
|
|
cmd.PersistentFlags().Uint16VarP(&self.SmtpPort, "smtp-port", "", 25, "port for smtp server")
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.SmtpAuthType, "smtp-auth-type", "", "", "authentication type for smtp server")
|
|
|
|
cmd.PersistentFlags().StringVarP(&self.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"))
|
2018-11-23 10:46:12 +00:00
|
|
|
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"))
|
|
|
|
|
|
|
|
self.Parser = cmd
|
|
|
|
return self
|
2018-08-21 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 10:46:12 +00:00
|
|
|
func (self *Config) Parse() error {
|
|
|
|
// set config defaults
|
|
|
|
// persistent flags
|
|
|
|
// environment & config
|
|
|
|
// viper.SetEnvPrefix("")
|
|
|
|
|
|
|
|
if err := self.Parser.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2018-08-21 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 12:35:57 +00:00
|
|
|
showHelp, _ := self.Parser.Flags().GetBool("help")
|
|
|
|
if showHelp {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-11-23 10:46:12 +00:00
|
|
|
if err := viper.Unmarshal(&self); err != nil {
|
|
|
|
panic("Unable to unmarshal config")
|
2018-08-21 08:36:54 +00:00
|
|
|
}
|
2018-11-23 10:46:12 +00:00
|
|
|
|
2019-08-23 10:23:33 +00:00
|
|
|
// spew.Dump(self)
|
2018-11-23 10:46:12 +00:00
|
|
|
return nil
|
2018-08-21 08:36:54 +00:00
|
|
|
}
|