Browse Source

Allow no config. Fix new lines. Add version flag.

Blallo 4 years ago
parent
commit
dc8f9ec86a
2 changed files with 41 additions and 11 deletions
  1. 4 6
      config.go
  2. 37 5
      main.go

+ 4 - 6
config.go

@@ -3,7 +3,6 @@ package main
 import (
 	"errors"
 	"fmt"
-	"os"
 
 	toml "github.com/pelletier/go-toml"
 )
@@ -71,21 +70,20 @@ func (c *Config) Merge(key string, value interface{}) {
 	Merge(key, value, c)
 }
 
-func readConfig(configPath, section string) *Config {
+func readConfig(configPath, section string) (*Config, error) {
 	config := NewConfig()
 	tree, err := toml.LoadFile(configPath)
 	if err != nil {
-		Error.F("Error in parsing the configuration\n%s", err)
-		os.Exit(2)
+		return config, err
 	}
 	keys := tree.Keys()
 
 	if !IsPresent(keys, section) {
-		return config
+		return config, errors.New("missing section")
 	}
 	sub := tree.Get(section).(*toml.Tree)
 	err = sub.Unmarshal(config)
-	return config
+	return config, err
 }
 
 func checkValidity(validations *[]Validation, obj interface{}, name, cmd, param string) {

+ 37 - 5
main.go

@@ -8,6 +8,8 @@ import (
 	"strings"
 )
 
+var version = "0.1"
+
 func readFromConsole() string {
 	var text, line string
 	var err error
@@ -17,6 +19,8 @@ func readFromConsole() string {
 		line, err = reader.ReadString('\n')
 		if line == "\n" {
 			counter += 1
+		} else {
+			counter = 0
 		}
 		text += fmt.Sprintf("%s\n", line)
 	}
@@ -24,7 +28,7 @@ func readFromConsole() string {
 		Error.F("Error in reading text from console\n%s", err)
 		os.Exit(1)
 	}
-	return text
+	return strings.TrimRight(text, "\n")
 }
 
 func toList(input string) []string {
@@ -38,13 +42,37 @@ func toList(input string) []string {
 	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() {
 	var err error
 	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 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(&section, "section", "default", "Section of the conf to read (defaults to \"default\")")
 	flag.BoolVar(&dbg, "dbg", false, "Enable debugging output")
 	flag.StringVar(&serverAddress, "server-address", "", "The SMTP server address")
@@ -61,6 +89,11 @@ func main() {
 
 	LogInit(dbg)
 
+	if versionFlag {
+		Info.F("Version: %s", version)
+		os.Exit(0)
+	}
+
 	if flag.NArg() == 0 {
 		text = readFromConsole()
 	} else {
@@ -95,9 +128,8 @@ parameters:
 		from,
 		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("Port", serverPort)
 	config.Server.Merge("Encryption", encryption)