comparison cmd/sievemgr/main.go @ 0:b00673734e58

Initial revision Split off sievemgr command from the managesieve repository.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Mon, 26 Oct 2020 14:19:24 +0100
parents
children 0cd5a454dfb4
comparison
equal deleted inserted replaced
-1:000000000000 0:b00673734e58
1 // Copyright (C) 2020 Guido Berhoerster <guido+managesieve@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 "errors"
26 "flag"
27 "fmt"
28 "os"
29 )
30
31 const (
32 exitSuccess = iota
33 exitFailure
34 exitUsage
35 )
36
37 type usageError string
38
39 func (e usageError) Error() string {
40 return string(e)
41 }
42
43 var (
44 skipCertVerify bool
45 username string
46 passwordFilename string
47 )
48
49 var cmds = []*command{
50 cmdList,
51 cmdPut,
52 cmdGet,
53 cmdActivate,
54 cmdDeactivate,
55 cmdDelete,
56 cmdInfo,
57 }
58
59 func usage() {
60 fmt.Fprintf(flag.CommandLine.Output(),
61 "usage:\n %s [options] [subcommand [options] [arguments]]\n",
62 flag.CommandLine.Name())
63 fmt.Fprintln(flag.CommandLine.Output(), "subcommands:")
64 for _, cmd := range cmds {
65 fmt.Fprintf(flag.CommandLine.Output(), " %s\n", cmd.Name())
66 }
67 fmt.Fprintln(flag.CommandLine.Output(), "global options:")
68 flag.PrintDefaults()
69 }
70
71 func main() {
72 flag.Usage = usage
73 flag.BoolVar(&skipCertVerify, "I", false,
74 "Skip TLS certificate verification")
75 flag.Parse()
76 if flag.NArg() == 0 {
77 fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand")
78 usage()
79 os.Exit(exitUsage)
80 }
81
82 name := flag.Arg(0)
83 var cmd *command
84 for _, c := range cmds {
85 if c.Name() == name {
86 cmd = c
87 break
88 }
89 }
90 if cmd == nil {
91 fmt.Fprintf(flag.CommandLine.Output(),
92 "unknown subcommand %q\n", name)
93 usage()
94 os.Exit(exitUsage)
95 }
96
97 cmd.Flag.Init(cmd.Name(), flag.ExitOnError)
98 cmd.Flag.Usage = cmd.Usage
99 args := flag.Args()
100 if err := cmd.Flag.Parse(args[1:]); err != nil {
101 fmt.Fprintln(flag.CommandLine.Output(), err)
102 os.Exit(exitFailure)
103 }
104
105 if err := cmd.Run(cmd, cmd.Flag.Args()); err != nil {
106 fmt.Fprintln(flag.CommandLine.Output(), err)
107
108 var uerr usageError
109 if errors.As(err, &uerr) {
110 cmd.Usage()
111 os.Exit(exitUsage)
112 }
113
114 os.Exit(exitFailure)
115 }
116 }