|
| 1 | +/* |
| 2 | +Copyright © 2024 thin-edge.io <[email protected]> |
| 3 | +*/ |
| 4 | +package tools |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "log/slog" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/thin-edge/tedge-container-plugin/pkg/cli" |
| 14 | + "github.com/thin-edge/tedge-container-plugin/pkg/container" |
| 15 | +) |
| 16 | + |
| 17 | +type ContainerLogsCommand struct { |
| 18 | + *cobra.Command |
| 19 | + |
| 20 | + CommandContext cli.Cli |
| 21 | + |
| 22 | + // Options |
| 23 | + Tail string |
| 24 | + Since string |
| 25 | + Until string |
| 26 | + Timestamps bool |
| 27 | + Follow bool |
| 28 | + Details bool |
| 29 | +} |
| 30 | + |
| 31 | +// NewContainerLogsCommand creates a new container logs command |
| 32 | +func NewContainerLogsCommand(ctx cli.Cli) *cobra.Command { |
| 33 | + command := &ContainerLogsCommand{ |
| 34 | + CommandContext: ctx, |
| 35 | + } |
| 36 | + cmd := &cobra.Command{ |
| 37 | + Use: "container-logs [OPTIONS] CONTAINER", |
| 38 | + Short: "Fetch the logs of a container", |
| 39 | + RunE: command.RunE, |
| 40 | + Args: cobra.ArbitraryArgs, |
| 41 | + SilenceUsage: true, |
| 42 | + } |
| 43 | + |
| 44 | + cmd.Flags().StringVar(&command.Since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)") |
| 45 | + cmd.Flags().StringVar(&command.Until, "until", "", "Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)") |
| 46 | + cmd.Flags().StringVarP(&command.Tail, "tail", "n", "all", "Number of lines to show from the end of the logs") |
| 47 | + cmd.Flags().BoolVarP(&command.Follow, "follow", "f", false, "Follow log output") |
| 48 | + cmd.Flags().BoolVarP(&command.Timestamps, "timestamps", "t", false, "Show timestamps") |
| 49 | + cmd.Flags().BoolVar(&command.Details, "details", false, "Show extra details provided to logs") |
| 50 | + command.Command = cmd |
| 51 | + return cmd |
| 52 | +} |
| 53 | + |
| 54 | +func (c *ContainerLogsCommand) RunE(cmd *cobra.Command, args []string) error { |
| 55 | + slog.Debug("Executing", "cmd", cmd.CalledAs(), "args", args) |
| 56 | + |
| 57 | + containerCli, err := container.NewContainerClient() |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + ctx := context.Background() |
| 63 | + |
| 64 | + if len(args) == 0 { |
| 65 | + slog.Info("Fetching logs for current container (if running inside a container)") |
| 66 | + con, err := containerCli.Self(ctx) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + args = append(args, con.ID) |
| 71 | + } |
| 72 | + |
| 73 | + // Write container logs (stdout and stderr) to stdout |
| 74 | + out := cmd.OutOrStdout() |
| 75 | + errs := make([]error, 0) |
| 76 | + for _, con := range args { |
| 77 | + idOrName := strings.TrimPrefix(con, "/") |
| 78 | + slog.Info("Fetching logs.", "id", idOrName) |
| 79 | + if err := containerCli.ContainerLogs(ctx, out, idOrName, container.LogsOptions{ |
| 80 | + ShowStdout: true, |
| 81 | + ShowStderr: true, |
| 82 | + Since: c.Since, |
| 83 | + Until: c.Until, |
| 84 | + Timestamps: c.Timestamps, |
| 85 | + Follow: c.Follow, |
| 86 | + Tail: c.Tail, |
| 87 | + Details: c.Details, |
| 88 | + }); err != nil { |
| 89 | + errs = append(errs, err) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return errors.Join(errs...) |
| 94 | +} |
0 commit comments