changeset 4:f925f15d8ce5

Add check subcommand for checking the script validity
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue, 27 Oct 2020 19:17:56 +0100
parents 3cd182bff992
children 4dff4c3f0fbb
files cmd/sievemgr/main.go cmd/sievemgr/put.go
diffstat 2 files changed, 68 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/cmd/sievemgr/main.go	Tue Oct 27 17:54:58 2020 +0100
+++ b/cmd/sievemgr/main.go	Tue Oct 27 19:17:56 2020 +0100
@@ -54,6 +54,7 @@
 	cmdDeactivate,
 	cmdDelete,
 	cmdInfo,
+	cmdCheck,
 }
 
 func usage() {
--- a/cmd/sievemgr/put.go	Tue Oct 27 17:54:58 2020 +0100
+++ b/cmd/sievemgr/put.go	Tue Oct 27 19:17:56 2020 +0100
@@ -34,6 +34,9 @@
 	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")
 }
 
 var cmdPut = &command{
@@ -41,30 +44,70 @@
 	Run:       runPut,
 }
 
+var cmdCheck = &command{
+	UsageLine: "check [options] host[:port] [file]",
+	Run:       runPut,
+}
+
 func runPut(cmd *command, args []string) error {
 	var scriptName string
 	var r io.Reader = os.Stdin
 	var host, port string
 	var err error
-	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)
+	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")
 		}
-		defer scriptFile.Close()
-		r = scriptFile
-		fallthrough
-	case 2: // only name
-		host, port, err = parseHostPort(args[0])
-		if err != nil {
-			return err
+	} else if cmd.Name() == "check" {
+		switch len(args) {
+		case 2: // filename
+			scriptFile, err := os.Open(args[1])
+			if err != nil {
+				return fmt.Errorf("failed to open script file: %s\n",
+					err)
+			}
+			defer scriptFile.Close()
+			r = scriptFile
+			fallthrough
+		case 1:
+			host, port, err = parseHostPort(args[0])
+			if err != nil {
+				return err
+			}
+		default:
+			return usageError("invalid number of arguments")
 		}
-
-		scriptName = args[1]
-	default:
-		return usageError("invalid number of arguments")
+	} else if cmd.Name() == "check" {
+		switch len(args) {
+		case 1: // filename
+			scriptFile, err := os.Open(args[0])
+			if err != nil {
+				return fmt.Errorf("failed to open script file: %s\n",
+					err)
+			}
+			defer scriptFile.Close()
+			r = scriptFile
+		case 0:
+		default:
+			return usageError("invalid number of arguments")
+		}
 	}
 
 	script, err := readLimitedString(r, managesieve.ReadLimit)
@@ -85,7 +128,13 @@
 	}
 	defer c.Logout()
 
-	if warnings, err := c.PutScript(scriptName, script); err != nil {
+	var warnings string
+	if cmd.Name() == "put" {
+		warnings, err = c.PutScript(scriptName, script)
+	} else if cmd.Name() == "check" {
+		warnings, err = c.CheckScript(script)
+	}
+	if err != nil {
 		return err
 	} else if warnings != "" {
 		fmt.Fprintln(os.Stderr, warnings)