diff cmd/sievemgr/main.go @ 5:4dff4c3f0fbb

Introduce configuration file where account information is specified Introduce a configuration file where account information must be specified instead of passing it with each invocation on the command line. Each account has a name by which it can be selected and one may be specified as the default account. This is intended to improve usability for productive usage. Enforce strict permissions since passwords may be specified for non-interactive usage. Remove command-line flags for passing account information.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue, 03 Nov 2020 23:44:45 +0100
parents f925f15d8ce5
children 2130614cd64a
line wrap: on
line diff
--- a/cmd/sievemgr/main.go	Tue Oct 27 19:17:56 2020 +0100
+++ b/cmd/sievemgr/main.go	Tue Nov 03 23:44:45 2020 +0100
@@ -26,6 +26,8 @@
 	"flag"
 	"fmt"
 	"os"
+
+	"go.guido-berhoerster.org/sievemgr/cmd/sievemgr/internal/config"
 )
 
 const (
@@ -40,12 +42,23 @@
 	return string(e)
 }
 
+// command-line flags
 var (
-	skipCertVerify   bool
-	username         string
-	passwordFilename string
+	confFilename   string
+	acctName       string
 )
 
+var conf config.Configuration
+
+func init() {
+	var err error
+	confFilename, err = config.DefaultFilename()
+	if err != nil {
+		fmt.Fprintln(flag.CommandLine.Output(), err)
+		os.Exit(exitFailure)
+	}
+}
+
 var cmds = []*command{
 	cmdList,
 	cmdPut,
@@ -71,8 +84,8 @@
 
 func main() {
 	flag.Usage = usage
-	flag.BoolVar(&skipCertVerify, "I", false,
-		"Skip TLS certificate verification")
+	flag.StringVar(&confFilename, "f", confFilename,
+		"Set the name of the configuration file")
 	flag.Parse()
 	if flag.NArg() == 0 {
 		fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand")
@@ -80,6 +93,11 @@
 		os.Exit(exitUsage)
 	}
 
+	if err := config.ParseFile(confFilename, &conf); err != nil {
+		fmt.Fprintln(flag.CommandLine.Output(), err)
+		os.Exit(exitFailure)
+	}
+
 	name := flag.Arg(0)
 	var cmd *command
 	for _, c := range cmds {