|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package main denotes the entry point of finch CLI. |
| 5 | +// TODO: Remove all instances of these calls once supported upstream |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" |
| 15 | + "github.com/docker/go-connections/nat" |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | + |
| 18 | + "github.com/runfinch/finch/pkg/command" |
| 19 | +) |
| 20 | + |
| 21 | +// Config is from https://github.com/moby/moby/blob/8dbd90ec00daa26dc45d7da2431c965dec99e8b4/api/types/container/config.go#L37-L69 |
| 22 | +type Config struct { |
| 23 | + Hostname string `json:",omitempty"` // Hostname |
| 24 | + User string `json:",omitempty"` // User that will run the command(s) inside the container, also support user:group |
| 25 | + AttachStdin bool // Attach the standard input, makes possible user interaction |
| 26 | + ExposedPorts nat.PortSet `json:",omitempty"` // List of exposed ports |
| 27 | + Env []string `json:",omitempty"` // List of environment variable to set in the container |
| 28 | + Cmd []string `json:",omitempty"` // Command to run when starting the container |
| 29 | + Volumes map[string]struct{} `json:",omitempty"` // List of volumes (mounts) used for the container |
| 30 | + WorkingDir string `json:",omitempty"` // Current directory (PWD) in the command will be launched |
| 31 | + Entrypoint []string `json:",omitempty"` // Entrypoint to run when starting the container |
| 32 | + Labels map[string]string `json:",omitempty"` // List of labels set to this containerT |
| 33 | + Image string `json:",omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +// Container mimics a `docker container inspect` object. |
| 37 | +// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L340-L374 |
| 38 | +type Container struct { |
| 39 | + ID string `json:"Id"` |
| 40 | + Created string |
| 41 | + Path string |
| 42 | + Args []string |
| 43 | + State *dockercompat.ContainerState |
| 44 | + Image string |
| 45 | + ResolvConfPath string |
| 46 | + HostnamePath string |
| 47 | + LogPath string |
| 48 | + Name string |
| 49 | + RestartCount int |
| 50 | + Driver string |
| 51 | + Platform string |
| 52 | + AppArmorProfile string |
| 53 | + SizeRw *int64 `json:",omitempty"` |
| 54 | + SizeRootFs *int64 `json:",omitempty"` |
| 55 | + Mounts []dockercompat.MountPoint |
| 56 | + Config *Config |
| 57 | + NetworkSettings *dockercompat.NetworkSettings |
| 58 | +} |
| 59 | + |
| 60 | +func prettyPrintJSON(input string) { |
| 61 | + var mergedData []Container |
| 62 | + jsonObjects := strings.Split(input, "\n") |
| 63 | + |
| 64 | + for i, jsonStr := range jsonObjects { |
| 65 | + if len(jsonStr) == 0 { |
| 66 | + continue |
| 67 | + } |
| 68 | + var container Container |
| 69 | + err := json.Unmarshal([]byte(jsonStr), &container) |
| 70 | + if err != nil { |
| 71 | + logrus.Error("Error parsing JSON at index: ", i, err) |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + if container.Config != nil { |
| 76 | + container.Config.Image = container.Image |
| 77 | + } |
| 78 | + |
| 79 | + if container.State != nil { |
| 80 | + container.State.StartedAt = "0001-01-01T00:00:00Z" |
| 81 | + } |
| 82 | + |
| 83 | + if container.NetworkSettings == nil { |
| 84 | + container.NetworkSettings = &dockercompat.NetworkSettings{ |
| 85 | + Ports: &nat.PortMap{}, |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + mergedData = append(mergedData, container) |
| 90 | + } |
| 91 | + |
| 92 | + finalJSON, err := json.MarshalIndent(mergedData, "", " ") |
| 93 | + if err != nil { |
| 94 | + logrus.Error("Error marshaling final JSON: ", err) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + fmt.Println(string(finalJSON)) |
| 99 | +} |
| 100 | + |
| 101 | +func inspectContainerOutputHandler(cmd command.Command) error { |
| 102 | + var stdoutBuf bytes.Buffer |
| 103 | + cmd.SetStdout(&stdoutBuf) |
| 104 | + err := cmd.Run() |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + prettyPrintJSON(stdoutBuf.String()) |
| 109 | + return err |
| 110 | +} |
0 commit comments