diff cmd/sievemgr/activate.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 0cd5a454dfb4
children fc5e6970a0d5
line wrap: on
line diff
--- a/cmd/sievemgr/activate.go	Tue Oct 27 19:17:56 2020 +0100
+++ b/cmd/sievemgr/activate.go	Tue Nov 03 23:44:45 2020 +0100
@@ -21,53 +21,46 @@
 
 package main
 
-import (
-	"net"
-)
-
 func init() {
-	cmdActivate.Flag.StringVar(&username, "u", "", "Set the username")
-	cmdActivate.Flag.StringVar(&passwordFilename, "P", "",
-		"Set the name of the password file")
-	cmdDeactivate.Flag.StringVar(&username, "u", "", "Set the username")
-	cmdDeactivate.Flag.StringVar(&passwordFilename, "P", "",
-		"Set the name of the password file")
+	cmdActivate.Flag.StringVar(&acctName, "a", "", "Select the account")
+	cmdDeactivate.Flag.StringVar(&acctName, "a", "", "Select the account")
 }
 
 var cmdActivate = &command{
-	UsageLine: "activate [options] host[:port] name",
+	UsageLine: "activate [options] name",
 	Run:       runActivate,
 }
 
 var cmdDeactivate = &command{
-	UsageLine: "deactivate [options] host[:port]",
+	UsageLine: "deactivate [options]",
 	Run:       runActivate,
 }
 
 func runActivate(cmd *command, args []string) error {
-	if (cmd.Name() == "activate" && len(args) != 2) ||
-		(cmd.Name() == "deactivate" && len(args) != 1) {
+	if (cmd.Name() == "activate" && len(args) != 1) ||
+		(cmd.Name() == "deactivate" && len(args) != 0) {
 		return usageError("invalid number of arguments")
 	}
 
-	host, port, err := parseHostPort(args[0])
+	var scriptName string
+	if len(args) > 0 {
+		scriptName = args[0]
+	}
+
+	acct, err := getAccount(&conf, acctName)
 	if err != nil {
 		return err
 	}
 
-	var scriptName string
-	if len(args) > 1 {
-		scriptName = args[1]
-	}
-
-	username, password, err := usernamePassword(host, port, username,
-		passwordFilename)
-	if err != nil {
+	if err := lookupHostPort(acct); err != nil {
 		return err
 	}
 
-	c, err := dialPlainAuth(net.JoinHostPort(host, port), username,
-		password)
+	if err := readPassword(acct); err != nil {
+		return err
+	}
+
+	c, err := dialPlainAuth(acct)
 	if err != nil {
 		return err
 	}