diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podmanV2/containers/create.go | 1 | ||||
-rw-r--r-- | cmd/podmanV2/containers/logs.go | 108 | ||||
-rw-r--r-- | cmd/podmanV2/containers/run.go | 4 | ||||
-rw-r--r-- | cmd/podmanV2/images/list.go | 14 |
4 files changed, 123 insertions, 4 deletions
diff --git a/cmd/podmanV2/containers/create.go b/cmd/podmanV2/containers/create.go index fd5300966..63daf1702 100644 --- a/cmd/podmanV2/containers/create.go +++ b/cmd/podmanV2/containers/create.go @@ -40,6 +40,7 @@ func init() { }) //common.GetCreateFlags(createCommand) flags := createCommand.Flags() + flags.SetInterspersed(false) flags.AddFlagSet(common.GetCreateFlags(&cliVals)) flags.AddFlagSet(common.GetNetFlags()) flags.SetNormalizeFunc(common.AliasFlags) diff --git a/cmd/podmanV2/containers/logs.go b/cmd/podmanV2/containers/logs.go new file mode 100644 index 000000000..d1a179495 --- /dev/null +++ b/cmd/podmanV2/containers/logs.go @@ -0,0 +1,108 @@ +package containers + +import ( + "os" + + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/containers/libpod/pkg/util" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +// logsOptionsWrapper wraps entities.LogsOptions and prevents leaking +// CLI-only fields into the API types. +type logsOptionsWrapper struct { + entities.ContainerLogsOptions + + SinceRaw string +} + +var ( + logsOptions logsOptionsWrapper + logsDescription = `Retrieves logs for one or more containers. + + 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 = &cobra.Command{ + Use: "logs [flags] CONTAINER [CONTAINER...]", + Short: "Fetch the logs of one or more container", + Long: logsDescription, + RunE: logs, + PreRunE: preRunE, + Example: `podman logs ctrID + podman logs --names ctrID1 ctrID2 + podman logs --tail 2 mywebserver + podman logs --follow=true --since 10m ctrID + podman logs mywebserver mydbserver`, + } + + containerLogsCommand = &cobra.Command{ + Use: logsCommand.Use, + Short: logsCommand.Short, + Long: logsCommand.Long, + PreRunE: logsCommand.PreRunE, + RunE: logsCommand.RunE, + Example: `podman container logs ctrID + podman container logs --names ctrID1 ctrID2 + podman container logs --tail 2 mywebserver + podman container logs --follow=true --since 10m ctrID + podman container logs mywebserver mydbserver`, + } +) + +func init() { + // logs + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: logsCommand, + }) + + logsCommand.SetHelpTemplate(registry.HelpTemplate()) + logsCommand.SetUsageTemplate(registry.UsageTemplate()) + + flags := logsCommand.Flags() + logsFlags(flags) + + // container logs + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: containerLogsCommand, + Parent: containerCmd, + }) + + containerLogsFlags := containerLogsCommand.Flags() + logsFlags(containerLogsFlags) +} + +func logsFlags(flags *pflag.FlagSet) { + flags.BoolVar(&logsOptions.Details, "details", false, "Show extra details provided to the logs") + flags.BoolVarP(&logsOptions.Follow, "follow", "f", false, "Follow log output. The default is false") + flags.BoolVarP(&logsOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") + flags.StringVar(&logsOptions.SinceRaw, "since", "", "Show logs since TIMESTAMP") + flags.Int64Var(&logsOptions.Tail, "tail", -1, "Output the specified number of LINES at the end of the logs. Defaults to -1, which prints all lines") + flags.BoolVarP(&logsOptions.Timestamps, "timestamps", "t", false, "Output the timestamps in the log") + flags.BoolVarP(&logsOptions.Names, "names", "n", false, "Output the container name in the log") + flags.SetInterspersed(false) + _ = flags.MarkHidden("details") +} + +func logs(cmd *cobra.Command, args []string) error { + if len(args) > 0 && logsOptions.Latest { + return errors.New("no containers can be specified when using 'latest'") + } + if !logsOptions.Latest && len(args) < 1 { + return errors.New("specify at least one container name or ID to log") + } + if logsOptions.SinceRaw != "" { + // parse time, error out if something is wrong + since, err := util.ParseInputTime(logsOptions.SinceRaw) + if err != nil { + return errors.Wrapf(err, "error parsing --since %q", logsOptions.SinceRaw) + } + logsOptions.Since = since + } + logsOptions.Writer = os.Stdout + return registry.ContainerEngine().ContainerLogs(registry.GetContext(), args, logsOptions.ContainerLogsOptions) +} diff --git a/cmd/podmanV2/containers/run.go b/cmd/podmanV2/containers/run.go index bd90aee2f..f839c358d 100644 --- a/cmd/podmanV2/containers/run.go +++ b/cmd/podmanV2/containers/run.go @@ -46,6 +46,7 @@ func init() { Command: runCommand, }) flags := runCommand.Flags() + flags.SetInterspersed(false) flags.AddFlagSet(common.GetCreateFlags(&cliVals)) flags.AddFlagSet(common.GetNetFlags()) flags.SetNormalizeFunc(common.AliasFlags) @@ -108,13 +109,14 @@ func run(cmd *cobra.Command, args []string) error { } runOpts.Spec = s report, err := registry.ContainerEngine().ContainerRun(registry.GetContext(), runOpts) + // report.ExitCode is set by ContainerRun even it it returns an error + registry.SetExitCode(report.ExitCode) if err != nil { return err } if cliVals.Detach { fmt.Println(report.Id) } - registry.SetExitCode(report.ExitCode) if runRmi { _, err := registry.ImageEngine().Delete(registry.GetContext(), []string{report.Id}, entities.ImageDeleteOptions{}) if err != nil { diff --git a/cmd/podmanV2/images/list.go b/cmd/podmanV2/images/list.go index 2d6cb3596..6b02e239e 100644 --- a/cmd/podmanV2/images/list.go +++ b/cmd/podmanV2/images/list.go @@ -130,6 +130,9 @@ func writeJSON(imageS []*entities.ImageSummary) error { } func writeTemplate(imageS []*entities.ImageSummary, err error) error { + var ( + hdr, row string + ) type image struct { entities.ImageSummary Repository string `json:"repository,omitempty"` @@ -148,10 +151,15 @@ func writeTemplate(imageS []*entities.ImageSummary, err error) error { listFlag.readOnly = true } } - - hdr, row := imageListFormat(listFlag) + if len(listFlag.format) < 1 { + hdr, row = imageListFormat(listFlag) + } else { + row = listFlag.format + if !strings.HasSuffix(row, "\n") { + row += "\n" + } + } format := hdr + "{{range . }}" + row + "{{end}}" - tmpl := template.Must(template.New("list").Funcs(report.PodmanTemplateFuncs()).Parse(format)) w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() |