Newer
Older
httpcat / src / main.go
package main

import (
	"fmt"
	"os"

	"github.com/jessevdk/go-flags"
)

const version string = "2.0"

type GlobalOptions struct {
	Verbose     bool `short:"v" long:"verbose" description:"Print more details"`
	HeadersOnly bool `long:"headers" description:"Show headers only (default is everything)"`
	BodiesOnly  bool `long:"bodies" description:"Show bodies only (default is everything)"`
	NoColour    bool `long:"nocolour" description:"Don't use colours in the output"`
}

type VersionCommand struct {
}

func (c *VersionCommand) Execute(args []string) error {
	fmt.Println("httpcat v" + version)
	return nil
}

var (
	globalOptions = new(GlobalOptions)
)

func main() {

	parser := flags.NewParser(globalOptions, flags.Default)

	parser.AddCommand("server", "Start a mock HTTP server", "", &ServerCommand{})
	parser.AddCommand("client", "Send an HTTP request", "", &ClientCommand{})
	parser.AddCommand("proxy", "Start reverse HTTP logging proxy", "", &ProxyCommand{})
	parser.AddCommand("version", "Display version", "", &VersionCommand{})

	// _, err := parser.ParseArgs(args[:])
	_, err := parser.Parse()

	// is this really how you do error handling in Go?
	if err != nil {
		parseErr := err.(*flags.Error)

		if parseErr.Type.String() == flags.ErrCommandRequired.String() {
			os.Stderr.WriteString("\n")
			parser.WriteHelp(os.Stderr)
		}
	}
}