|
@@ -8,6 +8,8 @@ import (
|
|
"strings"
|
|
"strings"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+var version = "0.1"
|
|
|
|
+
|
|
func readFromConsole() string {
|
|
func readFromConsole() string {
|
|
var text, line string
|
|
var text, line string
|
|
var err error
|
|
var err error
|
|
@@ -17,6 +19,8 @@ func readFromConsole() string {
|
|
line, err = reader.ReadString('\n')
|
|
line, err = reader.ReadString('\n')
|
|
if line == "\n" {
|
|
if line == "\n" {
|
|
counter += 1
|
|
counter += 1
|
|
|
|
+ } else {
|
|
|
|
+ counter = 0
|
|
}
|
|
}
|
|
text += fmt.Sprintf("%s\n", line)
|
|
text += fmt.Sprintf("%s\n", line)
|
|
}
|
|
}
|
|
@@ -24,7 +28,7 @@ func readFromConsole() string {
|
|
Error.F("Error in reading text from console\n%s", err)
|
|
Error.F("Error in reading text from console\n%s", err)
|
|
os.Exit(1)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
- return text
|
|
|
|
|
|
+ return strings.TrimRight(text, "\n")
|
|
}
|
|
}
|
|
|
|
|
|
func toList(input string) []string {
|
|
func toList(input string) []string {
|
|
@@ -38,13 +42,37 @@ func toList(input string) []string {
|
|
return result
|
|
return result
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func initializeConfig(configPath, section string) *Config {
|
|
|
|
+ if configPath == "" {
|
|
|
|
+ configPath = "/etc/sendmail.toml"
|
|
|
|
+
|
|
|
|
+ // Ignore non existing config only if `-conf` not used on command line.
|
|
|
|
+ if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
+ return NewConfig()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ config, err := readConfig(configPath, section)
|
|
|
|
+ if err != nil {
|
|
|
|
+ if os.IsNotExist(err) {
|
|
|
|
+ Error.F("Error in parsing the configuration\n%s", err)
|
|
|
|
+ os.Exit(-2)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Debug.F("---\nConfig from %s\n%s", configPath, *config)
|
|
|
|
+ return config
|
|
|
|
+}
|
|
|
|
+
|
|
func main() {
|
|
func main() {
|
|
var err error
|
|
var err error
|
|
var configPath, section, serverAddress, user, password, to, cc, bcc, from, subject, text string
|
|
var configPath, section, serverAddress, user, password, to, cc, bcc, from, subject, text string
|
|
- var encryption, dbg bool
|
|
|
|
|
|
+ var encryption, dbg, versionFlag bool
|
|
var serverPort_ int
|
|
var serverPort_ int
|
|
var serverPort int64
|
|
var serverPort int64
|
|
- flag.StringVar(&configPath, "conf", "/etc/sendmail.toml", "Path to a config file (defaults to /etc/sendmail.toml)")
|
|
|
|
|
|
+
|
|
|
|
+ flag.BoolVar(&versionFlag, "version", false, "Prints the version and exits")
|
|
|
|
+ flag.StringVar(&configPath, "conf", "", "Path to a config file (defaults to /etc/sendmail.toml)")
|
|
flag.StringVar(§ion, "section", "default", "Section of the conf to read (defaults to \"default\")")
|
|
flag.StringVar(§ion, "section", "default", "Section of the conf to read (defaults to \"default\")")
|
|
flag.BoolVar(&dbg, "dbg", false, "Enable debugging output")
|
|
flag.BoolVar(&dbg, "dbg", false, "Enable debugging output")
|
|
flag.StringVar(&serverAddress, "server-address", "", "The SMTP server address")
|
|
flag.StringVar(&serverAddress, "server-address", "", "The SMTP server address")
|
|
@@ -61,6 +89,11 @@ func main() {
|
|
|
|
|
|
LogInit(dbg)
|
|
LogInit(dbg)
|
|
|
|
|
|
|
|
+ if versionFlag {
|
|
|
|
+ Info.F("Version: %s", version)
|
|
|
|
+ os.Exit(0)
|
|
|
|
+ }
|
|
|
|
+
|
|
if flag.NArg() == 0 {
|
|
if flag.NArg() == 0 {
|
|
text = readFromConsole()
|
|
text = readFromConsole()
|
|
} else {
|
|
} else {
|
|
@@ -95,9 +128,8 @@ parameters:
|
|
from,
|
|
from,
|
|
subject,
|
|
subject,
|
|
)
|
|
)
|
|
- config := readConfig(configPath, section)
|
|
|
|
- Debug.F("---\nConfig from %s\n%s", configPath, *config)
|
|
|
|
|
|
|
|
|
|
+ config := initializeConfig(configPath, section)
|
|
config.Server.Merge("Address", serverAddress)
|
|
config.Server.Merge("Address", serverAddress)
|
|
config.Server.Merge("Port", serverPort)
|
|
config.Server.Merge("Port", serverPort)
|
|
config.Server.Merge("Encryption", encryption)
|
|
config.Server.Merge("Encryption", encryption)
|