From df6f4a36550afc9d78393b919433fe41165fbf08 Mon Sep 17 00:00:00 2001 From: Mike Brown Date: Thu, 2 Nov 2017 17:38:24 -0500 Subject: [PATCH] adds help for load command (#383) * adds help for load command Signed-off-by: Mike Brown * vendor restrom/dedent Signed-off-by: Mike Brown --- cmd/cri-containerd/cri_containerd.go | 35 +++++++++++++-- pkg/client/client.go | 4 +- vendor.conf | 3 +- vendor/github.com/renstrom/dedent/LICENSE | 21 +++++++++ vendor/github.com/renstrom/dedent/README.md | 50 +++++++++++++++++++++ vendor/github.com/renstrom/dedent/dedent.go | 49 ++++++++++++++++++++ 6 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 vendor/github.com/renstrom/dedent/LICENSE create mode 100644 vendor/github.com/renstrom/dedent/README.md create mode 100644 vendor/github.com/renstrom/dedent/dedent.go diff --git a/cmd/cri-containerd/cri_containerd.go b/cmd/cri-containerd/cri_containerd.go index d8068a919..8d4b7a4be 100644 --- a/cmd/cri-containerd/cri_containerd.go +++ b/cmd/cri-containerd/cri_containerd.go @@ -26,11 +26,13 @@ import ( "os/signal" "path/filepath" "runtime" + "strings" "syscall" "github.com/docker/docker/pkg/reexec" "github.com/golang/glog" "github.com/opencontainers/selinux/go-selinux" + "github.com/renstrom/dedent" "github.com/spf13/cobra" "golang.org/x/net/context" "k8s.io/kubernetes/pkg/util/interrupt" @@ -58,6 +60,30 @@ var cmd = &cobra.Command{ Long: desc, } +var ( + loadLong = dedent.Dedent(` + Help for "load TAR" command + + TAR - The path to a tar archive containing a container image. + + Requirement: + Containerd and cri-containerd daemons (grpc servers) must already be up and + running before the load command is used. + + Description: + Running as a client, cri-containerd implements the load command by sending the + load request to the already running cri-containerd daemon/server, which in + turn loads the image into containerd's image storage via the already running + containerd daemon.`) + + loadExample = dedent.Dedent(` + - use docker to pull the latest busybox image and save it as a tar archive: + $ docker pull busybox:latest + $ docker save busybox:latest -o busybox.tar + - use cri-containerd to load the image into containerd's image storage: + $ cri-containerd load busybox.tar`) +) + // Add golang flags as persistent flags. func init() { cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) @@ -85,10 +111,13 @@ func versionCommand() *cobra.Command { func loadImageCommand() *cobra.Command { c := &cobra.Command{ - Use: "load TAR", - Short: "Load an image from a tar archive.", - Args: cobra.ExactArgs(1), + Use: "load TAR", + Long: loadLong, + Short: "Load an image from a tar archive.", + Args: cobra.ExactArgs(1), + Example: loadExample, } + c.SetUsageTemplate(strings.Replace(c.UsageTemplate(), "Examples:", "Example:", 1)) endpoint, timeout := options.AddGRPCFlags(c.Flags()) c.RunE = func(cmd *cobra.Command, args []string) error { cl, err := client.NewCRIContainerdClient(*endpoint, *timeout) diff --git a/pkg/client/client.go b/pkg/client/client.go index 36459ff86..a7d1cbed5 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -29,14 +29,14 @@ import ( // NewCRIContainerdClient creates grpc client of cri-containerd // TODO(random-liu): Wrap grpc functions. func NewCRIContainerdClient(endpoint string, timeout time.Duration) (api.CRIContainerdServiceClient, error) { - addr, dailer, err := util.GetAddressAndDialer(endpoint) + addr, dialer, err := util.GetAddressAndDialer(endpoint) if err != nil { return nil, fmt.Errorf("failed to get dialer: %v", err) } conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithTimeout(timeout), - grpc.WithDialer(dailer), + grpc.WithDialer(dialer), ) if err != nil { return nil, fmt.Errorf("failed to dial: %v", err) diff --git a/vendor.conf b/vendor.conf index caa28999a..d4bf95e73 100644 --- a/vendor.conf +++ b/vendor.conf @@ -47,7 +47,8 @@ github.com/pkg/errors v0.8.0 github.com/pmezard/go-difflib v1.0.0 github.com/PuerkitoBio/purell v1.0.0 github.com/PuerkitoBio/urlesc 5bd2802263f21d8788851d5305584c82a5c75d7e -github.com/seccomp/libseccomp-golang 32f571b70023028bd57d9288c20efbcb237f3ce0 +github.com/renstrom/dedent 020d11c3b9c0c7a3c2efcc8e5cf5b9ef7bcea21f +github.com/seccomp/libseccomp-golang 32f571b70023028bd57d9288c20efbcb237f3ce0 github.com/sirupsen/logrus v1.0.0 github.com/spf13/cobra v0.0.1 github.com/spf13/pflag v1.0.0 diff --git a/vendor/github.com/renstrom/dedent/LICENSE b/vendor/github.com/renstrom/dedent/LICENSE new file mode 100644 index 000000000..66a9870fc --- /dev/null +++ b/vendor/github.com/renstrom/dedent/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Peter Renström + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/github.com/renstrom/dedent/README.md b/vendor/github.com/renstrom/dedent/README.md new file mode 100644 index 000000000..35b5aa134 --- /dev/null +++ b/vendor/github.com/renstrom/dedent/README.md @@ -0,0 +1,50 @@ +# Dedent + +[![Build Status](https://travis-ci.org/renstrom/dedent.svg?branch=master)](https://travis-ci.org/renstrom/dedent) +[![Godoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/renstrom/dedent) + +Removes common leading whitespace from multiline strings. Inspired by [`textwrap.dedent`](https://docs.python.org/3/library/textwrap.html#textwrap.dedent) in Python. + +## Usage / example + +Imagine the following snippet that prints a multiline string. You want the indentation to both look nice in the code as well as in the actual output. + +```go +package main + +import ( + "fmt" + + "github.com/renstrom/dedent" +) + +func main() { + s := `Lorem ipsum dolor sit amet, + consectetur adipiscing elit. + Curabitur justo tellus, facilisis nec efficitur dictum, + fermentum vitae ligula. Sed eu convallis sapien.` + fmt.Println(dedent.Dedent(s)) + fmt.Println("-------------") + fmt.Println(s) +} +``` + +To illustrate the difference, here's the output: + + +```bash +$ go run main.go +Lorem ipsum dolor sit amet, +consectetur adipiscing elit. +Curabitur justo tellus, facilisis nec efficitur dictum, +fermentum vitae ligula. Sed eu convallis sapien. +------------- +Lorem ipsum dolor sit amet, + consectetur adipiscing elit. + Curabitur justo tellus, facilisis nec efficitur dictum, + fermentum vitae ligula. Sed eu convallis sapien. +``` + +## License + +MIT diff --git a/vendor/github.com/renstrom/dedent/dedent.go b/vendor/github.com/renstrom/dedent/dedent.go new file mode 100644 index 000000000..9d5bfbabd --- /dev/null +++ b/vendor/github.com/renstrom/dedent/dedent.go @@ -0,0 +1,49 @@ +package dedent + +import ( + "regexp" + "strings" +) + +var ( + whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$") + leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])") +) + +// Dedent removes any common leading whitespace from every line in text. +// +// This can be used to make multiline strings to line up with the left edge of +// the display, while still presenting them in the source code in indented +// form. +func Dedent(text string) string { + var margin string + + text = whitespaceOnly.ReplaceAllString(text, "") + indents := leadingWhitespace.FindAllStringSubmatch(text, -1) + + // Look for the longest leading string of spaces and tabs common to all + // lines. + for i, indent := range indents { + if i == 0 { + margin = indent[1] + } else if strings.HasPrefix(indent[1], margin) { + // Current line more deeply indented than previous winner: + // no change (previous winner is still on top). + continue + } else if strings.HasPrefix(margin, indent[1]) { + // Current line consistent with and no deeper than previous winner: + // it's the new winner. + margin = indent[1] + } else { + // Current line and previous winner have no common whitespace: + // there is no margin. + margin = "" + break + } + } + + if margin != "" { + text = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(text, "") + } + return text +}