Allow no config. Fix new lines. Add version flag.
This commit is contained in:
parent
02ab2ec867
commit
dc8f9ec86a
10
config.go
10
config.go
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
toml "github.com/pelletier/go-toml"
|
toml "github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
@ -71,21 +70,20 @@ func (c *Config) Merge(key string, value interface{}) {
|
||||||
Merge(key, value, c)
|
Merge(key, value, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig(configPath, section string) *Config {
|
func readConfig(configPath, section string) (*Config, error) {
|
||||||
config := NewConfig()
|
config := NewConfig()
|
||||||
tree, err := toml.LoadFile(configPath)
|
tree, err := toml.LoadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.F("Error in parsing the configuration\n%s", err)
|
return config, err
|
||||||
os.Exit(2)
|
|
||||||
}
|
}
|
||||||
keys := tree.Keys()
|
keys := tree.Keys()
|
||||||
|
|
||||||
if !IsPresent(keys, section) {
|
if !IsPresent(keys, section) {
|
||||||
return config
|
return config, errors.New("missing section")
|
||||||
}
|
}
|
||||||
sub := tree.Get(section).(*toml.Tree)
|
sub := tree.Get(section).(*toml.Tree)
|
||||||
err = sub.Unmarshal(config)
|
err = sub.Unmarshal(config)
|
||||||
return config
|
return config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkValidity(validations *[]Validation, obj interface{}, name, cmd, param string) {
|
func checkValidity(validations *[]Validation, obj interface{}, name, cmd, param string) {
|
||||||
|
|
42
main.go
42
main.go
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user