view cmd/sievemgr/main.go @ 22:fc5e6970a0d5 default tip

Add support for specifying an authorization identity on the command line
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Wed, 17 Feb 2021 07:50:55 +0100
parents 44c07eb8ef08
children
line wrap: on
line source

// Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@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.

//go:generate go run gendocstr.go . code.guido-berhoerster.org/go/sievemgr docText docstr.go

package main

import (
	"errors"
	"flag"
	"fmt"
	"os"

	"go.guido-berhoerster.org/sievemgr/cmd/sievemgr/internal/config"
)

const (
	exitSuccess = iota
	exitFailure
	exitUsage
)

type usageError string

func (e usageError) Error() string {
	return string(e)
}

// command-line flags
var (
	confFilename   string
	acctName       string
	authzID        string
)

var conf config.Configuration

func init() {
	var err error
	confFilename, err = config.DefaultFilename()
	if err != nil {
		fmt.Fprintln(flag.CommandLine.Output(), err)
		os.Exit(exitFailure)
	}
}

var cmds = []*command{
	cmdList,
	cmdPut,
	cmdGet,
	cmdActivate,
	cmdDeactivate,
	cmdDelete,
	cmdInfo,
	cmdCheck,
	cmdCheckSpace,
	cmdRename,
	cmdEdit,
	cmdMan,
}

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.StringVar(&confFilename, "f", confFilename,
		"Set the name of the configuration file")
	flag.Parse()
	if flag.NArg() == 0 {
		fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand")
		usage()
		os.Exit(exitUsage)
	}

	if err := config.ParseFile(confFilename, &conf); err != nil {
		fmt.Fprintln(flag.CommandLine.Output(), err)
		os.Exit(exitFailure)
	}

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