github.com/demonoid81/containerd@v1.3.4/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  	"github.com/containerd/containerd/cmd/ctr/commands/plugins"
    31  	"github.com/containerd/containerd/cmd/ctr/commands/pprof"
    32  	"github.com/containerd/containerd/cmd/ctr/commands/run"
    33  	"github.com/containerd/containerd/cmd/ctr/commands/snapshots"
    34  	"github.com/containerd/containerd/cmd/ctr/commands/tasks"
    35  	versionCmd "github.com/containerd/containerd/cmd/ctr/commands/version"
    36  	"github.com/containerd/containerd/defaults"
    37  	"github.com/containerd/containerd/namespaces"
    38  	"github.com/containerd/containerd/version"
    39  	"github.com/sirupsen/logrus"
    40  	"github.com/urfave/cli"
    41  	"google.golang.org/grpc/grpclog"
    42  )
    43  
    44  var extraCmds = []cli.Command{}
    45  
    46  func init() {
    47  	// Discard grpc logs so that they don't mess with our stdio
    48  	grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
    49  
    50  	cli.VersionPrinter = func(c *cli.Context) {
    51  		fmt.Println(c.App.Name, version.Package, c.App.Version)
    52  	}
    53  }
    54  
    55  // New returns a *cli.App instance.
    56  func New() *cli.App {
    57  	app := cli.NewApp()
    58  	app.Name = "ctr"
    59  	app.Version = version.Version
    60  	app.Description = `
    61  ctr is an unsupported debug and administrative client for interacting
    62  with the containerd daemon. Because it is unsupported, the commands,
    63  options, and operations are not guaranteed to be backward compatible or
    64  stable from release to release of the containerd project.`
    65  	app.Usage = `
    66          __
    67    _____/ /______
    68   / ___/ __/ ___/
    69  / /__/ /_/ /
    70  \___/\__/_/
    71  
    72  containerd CLI
    73  `
    74  	app.Flags = []cli.Flag{
    75  		cli.BoolFlag{
    76  			Name:  "debug",
    77  			Usage: "enable debug output in logs",
    78  		},
    79  		cli.StringFlag{
    80  			Name:  "address, a",
    81  			Usage: "address for containerd's GRPC server",
    82  			Value: defaults.DefaultAddress,
    83  		},
    84  		cli.DurationFlag{
    85  			Name:  "timeout",
    86  			Usage: "total timeout for ctr commands",
    87  		},
    88  		cli.DurationFlag{
    89  			Name:  "connect-timeout",
    90  			Usage: "timeout for connecting to containerd",
    91  		},
    92  		cli.StringFlag{
    93  			Name:   "namespace, n",
    94  			Usage:  "namespace to use with commands",
    95  			Value:  namespaces.Default,
    96  			EnvVar: namespaces.NamespaceEnvVar,
    97  		},
    98  	}
    99  	app.Commands = append([]cli.Command{
   100  		plugins.Command,
   101  		versionCmd.Command,
   102  		containers.Command,
   103  		content.Command,
   104  		events.Command,
   105  		images.Command,
   106  		leases.Command,
   107  		namespacesCmd.Command,
   108  		pprof.Command,
   109  		run.Command,
   110  		snapshots.Command,
   111  		tasks.Command,
   112  		install.Command,
   113  	}, extraCmds...)
   114  	app.Before = func(context *cli.Context) error {
   115  		if context.GlobalBool("debug") {
   116  			logrus.SetLevel(logrus.DebugLevel)
   117  		}
   118  		return nil
   119  	}
   120  	return app
   121  }