Fix variable names according to go fmt

This commit is contained in:
Glenn Y. Rolland 2020-12-06 23:17:34 +01:00
parent 218bf7e0d8
commit 10744a6a6d
2 changed files with 41 additions and 39 deletions

View file

@ -25,22 +25,23 @@ type Config struct {
EmailTo []string `mapstructure:"email-to"` EmailTo []string `mapstructure:"email-to"`
EmailSubject string `mapstructure:"email-subject"` EmailSubject string `mapstructure:"email-subject"`
SmtpHostname string `mapstructure:"smtp-hostname"` SMTPHostname string `mapstructure:"smtp-hostname"`
SmtpPort uint16 `mapstructure:"smtp-port"` SMTPPort uint16 `mapstructure:"smtp-port"`
SmtpUsername string `mapstructure:"smtp-username"` SMTPUsername string `mapstructure:"smtp-username"`
SmtpPassword string `mapstructure:"smtp-password"` SMTPPassword string `mapstructure:"smtp-password"`
SmtpAuthType string `mapstructure:"smtp-auth-type"` SMTPAuthType string `mapstructure:"smtp-auth-type"`
SmtpSecurityType string `mapstructure:"smtp-security-type"` SMTPSecurityType string `mapstructure:"smtp-security-type"`
TrelloUrl string `mapstructure:"trello-url"` TrelloURL string `mapstructure:"trello-url"`
TrelloApiKey string `mapstructure:"trello-api-key"` TrelloAPIKey string `mapstructure:"trello-api-key"`
TrelloToken string `mapstructure:"trello-token"` TrelloToken string `mapstructure:"trello-token"`
Parser *cobra.Command `mapstructure:"-"` Parser *cobra.Command `mapstructure:"-"`
} }
// NewConfig : create configuration object
func NewConfig() *Config { func NewConfig() *Config {
self := &Config{} config := &Config{}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: programBinary, Use: programBinary,
@ -50,20 +51,20 @@ func NewConfig() *Config {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv() viper.AutomaticEnv()
cmd.PersistentFlags().StringVarP(&self.EmailFrom, "email-from", "", "", "address of sender") cmd.PersistentFlags().StringVarP(&config.EmailFrom, "email-from", "", "", "address of sender")
cmd.PersistentFlags().StringArrayVarP(&self.EmailTo, "email-to", "", []string{}, "address(es) of recipient(s)") cmd.PersistentFlags().StringArrayVarP(&config.EmailTo, "email-to", "", []string{}, "address(es) of recipient(s)")
cmd.PersistentFlags().StringVarP(&self.EmailSubject, "email-subject", "", "", "email subject") cmd.PersistentFlags().StringVarP(&config.EmailSubject, "email-subject", "", "", "email subject")
cmd.PersistentFlags().StringVarP(&self.TrelloUrl, "trello-url", "", "", "url of trello board") cmd.PersistentFlags().StringVarP(&config.TrelloURL, "trello-url", "", "", "url of trello board")
cmd.PersistentFlags().StringVarP(&self.TrelloUrl, "trello-api-key", "", "", "API KEY for trello access") cmd.PersistentFlags().StringVarP(&config.TrelloURL, "trello-api-key", "", "", "API KEY for trello access")
cmd.PersistentFlags().StringVarP(&self.TrelloToken, "trello-token", "", "", "TOKEN for trello access") cmd.PersistentFlags().StringVarP(&config.TrelloToken, "trello-token", "", "", "TOKEN for trello access")
cmd.PersistentFlags().StringVarP(&self.SmtpHostname, "smtp-hostname", "", "", "address of smtp server") cmd.PersistentFlags().StringVarP(&config.SMTPHostname, "smtp-hostname", "", "", "address of smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpUsername, "smtp-username", "", "", "username for smtp server") cmd.PersistentFlags().StringVarP(&config.SMTPUsername, "smtp-username", "", "", "username for smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpPassword, "smtp-password", "", "", "password for smtp server") cmd.PersistentFlags().StringVarP(&config.SMTPPassword, "smtp-password", "", "", "password for smtp server")
cmd.PersistentFlags().Uint16VarP(&self.SmtpPort, "smtp-port", "", 25, "port for smtp server") cmd.PersistentFlags().Uint16VarP(&config.SMTPPort, "smtp-port", "", 25, "port for smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpAuthType, "smtp-auth-type", "", "", "authentication type for smtp server") cmd.PersistentFlags().StringVarP(&config.SMTPAuthType, "smtp-auth-type", "", "", "authentication type for smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpSecurityType, "smtp-security-type", "", "", "security type for smtp server") cmd.PersistentFlags().StringVarP(&config.SMTPSecurityType, "smtp-security-type", "", "", "security type for smtp server")
viper.BindPFlag("email-from", cmd.PersistentFlags().Lookup("email-from")) viper.BindPFlag("email-from", cmd.PersistentFlags().Lookup("email-from"))
viper.BindPFlag("email-to", cmd.PersistentFlags().Lookup("email-to")) viper.BindPFlag("email-to", cmd.PersistentFlags().Lookup("email-to"))
@ -78,30 +79,31 @@ func NewConfig() *Config {
viper.BindPFlag("smtp-auth-type", cmd.PersistentFlags().Lookup("smtp-auth-type")) viper.BindPFlag("smtp-auth-type", cmd.PersistentFlags().Lookup("smtp-auth-type"))
viper.BindPFlag("smtp-security-type", cmd.PersistentFlags().Lookup("smtp-security-type")) viper.BindPFlag("smtp-security-type", cmd.PersistentFlags().Lookup("smtp-security-type"))
self.Parser = cmd config.Parser = cmd
return self return config
} }
func (self *Config) Parse() error { // Parse : handle command line options
func (config *Config) Parse() error {
// set config defaults // set config defaults
// persistent flags // persistent flags
// environment & config // environment & config
// viper.SetEnvPrefix("") // viper.SetEnvPrefix("")
if err := self.Parser.Execute(); err != nil { if err := config.Parser.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
showHelp, _ := self.Parser.Flags().GetBool("help") showHelp, _ := config.Parser.Flags().GetBool("help")
if showHelp { if showHelp {
os.Exit(0) os.Exit(0)
} }
if err := viper.Unmarshal(&self); err != nil { if err := viper.Unmarshal(&config); err != nil {
panic("Unable to unmarshal config") panic("Unable to unmarshal config")
} }
// spew.Dump(self) // spew.Dump(config)
return nil return nil
} }

View file

@ -16,7 +16,7 @@ func main() {
// Get task list as markdown // Get task list as markdown
fmt.Println("d: configuring trello") fmt.Println("d: configuring trello")
trelloCtx := NewTrello(config.TrelloApiKey, config.TrelloToken) trelloCtx := NewTrello(config.TrelloAPIKey, config.TrelloToken)
if trelloCtx == nil { if trelloCtx == nil {
fmt.Println("ERROR: Unable to initialize trello context") fmt.Println("ERROR: Unable to initialize trello context")
os.Exit(1) os.Exit(1)
@ -24,9 +24,9 @@ func main() {
fmt.Println("d: getting trello boards") fmt.Println("d: getting trello boards")
var trelloBoardsList []TrelloBoard var trelloBoardsList []TrelloBoard
if len(config.TrelloUrl) > 0 { if len(config.TrelloURL) > 0 {
fmt.Printf("d: using given url %s\n", config.TrelloUrl) fmt.Printf("d: using given url %s\n", config.TrelloURL)
trelloBoard := trelloCtx.GetBoard(config.TrelloUrl) trelloBoard := trelloCtx.GetBoard(config.TrelloURL)
trelloBoardsList = append(trelloBoardsList, trelloBoard) trelloBoardsList = append(trelloBoardsList, trelloBoard)
} else { } else {
fmt.Println("d: fetching boards") fmt.Println("d: fetching boards")
@ -58,21 +58,21 @@ func main() {
// Connect and send email // Connect and send email
var transport *mail.Dialer var transport *mail.Dialer
if len(config.SmtpUsername) > 0 { if len(config.SMTPUsername) > 0 {
fmt.Println("d: transport w/ username") fmt.Println("d: transport w/ username")
transport = mail.NewDialer( transport = mail.NewDialer(
config.SmtpHostname, config.SMTPHostname,
int(config.SmtpPort), int(config.SMTPPort),
config.SmtpUsername, config.SMTPUsername,
config.SmtpPassword, config.SMTPPassword,
) )
// disable cert verification // disable cert verification
transport.TLSConfig = &tls.Config{InsecureSkipVerify: true} transport.TLSConfig = &tls.Config{InsecureSkipVerify: true}
} else { } else {
fmt.Println("d: transport w/out username") fmt.Println("d: transport w/out username")
transport = &mail.Dialer{ transport = &mail.Dialer{
Host: config.SmtpHostname, Host: config.SMTPHostname,
Port: int(config.SmtpPort), Port: int(config.SMTPPort),
} }
} }