diff options
author | baude <bbaude@redhat.com> | 2019-01-31 13:20:04 -0600 |
---|---|---|
committer | baude <bbaude@redhat.com> | 2019-02-08 10:26:43 -0600 |
commit | 25a3923b61a5ca014318e6d957f68abd03947297 (patch) | |
tree | 2ccb4a0bd9bda70c1c258dcb1b8aca8961d9ad30 /cmd/podman/logs.go | |
parent | 962850c6e0dfcee926af31fc0ad24f1f6c26f8ac (diff) | |
download | podman-25a3923b61a5ca014318e6d957f68abd03947297.tar.gz podman-25a3923b61a5ca014318e6d957f68abd03947297.tar.bz2 podman-25a3923b61a5ca014318e6d957f68abd03947297.zip |
Migrate to cobra CLI
We intend to migrate to the cobra cli from urfave/cli because the
project is more well maintained. There are also some technical reasons
as well which extend into our remote client work.
Signed-off-by: baude <bbaude@redhat.com>
Diffstat (limited to 'cmd/podman/logs.go')
-rw-r--r-- | cmd/podman/logs.go | 91 |
1 files changed, 41 insertions, 50 deletions
diff --git a/cmd/podman/logs.go b/cmd/podman/logs.go index 75947c34e..83f900e63 100644 --- a/cmd/podman/logs.go +++ b/cmd/podman/logs.go @@ -4,91 +4,82 @@ import ( "os" "time" + "github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/libpodruntime" "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/logs" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "github.com/urfave/cli" + "github.com/spf13/cobra" ) var ( - logsFlags = []cli.Flag{ - cli.BoolFlag{ - Name: "details", - Usage: "Show extra details provided to the logs", - Hidden: true, - }, - cli.BoolFlag{ - Name: "follow, f", - Usage: "Follow log output. The default is false", - }, - cli.StringFlag{ - Name: "since", - Usage: "Show logs since TIMESTAMP", - }, - cli.Uint64Flag{ - Name: "tail", - Usage: "Output the specified number of LINES at the end of the logs. Defaults to 0, which prints all lines", - }, - cli.BoolFlag{ - Name: "timestamps, t", - Usage: "Output the timestamps in the log", - }, - LatestFlag, - } + logsCommand cliconfig.LogsValues logsDescription = "The podman logs command batch-retrieves whatever logs are present for a container at the time of execution. This does not guarantee execution" + "order when combined with podman run (i.e. your run may not have generated any logs at the time you execute podman logs" - logsCommand = cli.Command{ - Name: "logs", - Usage: "Fetch the logs of a container", - Description: logsDescription, - Flags: sortFlags(logsFlags), - Action: logsCmd, - ArgsUsage: "CONTAINER", - SkipArgReorder: true, - OnUsageError: usageErrorHandler, - UseShortOptionHandling: true, + _logsCommand = &cobra.Command{ + Use: "logs", + Short: "Fetch the logs of a container", + Long: logsDescription, + RunE: func(cmd *cobra.Command, args []string) error { + logsCommand.InputArgs = args + logsCommand.GlobalFlags = MainGlobalOpts + return logsCmd(&logsCommand) + }, + Example: "CONTAINER", } ) -func logsCmd(c *cli.Context) error { +func init() { + logsCommand.Command = _logsCommand + flags := logsCommand.Flags() + flags.BoolVar(&logsCommand.Details, "details", false, "Show extra details provided to the logs") + flags.BoolVarP(&logsCommand.Follow, "follow", "f", false, "Follow log output. The default is false") + flags.BoolVarP(&waitCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of") + flags.StringVar(&logsCommand.Since, "since", "", "Show logs since TIMESTAMP") + flags.Uint64Var(&logsCommand.Tail, "tail", 0, "Output the specified number of LINES at the end of the logs. Defaults to 0, which prints all lines") + flags.BoolVarP(&logsCommand.Timestamps, "timestamps", "t", false, "Output the timestamps in the log") + flags.MarkHidden("details") + + flags.SetInterspersed(false) + + rootCmd.AddCommand(logsCommand.Command) +} + +func logsCmd(c *cliconfig.LogsValues) error { var ctr *libpod.Container var err error - if err := validateFlags(c, logsFlags); err != nil { - return err - } - runtime, err := libpodruntime.GetRuntime(c) + runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand) if err != nil { return errors.Wrapf(err, "could not get runtime") } defer runtime.Shutdown(false) - args := c.Args() - if len(args) != 1 && !c.Bool("latest") { + args := c.InputArgs + if len(args) != 1 && !c.Latest { return errors.Errorf("'podman logs' requires exactly one container name/ID") } sinceTime := time.Time{} - if c.IsSet("since") { + if c.Flag("since").Changed { // parse time, error out if something is wrong - since, err := parseInputTime(c.String("since")) + since, err := parseInputTime(c.Since) if err != nil { - return errors.Wrapf(err, "could not parse time: %q", c.String("since")) + return errors.Wrapf(err, "could not parse time: %q", c.Since) } sinceTime = since } opts := &logs.LogOptions{ - Details: c.Bool("details"), - Follow: c.Bool("follow"), + Details: c.Details, + Follow: c.Follow, Since: sinceTime, - Tail: c.Uint64("tail"), - Timestamps: c.Bool("timestamps"), + Tail: c.Tail, + Timestamps: c.Timestamps, } - if c.Bool("latest") { + if c.Latest { ctr, err = runtime.GetLatestContainer() } else { ctr, err = runtime.LookupContainer(args[0]) |