github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/cmd/ctr/app/main.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package app
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  
    23  	"github.com/containerd/containerd/cmd/ctr/commands/containers"
    24  	"github.com/containerd/containerd/cmd/ctr/commands/content"
    25  	"github.com/containerd/containerd/cmd/ctr/commands/events"
    26  	"github.com/containerd/containerd/cmd/ctr/commands/images"
    27  	"github.com/containerd/containerd/cmd/ctr/commands/install"
    28  	"github.com/containerd/containerd/cmd/ctr/commands/leases"
    29  	namespacesCmd "github.com/containerd/containerd/cmd/ctr/commands/namespaces"
    30  	ociCmd "github.com/containerd/containerd/cmd/ctr/commands/oci"
    31  	"github.com/containerd/containerd/cmd/ctr/commands/plugins"
    32  	"github.com/containerd/containerd/cmd/ctr/commands/pprof"
    33  	"github.com/containerd/containerd/cmd/ctr/commands/run"
    34  	"github.com/containerd/containerd/cmd/ctr/commands/snapshots"
    35  	"github.com/containerd/containerd/cmd/ctr/commands/tasks"
    36  	versionCmd "github.com/containerd/containerd/cmd/ctr/commands/version"
    37  	"github.com/containerd/containerd/defaults"
    38  	"github.com/containerd/containerd/namespaces"
    39  	"github.com/containerd/containerd/version"
    40  	"github.com/sirupsen/logrus"
    41  	"github.com/urfave/cli"
    42  	"google.golang.org/grpc/grpclog"
    43  )
    44  
    45  var extraCmds = []cli.Command{}
    46  
    47  func init() {
    48  	// Discard grpc logs so that they don't mess with our stdio
    49  	grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
    50  
    51  	cli.VersionPrinter = func(c *cli.Context) {
    52  		fmt.Println(c.App.Name, version.Package, c.App.Version)
    53  	}
    54  }
    55  
    56  // New returns a *cli.App instance.
    57  func New() *cli.App {
    58  	app := cli.NewApp()
    59  	app.Name = "ctr"
    60  	app.Version = version.Version
    61  	app.Description = `
    62  ctr is an unsupported debug and administrative client for interacting
    63  with the containerd daemon. Because it is unsupported, the commands,
    64  options, and operations are not guaranteed to be backward compatible or
    65  stable from release to release of the containerd project.`
    66  	app.Usage = `
    67          __
    68    _____/ /______
    69   / ___/ __/ ___/
    70  / /__/ /_/ /
    71  \___/\__/_/
    72  
    73  containerd CLI
    74  `
    75  	app.EnableBashCompletion = true
    76  	app.Flags = []cli.Flag{
    77  		cli.BoolFlag{
    78  			Name:  "debug",
    79  			Usage: "enable debug output in logs",
    80  		},
    81  		cli.StringFlag{
    82  			Name:   "address, a",
    83  			Usage:  "address for containerd's GRPC server",
    84  			Value:  defaults.DefaultAddress,
    85  			EnvVar: "CONTAINERD_ADDRESS",
    86  		},
    87  		cli.DurationFlag{
    88  			Name:  "timeout",
    89  			Usage: "total timeout for ctr commands",
    90  		},
    91  		cli.DurationFlag{
    92  			Name:  "connect-timeout",
    93  			Usage: "timeout for connecting to containerd",
    94  		},
    95  		cli.StringFlag{
    96  			Name:   "namespace, n",
    97  			Usage:  "namespace to use with commands",
    98  			Value:  namespaces.Default,
    99  			EnvVar: namespaces.NamespaceEnvVar,
   100  		},
   101  	}
   102  	app.Commands = append([]cli.Command{
   103  		plugins.Command,
   104  		versionCmd.Command,
   105  		containers.Command,
   106  		content.Command,
   107  		events.Command,
   108  		images.Command,
   109  		leases.Command,
   110  		namespacesCmd.Command,
   111  		pprof.Command,
   112  		run.Command,
   113  		snapshots.Command,
   114  		tasks.Command,
   115  		install.Command,
   116  		ociCmd.Command,
   117  	}, extraCmds...)
   118  	app.Before = func(context *cli.Context) error {
   119  		if context.GlobalBool("debug") {
   120  			logrus.SetLevel(logrus.DebugLevel)
   121  		}
   122  		return nil
   123  	}
   124  	return app
   125  }