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