diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd/sievemgr/main.go	Thu Oct 15 09:11:05 2020 +0200
@@ -0,0 +1,115 @@
+// Copyright (C) 2020 Guido Berhoerster <guido+managesieve@berhoerster.name>
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+package main
+
+import (
+	"errors"
+	"flag"
+	"fmt"
+	"os"
+)
+
+const (
+	exitSuccess = iota
+	exitFailure
+	exitUsage
+)
+
+type usageError string
+
+func (e usageError) Error() string {
+	return string(e)
+}
+
+var (
+	skipCertVerify   bool
+	username         string
+	passwordFilename string
+)
+
+var cmds = []*command{
+	cmdList,
+	cmdPut,
+	cmdGet,
+	cmdActivate,
+	cmdDeactivate,
+	cmdDelete,
+}
+
+func usage() {
+	fmt.Fprintf(flag.CommandLine.Output(),
+		"usage:\n  %s [options] [subcommand [options] [arguments]]\n",
+		flag.CommandLine.Name())
+	fmt.Fprintln(flag.CommandLine.Output(), "subcommands:")
+	for _, cmd := range cmds {
+		fmt.Fprintf(flag.CommandLine.Output(), "  %s\n", cmd.Name())
+	}
+	fmt.Fprintln(flag.CommandLine.Output(), "global options:")
+	flag.PrintDefaults()
+}
+
+func main() {
+	flag.Usage = usage
+	flag.BoolVar(&skipCertVerify, "I", false,
+		"Skip TLS certificate verification")
+	flag.Parse()
+	if flag.NArg() == 0 {
+		fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand")
+		usage()
+		os.Exit(exitUsage)
+	}
+
+	name := flag.Arg(0)
+	var cmd *command
+	for _, c := range cmds {
+		if c.Name() == name {
+			cmd = c
+			break
+		}
+	}
+	if cmd == nil {
+		fmt.Fprintf(flag.CommandLine.Output(),
+			"unknown subcommand %q\n", name)
+		usage()
+		os.Exit(exitUsage)
+	}
+
+	cmd.Flag.Init(cmd.Name(), flag.ExitOnError)
+	cmd.Flag.Usage = cmd.Usage
+	args := flag.Args()
+	if err := cmd.Flag.Parse(args[1:]); err != nil {
+		fmt.Fprintln(flag.CommandLine.Output(), err)
+		os.Exit(exitFailure)
+	}
+
+	if err := cmd.Run(cmd, cmd.Flag.Args()); err != nil {
+		fmt.Fprintln(flag.CommandLine.Output(), err)
+
+		var uerr usageError
+		if errors.As(err, &uerr) {
+			cmd.Usage()
+			os.Exit(exitUsage)
+		}
+
+		os.Exit(exitFailure)
+	}
+}