|
| 1 | +/* |
| 2 | +Copyright © 2024 thin-edge.io <[email protected]> |
| 3 | +*/ |
| 4 | +package container_group_plugin |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "log/slog" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/thin-edge/tedge-container-plugin/pkg/cli" |
| 13 | + "github.com/thin-edge/tedge-container-plugin/pkg/container" |
| 14 | +) |
| 15 | + |
| 16 | +type ContainerLogsCommand struct { |
| 17 | + *cobra.Command |
| 18 | + |
| 19 | + CommandContext cli.Cli |
| 20 | + |
| 21 | + // Options |
| 22 | + Since string |
| 23 | + Until string |
| 24 | +} |
| 25 | + |
| 26 | +func NewGetCommand(ctx cli.Cli) *cobra.Command { |
| 27 | + command := &ContainerLogsCommand{ |
| 28 | + CommandContext: ctx, |
| 29 | + } |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "get", |
| 32 | + Short: "Get container-group logs", |
| 33 | + RunE: command.RunE, |
| 34 | + Args: cobra.ExactArgs(1), |
| 35 | + SilenceUsage: true, |
| 36 | + } |
| 37 | + |
| 38 | + 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)") |
| 39 | + 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)") |
| 40 | + command.Command = cmd |
| 41 | + return cmd |
| 42 | +} |
| 43 | + |
| 44 | +func (c *ContainerLogsCommand) RunE(cmd *cobra.Command, args []string) error { |
| 45 | + slog.Info("Executing", "cmd", cmd.CalledAs(), "args", args) |
| 46 | + |
| 47 | + containerCli, err := container.NewContainerClient(context.TODO(), c.CommandContext.GetContainerClientOptions()...) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + ctx := context.Background() |
| 53 | + |
| 54 | + // lookup |
| 55 | + projectName, serviceName, found := container.ParseContainerGroup(args[0]) |
| 56 | + if !found { |
| 57 | + return fmt.Errorf("invalid service name. expected name in format of '<project>@<service>'") |
| 58 | + } |
| 59 | + services, err := containerCli.LookupProject(context.Background(), projectName, serviceName) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + if len(services) == 0 { |
| 65 | + slog.Info("No matching services found") |
| 66 | + return nil |
| 67 | + } |
| 68 | + |
| 69 | + // containerCli.List(context.Background(), container.FilterOptions{}) |
| 70 | + |
| 71 | + // Write container logs (stdout and stderr) to stdout |
| 72 | + out := cmd.OutOrStdout() |
| 73 | + slog.Info("Fetching logs", "id", services[0].ID, "name", services[0].Names) |
| 74 | + err = containerCli.ContainerLogs(ctx, out, services[0].ID, container.LogsOptions{ |
| 75 | + ShowStdout: true, |
| 76 | + ShowStderr: true, |
| 77 | + Since: c.Since, |
| 78 | + Until: c.Until, |
| 79 | + Timestamps: false, |
| 80 | + Follow: false, |
| 81 | + Tail: "100000", |
| 82 | + Details: false, |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
0 commit comments