main.go 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
package main // import "github.com/inclavare-containers/runectl"

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli"
	"io"
	"os"
	"strings"
)

// version will be populated by the Makefile, read from
// VERSION file of the source code.
var version = ""

// gitCommit will be the hash that the binary was built from
// and will be populated by the Makefile
var gitCommit = ""

const usage = `runectl command line tool
`

func main() {
	app := cli.NewApp()
	app.Name = "runectl"
	app.Usage = usage

	var ver []string
	if version != "" {
		ver = append(ver, version)
	}
	if gitCommit != "" {
		ver = append(ver, fmt.Sprintf("commit: %s", gitCommit))
	}
	app.Version = strings.Join(ver, "\n")

	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "verbose",
			Usage: "enable verbose output",
		},
	}

	app.Commands = []cli.Command{
		generateTokenCommand,
	}

	//app.Before = func(context *cli.Context) error {
	//        return logs.ConfigureLogging(createLogConfig(context))
	//}

	// If the command returns an error, cli takes upon itself to print
	// the error on cli.ErrWriter and exit.
	// Use our own writer here to ensure the log gets sent to the right location.
	cli.ErrWriter = &FatalWriter{cli.ErrWriter}
	if err := app.Run(os.Args); err != nil {
		fatal(err)
	}
}

type FatalWriter struct {
	cliErrWriter io.Writer
}

func (f *FatalWriter) Write(p []byte) (n int, err error) {
	logrus.Error(string(p))
	return f.cliErrWriter.Write(p)
}

// fatal prints the error's details if it is a libcontainer specific error type
// then exits the program with an exit status of 1.
func fatal(err error) {
	// make sure the error is written to the logger
	logrus.Error(err)
	fmt.Fprintln(os.Stderr, err)
	os.Exit(1)
}