Skip to content

Commit

Permalink
Merge pull request #2274 from baude/cobraprep
Browse files Browse the repository at this point in the history
Migrate to cobra CLI
  • Loading branch information
openshift-merge-robot authored Feb 8, 2019
2 parents 962850c + 25a3923 commit afd4d5f
Show file tree
Hide file tree
Showing 149 changed files with 11,433 additions and 4,437 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ vendor: .install.vndr
$(GOPATH)/bin/vndr \
-whitelist "github.com/varlink/go" \
-whitelist "github.com/onsi/ginkgo" \
-whitelist "github.com/onsi/gomega"
-whitelist "github.com/onsi/gomega" \
-whitelist "gopkg.in/fsnotify.v1"

.PHONY: \
.gopathok \
Expand Down
67 changes: 33 additions & 34 deletions cmd/podman/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,56 @@ package main
import (
"os"

"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/libpod"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/spf13/cobra"
)

var (
attachFlags = []cli.Flag{
cli.StringFlag{
Name: "detach-keys",
Usage: "Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.",
},
cli.BoolFlag{
Name: "no-stdin",
Usage: "Do not attach STDIN. The default is false.",
},
cli.BoolTFlag{
Name: "sig-proxy",
Usage: "Proxy received signals to the process (default true)",
},
LatestFlag,
}
attachCommand cliconfig.AttachValues
attachDescription = "The podman attach command allows you to attach to a running container using the container's ID or name, either to view its ongoing output or to control it interactively."
attachCommand = cli.Command{
Name: "attach",
Usage: "Attach to a running container",
Description: attachDescription,
Flags: sortFlags(attachFlags),
Action: attachCmd,
ArgsUsage: "",
OnUsageError: usageErrorHandler,
_attachCommand = &cobra.Command{
Use: "attach",
Short: "Attach to a running container",
Long: attachDescription,
RunE: func(cmd *cobra.Command, args []string) error {
attachCommand.InputArgs = args
attachCommand.GlobalFlags = MainGlobalOpts
return attachCmd(&attachCommand)
},
Example: "",
}
)

func attachCmd(c *cli.Context) error {
args := c.Args()
func init() {
attachCommand.Command = _attachCommand
flags := attachCommand.Flags()
flags.StringVar(&attachCommand.DetachKeys, "detach-keys", "", "Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _")
flags.BoolVar(&attachCommand.NoStdin, "no-stdin", false, "Do not attach STDIN. The default is false")
flags.BoolVar(&attachCommand.SigProxy, "sig-proxy", true, "Proxy received signals to the process (default true)")

flags.BoolVarP(&attachCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")

rootCmd.AddCommand(attachCommand.Command)
}

func attachCmd(c *cliconfig.AttachValues) error {
args := c.InputArgs
var ctr *libpod.Container
if err := validateFlags(c, attachFlags); err != nil {
return err
}
if len(c.Args()) > 1 || (len(c.Args()) == 0 && !c.Bool("latest")) {

if len(c.InputArgs) > 1 || (len(c.InputArgs) == 0 && !c.Latest) {
return errors.Errorf("attach requires the name or id of one running container or the latest flag")
}

runtime, err := libpodruntime.GetRuntime(c)
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.Shutdown(false)

if c.Bool("latest") {
if c.Latest {
ctr, err = runtime.GetLatestContainer()
} else {
ctr, err = runtime.LookupContainer(args[0])
Expand All @@ -72,11 +71,11 @@ func attachCmd(c *cli.Context) error {
}

inputStream := os.Stdin
if c.Bool("no-stdin") {
if c.NoStdin {
inputStream = nil
}

if err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"), false); err != nil {
if err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.DetachKeys, c.SigProxy, false); err != nil {
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
}

Expand Down
147 changes: 87 additions & 60 deletions cmd/podman/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,58 @@ import (
"github.com/containers/buildah/imagebuildah"
buildahcli "github.com/containers/buildah/pkg/cli"
"github.com/containers/buildah/pkg/parse"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/pkg/rootless"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/spf13/cobra"
)

var (
layerFlags = []cli.Flag{
cli.BoolTFlag{
Name: "force-rm",
Usage: "Always remove intermediate containers after a build, even if the build is unsuccessful. (default true)",
},
cli.BoolTFlag{
Name: "layers",
Usage: "Cache intermediate layers during build. Use BUILDAH_LAYERS environment variable to override. ",
},
}
buildCommand cliconfig.BuildValues
buildDescription = "Builds an OCI or Docker image using instructions from one\n" +
"or more Dockerfiles and a specified build context directory."
buildCommand = cli.Command{
Name: "build",
Usage: "Build an image using instructions from Dockerfiles",
Description: buildDescription,
Flags: sortFlags(append(append(buildahcli.BudFlags, layerFlags...), buildahcli.FromAndBudFlags...)),
Action: buildCmd,
ArgsUsage: "CONTEXT-DIRECTORY | URL",
SkipArgReorder: true,
OnUsageError: usageErrorHandler,
layerValues buildahcli.LayerResults
budFlagsValues buildahcli.BudResults
fromAndBudValues buildahcli.FromAndBudResults
userNSValues buildahcli.UserNSResults
namespaceValues buildahcli.NameSpaceResults

_buildCommand = &cobra.Command{
Use: "build",
Short: "Build an image using instructions from Dockerfiles",
Long: buildDescription,
RunE: func(cmd *cobra.Command, args []string) error {
buildCommand.InputArgs = args
buildCommand.GlobalFlags = MainGlobalOpts
buildCommand.BudResults = &budFlagsValues
buildCommand.UserNSResults = &userNSValues
buildCommand.FromAndBudResults = &fromAndBudValues
buildCommand.LayerResults = &layerValues
buildCommand.NameSpaceResults = &namespaceValues
return buildCmd(&buildCommand)
},
Example: "CONTEXT-DIRECTORY | URL",
}
)

func init() {
buildCommand.Command = _buildCommand
flags := buildCommand.Flags()
flags.SetInterspersed(false)

flags.BoolVar(&layerValues.ForceRm, "force-rm", true, "Always remove intermediate containers after a build, even if the build is unsuccessful. (default true)")
flags.BoolVar(&layerValues.Layers, "layers", true, "Cache intermediate layers during build. Use BUILDAH_LAYERS environment variable to override")
budFlags := buildahcli.GetBudFlags(&budFlagsValues)
fromAndBugFlags := buildahcli.GetFromAndBudFlags(&fromAndBudValues, &userNSValues, &namespaceValues)

flags.AddFlagSet(&budFlags)
flags.AddFlagSet(&fromAndBugFlags)

rootCmd.AddCommand(buildCommand.Command)
}

func getDockerfiles(files []string) []string {
var dockerfiles []string
for _, f := range files {
Expand All @@ -54,30 +74,30 @@ func getDockerfiles(files []string) []string {
return dockerfiles
}

func buildCmd(c *cli.Context) error {
func buildCmd(c *cliconfig.BuildValues) error {
// The following was taken directly from containers/buildah/cmd/bud.go
// TODO Find a away to vendor more of this in rather than copy from bud

output := ""
tags := []string{}
if c.IsSet("tag") || c.IsSet("t") {
tags = c.StringSlice("tag")
if c.Flag("tag").Changed {
tags = c.Tag
if len(tags) > 0 {
output = tags[0]
tags = tags[1:]
}
}
pullPolicy := imagebuildah.PullNever
if c.BoolT("pull") {
if c.Pull {
pullPolicy = imagebuildah.PullIfMissing
}
if c.Bool("pull-always") {
if c.PullAlways {
pullPolicy = imagebuildah.PullAlways
}

args := make(map[string]string)
if c.IsSet("build-arg") {
for _, arg := range c.StringSlice("build-arg") {
if c.Flag("build-arg").Changed {
for _, arg := range c.BuildArg {
av := strings.SplitN(arg, "=", 2)
if len(av) > 1 {
args[av[0]] = av[1]
Expand All @@ -87,15 +107,15 @@ func buildCmd(c *cli.Context) error {
}
}

dockerfiles := getDockerfiles(c.StringSlice("file"))
format, err := getFormat(c)
dockerfiles := getDockerfiles(c.File)
format, err := getFormat(&c.PodmanCommand)
if err != nil {
return nil
}
contextDir := ""
cliArgs := c.Args()
cliArgs := c.InputArgs

layers := c.BoolT("layers") // layers for podman defaults to true
layers := c.Layers // layers for podman defaults to true
// Check to see if the BUILDAH_LAYERS environment variable is set and override command-line
if _, ok := os.LookupEnv("BUILDAH_LAYERS"); ok {
layers = buildahcli.UseLayers()
Expand Down Expand Up @@ -124,7 +144,7 @@ func buildCmd(c *cli.Context) error {
}
contextDir = absDir
}
cliArgs = cliArgs.Tail()
cliArgs = Tail(cliArgs)
} else {
// No context directory or URL was specified. Try to use the
// home of the first locally-available Dockerfile.
Expand Down Expand Up @@ -153,17 +173,14 @@ func buildCmd(c *cli.Context) error {
if len(dockerfiles) == 0 {
dockerfiles = append(dockerfiles, filepath.Join(contextDir, "Dockerfile"))
}
if err := parse.ValidateFlags(c, buildahcli.BudFlags); err != nil {
return err
}

runtimeFlags := []string{}
for _, arg := range c.StringSlice("runtime-flag") {
for _, arg := range c.RuntimeOpts {
runtimeFlags = append(runtimeFlags, "--"+arg)
}
// end from buildah

runtime, err := libpodruntime.GetRuntime(c)
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
Expand All @@ -173,10 +190,10 @@ func buildCmd(c *cli.Context) error {
stdout = os.Stdout
stderr = os.Stderr
reporter = os.Stderr
if c.IsSet("logfile") {
f, err := os.OpenFile(c.String("logfile"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if c.Flag("logfile").Changed {
f, err := os.OpenFile(c.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return errors.Errorf("error opening logfile %q: %v", c.String("logfile"), err)
return errors.Errorf("error opening logfile %q: %v", c.Logfile, err)
}
defer f.Close()
logrus.SetOutput(f)
Expand All @@ -185,36 +202,36 @@ func buildCmd(c *cli.Context) error {
reporter = f
}

systemContext, err := parse.SystemContextFromOptions(c)
systemContext, err := parse.SystemContextFromOptions(c.PodmanCommand.Command)
if err != nil {
return errors.Wrapf(err, "error building system context")
}
systemContext.AuthFilePath = getAuthFile(c.String("authfile"))
commonOpts, err := parse.CommonBuildOptions(c)
systemContext.AuthFilePath = getAuthFile(c.Authfile)
commonOpts, err := parse.CommonBuildOptions(c.PodmanCommand.Command)
if err != nil {
return err
}

namespaceOptions, networkPolicy, err := parse.NamespaceOptions(c)
namespaceOptions, networkPolicy, err := parse.NamespaceOptions(c.PodmanCommand.Command)
if err != nil {
return errors.Wrapf(err, "error parsing namespace-related options")
}
usernsOption, idmappingOptions, err := parse.IDMappingOptions(c)
usernsOption, idmappingOptions, err := parse.IDMappingOptions(c.PodmanCommand.Command)
if err != nil {
return errors.Wrapf(err, "error parsing ID mapping options")
}
namespaceOptions.AddOrReplace(usernsOption...)

ociruntime := runtime.GetOCIRuntimePath()
if c.IsSet("runtime") {
ociruntime = c.String("runtime")
if c.Flag("runtime").Changed {
ociruntime = c.Runtime
}
options := imagebuildah.BuildOptions{
ContextDirectory: contextDir,
PullPolicy: pullPolicy,
Compression: imagebuildah.Gzip,
Quiet: c.Bool("quiet"),
SignaturePolicyPath: c.String("signature-policy"),
Quiet: c.Quiet,
SignaturePolicyPath: c.SignaturePolicy,
Args: args,
Output: output,
AdditionalTags: tags,
Expand All @@ -227,22 +244,22 @@ func buildCmd(c *cli.Context) error {
SystemContext: systemContext,
NamespaceOptions: namespaceOptions,
ConfigureNetwork: networkPolicy,
CNIPluginPath: c.String("cni-plugin-path"),
CNIConfigDir: c.String("cni-config-dir"),
CNIPluginPath: c.CNIPlugInPath,
CNIConfigDir: c.CNIConfigDir,
IDMappingOptions: idmappingOptions,
CommonBuildOpts: commonOpts,
DefaultMountsFilePath: c.GlobalString("default-mounts-file"),
IIDFile: c.String("iidfile"),
Squash: c.Bool("squash"),
Labels: c.StringSlice("label"),
Annotations: c.StringSlice("annotation"),
DefaultMountsFilePath: c.GlobalFlags.DefaultMountsFile,
IIDFile: c.Iidfile,
Squash: c.Squash,
Labels: c.Label,
Annotations: c.Annotation,
Layers: layers,
NoCache: c.Bool("no-cache"),
RemoveIntermediateCtrs: c.BoolT("rm"),
ForceRmIntermediateCtrs: c.BoolT("force-rm"),
NoCache: c.NoCache,
RemoveIntermediateCtrs: c.Rm,
ForceRmIntermediateCtrs: c.ForceRm,
}

if c.Bool("quiet") {
if c.Quiet {
options.ReportWriter = ioutil.Discard
}

Expand All @@ -252,3 +269,13 @@ func buildCmd(c *cli.Context) error {

return runtime.Build(getContext(), options, dockerfiles...)
}

// Tail returns a string slice after the first element unless there are
// not enough elements, then it returns an empty slice. This is to replace
// the urfavecli Tail method for args
func Tail(a []string) []string {
if len(a) >= 2 {
return []string(a)[1:]
}
return []string{}
}
Loading

0 comments on commit afd4d5f

Please sign in to comment.