projects/sievemgr

changeset 4:f925f15d8ce5

Add check subcommand for checking the script validity
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue Oct 27 19:17:56 2020 +0100 (9 months ago)
parents 3cd182bff992
children 4dff4c3f0fbb
files cmd/sievemgr/main.go cmd/sievemgr/put.go
line diff
     1.1 --- a/cmd/sievemgr/main.go	Tue Oct 27 17:54:58 2020 +0100
     1.2 +++ b/cmd/sievemgr/main.go	Tue Oct 27 19:17:56 2020 +0100
     1.3 @@ -54,6 +54,7 @@
     1.4  	cmdDeactivate,
     1.5  	cmdDelete,
     1.6  	cmdInfo,
     1.7 +	cmdCheck,
     1.8  }
     1.9  
    1.10  func usage() {
     2.1 --- a/cmd/sievemgr/put.go	Tue Oct 27 17:54:58 2020 +0100
     2.2 +++ b/cmd/sievemgr/put.go	Tue Oct 27 19:17:56 2020 +0100
     2.3 @@ -34,6 +34,9 @@
     2.4  	cmdPut.Flag.StringVar(&username, "u", "", "Set the username")
     2.5  	cmdPut.Flag.StringVar(&passwordFilename, "P", "",
     2.6  		"Set the name of the password file")
     2.7 +	cmdCheck.Flag.StringVar(&username, "u", "", "Set the username")
     2.8 +	cmdCheck.Flag.StringVar(&passwordFilename, "P", "",
     2.9 +		"Set the name of the password file")
    2.10  }
    2.11  
    2.12  var cmdPut = &command{
    2.13 @@ -41,30 +44,70 @@
    2.14  	Run:       runPut,
    2.15  }
    2.16  
    2.17 +var cmdCheck = &command{
    2.18 +	UsageLine: "check [options] host[:port] [file]",
    2.19 +	Run:       runPut,
    2.20 +}
    2.21 +
    2.22  func runPut(cmd *command, args []string) error {
    2.23  	var scriptName string
    2.24  	var r io.Reader = os.Stdin
    2.25  	var host, port string
    2.26  	var err error
    2.27 -	switch len(args) {
    2.28 -	case 3: // name and filename
    2.29 -		scriptFile, err := os.Open(args[2])
    2.30 -		if err != nil {
    2.31 -			return fmt.Errorf("failed to open script file: %s\n",
    2.32 -				err)
    2.33 +	if cmd.Name() == "put" {
    2.34 +		switch len(args) {
    2.35 +		case 3: // name and filename
    2.36 +			scriptFile, err := os.Open(args[2])
    2.37 +			if err != nil {
    2.38 +				return fmt.Errorf("failed to open script file: %s\n",
    2.39 +					err)
    2.40 +			}
    2.41 +			defer scriptFile.Close()
    2.42 +			r = scriptFile
    2.43 +			fallthrough
    2.44 +		case 2: // only name
    2.45 +			host, port, err = parseHostPort(args[0])
    2.46 +			if err != nil {
    2.47 +				return err
    2.48 +			}
    2.49 +
    2.50 +			scriptName = args[1]
    2.51 +		default:
    2.52 +			return usageError("invalid number of arguments")
    2.53  		}
    2.54 -		defer scriptFile.Close()
    2.55 -		r = scriptFile
    2.56 -		fallthrough
    2.57 -	case 2: // only name
    2.58 -		host, port, err = parseHostPort(args[0])
    2.59 -		if err != nil {
    2.60 -			return err
    2.61 +	} else if cmd.Name() == "check" {
    2.62 +		switch len(args) {
    2.63 +		case 2: // filename
    2.64 +			scriptFile, err := os.Open(args[1])
    2.65 +			if err != nil {
    2.66 +				return fmt.Errorf("failed to open script file: %s\n",
    2.67 +					err)
    2.68 +			}
    2.69 +			defer scriptFile.Close()
    2.70 +			r = scriptFile
    2.71 +			fallthrough
    2.72 +		case 1:
    2.73 +			host, port, err = parseHostPort(args[0])
    2.74 +			if err != nil {
    2.75 +				return err
    2.76 +			}
    2.77 +		default:
    2.78 +			return usageError("invalid number of arguments")
    2.79  		}
    2.80 -
    2.81 -		scriptName = args[1]
    2.82 -	default:
    2.83 -		return usageError("invalid number of arguments")
    2.84 +	} else if cmd.Name() == "check" {
    2.85 +		switch len(args) {
    2.86 +		case 1: // filename
    2.87 +			scriptFile, err := os.Open(args[0])
    2.88 +			if err != nil {
    2.89 +				return fmt.Errorf("failed to open script file: %s\n",
    2.90 +					err)
    2.91 +			}
    2.92 +			defer scriptFile.Close()
    2.93 +			r = scriptFile
    2.94 +		case 0:
    2.95 +		default:
    2.96 +			return usageError("invalid number of arguments")
    2.97 +		}
    2.98  	}
    2.99  
   2.100  	script, err := readLimitedString(r, managesieve.ReadLimit)
   2.101 @@ -85,7 +128,13 @@
   2.102  	}
   2.103  	defer c.Logout()
   2.104  
   2.105 -	if warnings, err := c.PutScript(scriptName, script); err != nil {
   2.106 +	var warnings string
   2.107 +	if cmd.Name() == "put" {
   2.108 +		warnings, err = c.PutScript(scriptName, script)
   2.109 +	} else if cmd.Name() == "check" {
   2.110 +		warnings, err = c.CheckScript(script)
   2.111 +	}
   2.112 +	if err != nil {
   2.113  		return err
   2.114  	} else if warnings != "" {
   2.115  		fmt.Fprintln(os.Stderr, warnings)