Mercurial > projects > sievemgr
annotate cmd/sievemgr/checkspace.go @ 22:fc5e6970a0d5 default tip
Add support for specifying an authorization identity on the command line
author | Guido Berhoerster <guido+sievemgr@berhoerster.name> |
---|---|
date | Wed, 17 Feb 2021 07:50:55 +0100 |
parents | 2130614cd64a |
children |
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") |
22
fc5e6970a0d5
Add support for specifying an authorization identity on the command line
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
6
diff
changeset
|
33 cmdCheckSpace.Flag.StringVar(&authzID, "A", "", "Specify the authorization identity") |
6
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 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
36 var cmdCheckSpace = &command{ |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
37 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
|
38 Run: runCheckSpace, |
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 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
41 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
|
42 var err error |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
43 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
|
44 if err != nil { |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
45 return err |
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 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
48 var scriptName string |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
49 var scriptSize int64 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
50 switch len(args) { |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
51 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
|
52 scriptName = args[0] |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
53 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
|
54 if err != nil { |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
55 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
|
56 } |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
57 scriptSize = info.Size() |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
58 case 1: // only name |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
59 scriptName = args[0] |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
60 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
|
61 if err != nil { |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
62 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
|
63 err) |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
64 } |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
65 default: |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
66 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
|
67 } |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
68 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
69 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
|
70 return err |
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 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
73 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
|
74 return err |
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 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
77 c, err := dialPlainAuth(acct) |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
78 if err != nil { |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
79 return err |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
80 } |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
81 defer c.Logout() |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
82 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
83 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
|
84 return err |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
85 } else if !ok { |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
86 return errTooBig |
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 |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
89 return nil |
2130614cd64a
Add checkspace subcommand for querying the server for sufficient space
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff
changeset
|
90 } |