comparison cmd/sievemgr/main.go @ 0:6369453d47a3

Initial revision
author Guido Berhoerster <guido+managesieve@berhoerster.name>
date Thu, 15 Oct 2020 09:11:05 +0200
parents
children eec31eb2d21a
comparison
equal deleted inserted replaced
-1:000000000000 0:6369453d47a3
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 }
57
58 func usage() {
59 fmt.Fprintf(flag.CommandLine.Output(),
60 "usage:\n %s [options] [subcommand [options] [arguments]]\n",
61 flag.CommandLine.Name())
62 fmt.Fprintln(flag.CommandLine.Output(), "subcommands:")
63 for _, cmd := range cmds {
64 fmt.Fprintf(flag.CommandLine.Output(), " %s\n", cmd.Name())
65 }
66 fmt.Fprintln(flag.CommandLine.Output(), "global options:")
67 flag.PrintDefaults()
68 }
69
70 func main() {
71 flag.Usage = usage
72 flag.BoolVar(&skipCertVerify, "I", false,
73 "Skip TLS certificate verification")
74 flag.Parse()
75 if flag.NArg() == 0 {
76 fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand")
77 usage()
78 os.Exit(exitUsage)
79 }
80
81 name := flag.Arg(0)
82 var cmd *command
83 for _, c := range cmds {
84 if c.Name() == name {
85 cmd = c
86 break
87 }
88 }
89 if cmd == nil {
90 fmt.Fprintf(flag.CommandLine.Output(),
91 "unknown subcommand %q\n", name)
92 usage()
93 os.Exit(exitUsage)
94 }
95
96 cmd.Flag.Init(cmd.Name(), flag.ExitOnError)
97 cmd.Flag.Usage = cmd.Usage
98 args := flag.Args()
99 if err := cmd.Flag.Parse(args[1:]); err != nil {
100 fmt.Fprintln(flag.CommandLine.Output(), err)
101 os.Exit(exitFailure)
102 }
103
104 if err := cmd.Run(cmd, cmd.Flag.Args()); err != nil {
105 fmt.Fprintln(flag.CommandLine.Output(), err)
106
107 var uerr usageError
108 if errors.As(err, &uerr) {
109 cmd.Usage()
110 os.Exit(exitUsage)
111 }
112
113 os.Exit(exitFailure)
114 }
115 }