projects/sievemgr

view cmd/sievemgr/main.go @ 4:f925f15d8ce5

Add check subcommand for checking the script validity
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue Oct 27 19:17:56 2020 +0100 (10 months ago)
parents 0cd5a454dfb4
children 4dff4c3f0fbb
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 cmdCheck,
58 }
60 func usage() {
61 fmt.Fprintf(flag.CommandLine.Output(),
62 "usage:\n %s [options] [subcommand [options] [arguments]]\n",
63 flag.CommandLine.Name())
64 fmt.Fprintln(flag.CommandLine.Output(), "subcommands:")
65 for _, cmd := range cmds {
66 fmt.Fprintf(flag.CommandLine.Output(), " %s\n", cmd.Name())
67 }
68 fmt.Fprintln(flag.CommandLine.Output(), "global options:")
69 flag.PrintDefaults()
70 }
72 func main() {
73 flag.Usage = usage
74 flag.BoolVar(&skipCertVerify, "I", false,
75 "Skip TLS certificate verification")
76 flag.Parse()
77 if flag.NArg() == 0 {
78 fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand")
79 usage()
80 os.Exit(exitUsage)
81 }
83 name := flag.Arg(0)
84 var cmd *command
85 for _, c := range cmds {
86 if c.Name() == name {
87 cmd = c
88 break
89 }
90 }
91 if cmd == nil {
92 fmt.Fprintf(flag.CommandLine.Output(),
93 "unknown subcommand %q\n", name)
94 usage()
95 os.Exit(exitUsage)
96 }
98 cmd.Flag.Init(cmd.Name(), flag.ExitOnError)
99 cmd.Flag.Usage = cmd.Usage
100 args := flag.Args()
101 if err := cmd.Flag.Parse(args[1:]); err != nil {
102 fmt.Fprintln(flag.CommandLine.Output(), err)
103 os.Exit(exitFailure)
104 }
106 if err := cmd.Run(cmd, cmd.Flag.Args()); err != nil {
107 fmt.Fprintln(flag.CommandLine.Output(), err)
109 var uerr usageError
110 if errors.As(err, &uerr) {
111 cmd.Usage()
112 os.Exit(exitUsage)
113 }
115 os.Exit(exitFailure)
116 }
117 }