Mercurial > projects > sievemgr
comparison cmd/sievemgr/checkspace.go @ 6:2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
author | Guido Berhoerster <guido+sievemgr@berhoerster.name> |
---|---|
date | Sat, 07 Nov 2020 09:28:27 +0100 |
parents | |
children | fc5e6970a0d5 |
comparison
equal
deleted
inserted
replaced
5:4dff4c3f0fbb | 6:2130614cd64a |
---|---|
1 // Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name> | |
2 // | |
3 // Permission is hereby granted, free of charge, to any person obtaining | |
4 // a copy of this software and associated documentation files (the | |
5 // "Software"), to deal in the Software without restriction, including | |
6 // without limitation the rights to use, copy, modify, merge, publish, | |
7 // distribute, sublicense, and/or sell copies of the Software, and to | |
8 // permit persons to whom the Software is furnished to do so, subject to | |
9 // the following conditions: | |
10 // | |
11 // The above copyright notice and this permission notice shall be included | |
12 // in all copies or substantial portions of the Software. | |
13 // | |
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
21 | |
22 package main | |
23 | |
24 import ( | |
25 "fmt" | |
26 "io" | |
27 "io/ioutil" | |
28 "os" | |
29 ) | |
30 | |
31 func init() { | |
32 cmdCheckSpace.Flag.StringVar(&acctName, "a", "", "Select the account") | |
33 } | |
34 | |
35 var cmdCheckSpace = &command{ | |
36 UsageLine: "checkspace [options] name [file]", | |
37 Run: runCheckSpace, | |
38 } | |
39 | |
40 func runCheckSpace(cmd *command, args []string) error { | |
41 var err error | |
42 acct, err := getAccount(&conf, acctName) | |
43 if err != nil { | |
44 return err | |
45 } | |
46 | |
47 var scriptName string | |
48 var scriptSize int64 | |
49 switch len(args) { | |
50 case 2: // name and filename | |
51 scriptName = args[0] | |
52 info, err := os.Stat(args[1]) | |
53 if err != nil { | |
54 return fmt.Errorf("failed to stat script file: %s\n", err) | |
55 } | |
56 scriptSize = info.Size() | |
57 case 1: // only name | |
58 scriptName = args[0] | |
59 scriptSize, err = io.Copy(ioutil.Discard, os.Stdin) | |
60 if err != nil { | |
61 return fmt.Errorf("failed to read script: %s\n", | |
62 err) | |
63 } | |
64 default: | |
65 return usageError("invalid number of arguments") | |
66 } | |
67 | |
68 if err := lookupHostPort(acct); err != nil { | |
69 return err | |
70 } | |
71 | |
72 if err := readPassword(acct); err != nil { | |
73 return err | |
74 } | |
75 | |
76 c, err := dialPlainAuth(acct) | |
77 if err != nil { | |
78 return err | |
79 } | |
80 defer c.Logout() | |
81 | |
82 if ok, err := c.HaveSpace(scriptName, scriptSize); err != nil { | |
83 return err | |
84 } else if !ok { | |
85 return errTooBig | |
86 } | |
87 | |
88 return nil | |
89 } |