github.com/georgethebeatle/containerd@v0.2.5/ctr/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  
     8  	netcontext "golang.org/x/net/context"
     9  
    10  	"github.com/Sirupsen/logrus"
    11  	"github.com/codegangsta/cli"
    12  	"github.com/docker/containerd"
    13  	"github.com/docker/containerd/api/grpc/types"
    14  )
    15  
    16  const usage = `High performance container daemon cli`
    17  
    18  type exit struct {
    19  	Code int
    20  }
    21  
    22  func main() {
    23  	// We want our defer functions to be run when calling fatal()
    24  	defer func() {
    25  		if e := recover(); e != nil {
    26  			if ex, ok := e.(exit); ok == true {
    27  				os.Exit(ex.Code)
    28  			}
    29  			panic(e)
    30  		}
    31  	}()
    32  	app := cli.NewApp()
    33  	app.Name = "ctr"
    34  	if containerd.GitCommit != "" {
    35  		app.Version = fmt.Sprintf("%s commit: %s", containerd.Version, containerd.GitCommit)
    36  	} else {
    37  		app.Version = containerd.Version
    38  	}
    39  	app.Usage = usage
    40  	app.Flags = []cli.Flag{
    41  		cli.BoolFlag{
    42  			Name:  "debug",
    43  			Usage: "enable debug output in the logs",
    44  		},
    45  		cli.StringFlag{
    46  			Name:  "address",
    47  			Value: "unix:///run/containerd/containerd.sock",
    48  			Usage: "proto://address of GRPC API",
    49  		},
    50  		cli.DurationFlag{
    51  			Name:  "conn-timeout",
    52  			Value: 1 * time.Second,
    53  			Usage: "GRPC connection timeout",
    54  		},
    55  	}
    56  	app.Commands = []cli.Command{
    57  		checkpointCommand,
    58  		containersCommand,
    59  		eventsCommand,
    60  		stateCommand,
    61  		versionCommand,
    62  	}
    63  	app.Before = func(context *cli.Context) error {
    64  		if context.GlobalBool("debug") {
    65  			logrus.SetLevel(logrus.DebugLevel)
    66  		}
    67  		return nil
    68  	}
    69  	if err := app.Run(os.Args); err != nil {
    70  		logrus.Fatal(err)
    71  	}
    72  }
    73  
    74  var versionCommand = cli.Command{
    75  	Name:  "version",
    76  	Usage: "return the daemon version",
    77  	Action: func(context *cli.Context) {
    78  		c := getClient(context)
    79  		resp, err := c.GetServerVersion(netcontext.Background(), &types.GetServerVersionRequest{})
    80  		if err != nil {
    81  			fatal(err.Error(), 1)
    82  		}
    83  		fmt.Printf("daemon version %d.%d.%d commit: %s\n", resp.Major, resp.Minor, resp.Patch, resp.Revision)
    84  	},
    85  }
    86  
    87  func fatal(err string, code int) {
    88  	fmt.Fprintf(os.Stderr, "[ctr] %s\n", err)
    89  	panic(exit{code})
    90  }