projects/sievemgr

changeset 6:2130614cd64a

Add checkspace subcommand for querying the server for sufficient space
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Sat Nov 07 09:28:27 2020 +0100 (3 months ago)
parents 4dff4c3f0fbb
children 3abc8be485c0
files cmd/sievemgr/checkspace.go cmd/sievemgr/main.go
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cmd/sievemgr/checkspace.go	Sat Nov 07 09:28:27 2020 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +// Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name>
     1.5 +//
     1.6 +// Permission is hereby granted, free of charge, to any person obtaining
     1.7 +// a copy of this software and associated documentation files (the
     1.8 +// "Software"), to deal in the Software without restriction, including
     1.9 +// without limitation the rights to use, copy, modify, merge, publish,
    1.10 +// distribute, sublicense, and/or sell copies of the Software, and to
    1.11 +// permit persons to whom the Software is furnished to do so, subject to
    1.12 +// the following conditions:
    1.13 +//
    1.14 +// The above copyright notice and this permission notice shall be included
    1.15 +// in all copies or substantial portions of the Software.
    1.16 +//
    1.17 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1.18 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.19 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.20 +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.21 +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.22 +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.23 +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.24 +
    1.25 +package main
    1.26 +
    1.27 +import (
    1.28 +	"fmt"
    1.29 +	"io"
    1.30 +	"io/ioutil"
    1.31 +	"os"
    1.32 +)
    1.33 +
    1.34 +func init() {
    1.35 +	cmdCheckSpace.Flag.StringVar(&acctName, "a", "", "Select the account")
    1.36 +}
    1.37 +
    1.38 +var cmdCheckSpace = &command{
    1.39 +	UsageLine: "checkspace [options] name [file]",
    1.40 +	Run:       runCheckSpace,
    1.41 +}
    1.42 +
    1.43 +func runCheckSpace(cmd *command, args []string) error {
    1.44 +	var err error
    1.45 +	acct, err := getAccount(&conf, acctName)
    1.46 +	if err != nil {
    1.47 +		return err
    1.48 +	}
    1.49 +
    1.50 +	var scriptName string
    1.51 +	var scriptSize int64
    1.52 +	switch len(args) {
    1.53 +	case 2: // name and filename
    1.54 +		scriptName = args[0]
    1.55 +		info, err := os.Stat(args[1])
    1.56 +		if err != nil {
    1.57 +			return fmt.Errorf("failed to stat script file: %s\n", err)
    1.58 +		}
    1.59 +		scriptSize = info.Size()
    1.60 +	case 1: // only name
    1.61 +		scriptName = args[0]
    1.62 +		scriptSize, err = io.Copy(ioutil.Discard, os.Stdin)
    1.63 +		if err != nil {
    1.64 +			return fmt.Errorf("failed to read script: %s\n",
    1.65 +				err)
    1.66 +		}
    1.67 +	default:
    1.68 +		return usageError("invalid number of arguments")
    1.69 +	}
    1.70 +
    1.71 +	if err := lookupHostPort(acct); err != nil {
    1.72 +		return err
    1.73 +	}
    1.74 +
    1.75 +	if err := readPassword(acct); err != nil {
    1.76 +		return err
    1.77 +	}
    1.78 +
    1.79 +	c, err := dialPlainAuth(acct)
    1.80 +	if err != nil {
    1.81 +		return err
    1.82 +	}
    1.83 +	defer c.Logout()
    1.84 +
    1.85 +	if ok, err := c.HaveSpace(scriptName, scriptSize); err != nil {
    1.86 +		return err
    1.87 +	} else if !ok {
    1.88 +		return errTooBig
    1.89 +	}
    1.90 +
    1.91 +	return nil
    1.92 +}
     2.1 --- a/cmd/sievemgr/main.go	Tue Nov 03 23:44:45 2020 +0100
     2.2 +++ b/cmd/sievemgr/main.go	Sat Nov 07 09:28:27 2020 +0100
     2.3 @@ -68,6 +68,7 @@
     2.4  	cmdDelete,
     2.5  	cmdInfo,
     2.6  	cmdCheck,
     2.7 +	cmdCheckSpace,
     2.8  }
     2.9  
    2.10  func usage() {