view cmd/sievemgr/main.go @ 5:4dff4c3f0fbb

Introduce configuration file where account information is specified Introduce a configuration file where account information must be specified instead of passing it with each invocation on the command line. Each account has a name by which it can be selected and one may be specified as the default account. This is intended to improve usability for productive usage. Enforce strict permissions since passwords may be specified for non-interactive usage. Remove command-line flags for passing account information.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue, 03 Nov 2020 23:44:45 +0100
parents f925f15d8ce5
children 2130614cd64a
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.

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
)

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

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