projects/sievemgr

view cmd/sievemgr/main.go @ 3:3cd182bff992

Show warning messages in put output
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue Oct 27 17:54:58 2020 +0100 (13 months ago)
parents b00673734e58
children f925f15d8ce5
line source
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.
22 package main
24 import (
25 "errors"
26 "flag"
27 "fmt"
28 "os"
29 )
31 const (
32 exitSuccess = iota
33 exitFailure
34 exitUsage
35 )
37 type usageError string
39 func (e usageError) Error() string {
40 return string(e)
41 }
43 var (
44 skipCertVerify bool
45 username string
46 passwordFilename string
47 )
49 var cmds = []*command{
50 cmdList,
51 cmdPut,
52 cmdGet,
53 cmdActivate,
54 cmdDeactivate,
55 cmdDelete,
56 cmdInfo,
57 }
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 }
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 }
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 }
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 }
105 if err := cmd.Run(cmd, cmd.Flag.Args()); err != nil {
106 fmt.Fprintln(flag.CommandLine.Output(), err)
108 var uerr usageError
109 if errors.As(err, &uerr) {
110 cmd.Usage()
111 os.Exit(exitUsage)
112 }
114 os.Exit(exitFailure)
115 }
116 }