view 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
line wrap: on
line source

// 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,
	cmdInfo,
}

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)
	}
}