diff cmd/sievemgr/put.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 fc5e6970a0d5
line wrap: on
line diff
--- a/cmd/sievemgr/put.go	Tue Oct 27 19:17:56 2020 +0100
+++ b/cmd/sievemgr/put.go	Tue Nov 03 23:44:45 2020 +0100
@@ -24,60 +24,38 @@
 import (
 	"fmt"
 	"io"
-	"net"
 	"os"
 
 	"go.guido-berhoerster.org/managesieve"
 )
 
 func init() {
-	cmdPut.Flag.StringVar(&username, "u", "", "Set the username")
-	cmdPut.Flag.StringVar(&passwordFilename, "P", "",
-		"Set the name of the password file")
-	cmdCheck.Flag.StringVar(&username, "u", "", "Set the username")
-	cmdCheck.Flag.StringVar(&passwordFilename, "P", "",
-		"Set the name of the password file")
+	cmdPut.Flag.StringVar(&acctName, "a", "", "Select the account")
+	cmdCheck.Flag.StringVar(&acctName, "a", "", "Select the account")
 }
 
 var cmdPut = &command{
-	UsageLine: "put [options] host[:port] name [file]",
+	UsageLine: "put [options] name [file]",
 	Run:       runPut,
 }
 
 var cmdCheck = &command{
-	UsageLine: "check [options] host[:port] [file]",
+	UsageLine: "check [options] [file]",
 	Run:       runPut,
 }
 
 func runPut(cmd *command, args []string) error {
+	var err error
+	acct, err := getAccount(&conf, acctName)
+	if err != nil {
+		return err
+	}
+
 	var scriptName string
 	var r io.Reader = os.Stdin
-	var host, port string
-	var err error
 	if cmd.Name() == "put" {
 		switch len(args) {
-		case 3: // name and filename
-			scriptFile, err := os.Open(args[2])
-			if err != nil {
-				return fmt.Errorf("failed to open script file: %s\n",
-					err)
-			}
-			defer scriptFile.Close()
-			r = scriptFile
-			fallthrough
-		case 2: // only name
-			host, port, err = parseHostPort(args[0])
-			if err != nil {
-				return err
-			}
-
-			scriptName = args[1]
-		default:
-			return usageError("invalid number of arguments")
-		}
-	} else if cmd.Name() == "check" {
-		switch len(args) {
-		case 2: // filename
+		case 2: // name and filename
 			scriptFile, err := os.Open(args[1])
 			if err != nil {
 				return fmt.Errorf("failed to open script file: %s\n",
@@ -86,11 +64,8 @@
 			defer scriptFile.Close()
 			r = scriptFile
 			fallthrough
-		case 1:
-			host, port, err = parseHostPort(args[0])
-			if err != nil {
-				return err
-			}
+		case 1: // only name
+			scriptName = args[0]
 		default:
 			return usageError("invalid number of arguments")
 		}
@@ -115,14 +90,15 @@
 		return fmt.Errorf("failed to read script: %s\n", err)
 	}
 
-	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
 	}