gomailer/cmd/root.go

105 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-04-06 23:09:43 +00:00
package cmd
import (
2023-04-08 00:31:59 +00:00
"log"
2023-04-06 23:09:43 +00:00
"os"
2023-04-08 00:31:59 +00:00
"git.dafu.dev/dafu/gomailer/pkg"
2023-04-06 23:09:43 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
2023-04-08 00:31:59 +00:00
var Cfg Config
type Config struct {
mailserver gomailer.MailServer `mapstructure:"mailserver"`
mailsettings gomailer.MailSettings `mapstructure:"mailsettings"`
}
2023-04-06 23:09:43 +00:00
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gomailer",
Short: "use it to test email templates",
Args: cobra.MinimumNArgs(1),
2023-04-08 00:31:59 +00:00
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
log.Printf("Templates: %v", args)
for _, arg := range args {
gomailer.Inline(arg)
}
},
2023-04-06 23:09:43 +00:00
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.MousetrapHelpText = "" // disable warning on windows for drag n drop to exe
2023-04-06 23:09:43 +00:00
cobra.OnInitialize(initConfig)
2023-04-08 00:31:59 +00:00
rootCmd.CompletionOptions.DisableDefaultCmd = true
2023-04-06 23:09:43 +00:00
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gomailer.yaml)")
2023-04-08 00:31:59 +00:00
// rootCmd.PersistentFlags().StringVarP(&Template, "template", "t", "", "Path to html template")
// viper.BindPFlag("template", rootCmd.PersistentFlags().Lookup("template"))
2023-04-06 23:09:43 +00:00
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
2023-04-08 00:31:59 +00:00
viper.AddConfigPath(".")
2023-04-06 23:09:43 +00:00
viper.SetConfigType("yaml")
2023-04-08 00:31:59 +00:00
viper.SetConfigName("gomailer")
2023-04-06 23:09:43 +00:00
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
2023-04-08 00:31:59 +00:00
mserver := &gomailer.MailServer{}
msettings := &gomailer.MailSettings{}
if err := viper.UnmarshalKey("mailserver", mserver); err != nil {
log.Fatal(err)
}
if err := viper.UnmarshalKey("mailsettings", msettings); err != nil {
log.Fatal(err)
}
Cfg.mailserver = *mserver
Cfg.mailsettings = *msettings
} else {
generateDefaultConfig()
2023-04-06 23:09:43 +00:00
}
}
2023-04-08 00:31:59 +00:00
func generateDefaultConfig() {
// mailserver
viper.SetDefault("mailserver.Port", 25)
viper.SetDefault("mailserver.Host", "localhost")
viper.SetDefault("mailserver.Username", "user")
viper.SetDefault("mailserver.Password", "pass")
viper.SetDefault("mailserver.Tls", "notls|mandatory")
viper.SetDefault("mailserver.Authtype", "nologin|plain|login|cram-md5")
viper.SetDefault("mailserver.Skipverify", false)
// mailsettings
viper.SetDefault("mailsettings.Subject", "subject")
viper.SetDefault("mailsettings.From", "from@local")
viper.SetDefault("mailsettings.To", "to@local")
viper.SetDefault("mailsettings.Cc", "cc@local")
viper.SetDefault("mailsettings.Template", "email.html")
viper.SetDefault("mailsettings.Inline", true)
viper.SafeWriteConfig()
log.Printf("Writing default config file: " + viper.ConfigFileUsed())
}