feat: add IP ADDRESS column to 'uc ps' and 'uc inspect'

This commit is contained in:
Pasha Sviderski
2025-12-12 20:23:43 +10:00
parent 95afc7f289
commit cb0df2169f
2 changed files with 21 additions and 3 deletions
+11 -1
View File
@@ -71,6 +71,7 @@ type containerInfo struct {
status string status string
highlight containerHighlight highlight containerHighlight
created time.Time created time.Time
ip string
} }
func runPs(ctx context.Context, uncli *cli.CLI, opts psOptions) error { func runPs(ctx context.Context, uncli *cli.CLI, opts psOptions) error {
@@ -142,7 +143,7 @@ func printContainers(containers []containerInfo) error {
return lipgloss.NewStyle().PaddingRight(3) return lipgloss.NewStyle().PaddingRight(3)
}) })
t.Headers("SERVICE", "CONTAINER ID", "CONTAINER NAME", "IMAGE", "CREATED", "STATUS", "MACHINE") t.Headers("SERVICE", "CONTAINER ID", "CONTAINER NAME", "IMAGE", "CREATED", "STATUS", "IP ADDRESS", "MACHINE")
for _, ctr := range containers { for _, ctr := range containers {
id := ctr.id id := ctr.id
@@ -171,6 +172,7 @@ func printContainers(containers []containerInfo) error {
ctr.image, ctr.image,
created, created,
statusStyle.Render(ctr.status), statusStyle.Render(ctr.status),
ctr.ip,
ctr.machineName, ctr.machineName,
) )
} }
@@ -257,6 +259,13 @@ func collectContainers(ctx context.Context, cli *client.Client) ([]containerInfo
created, _ := time.Parse(time.RFC3339Nano, ctr.Container.Created) created, _ := time.Parse(time.RFC3339Nano, ctr.Container.Created)
ip := ctr.Container.UncloudNetworkIP()
ipStr := ""
// The container might not have an IP if it's not running or uses the host network.
if ip.IsValid() {
ipStr = ip.String()
}
info := containerInfo{ info := containerInfo{
serviceName: ctr.ServiceName(), serviceName: ctr.ServiceName(),
machineName: machineName, machineName: machineName,
@@ -266,6 +275,7 @@ func collectContainers(ctx context.Context, cli *client.Client) ([]containerInfo
status: status, status: status,
highlight: highlight, highlight: highlight,
created: created, created: created,
ip: ipStr,
} }
containers = append(containers, info) containers = append(containers, info)
} }
+10 -2
View File
@@ -74,7 +74,7 @@ func inspect(ctx context.Context, uncli *cli.CLI, opts inspectOptions) error {
// Print the list of containers in a table format. // Print the list of containers in a table format.
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
if _, err = fmt.Fprintln(tw, "CONTAINER ID\tIMAGE\tCREATED\tSTATUS\tMACHINE"); err != nil { if _, err = fmt.Fprintln(tw, "CONTAINER ID\tIMAGE\tCREATED\tSTATUS\tIP ADDRESS\tMACHINE"); err != nil {
return fmt.Errorf("write header: %w", err) return fmt.Errorf("write header: %w", err)
} }
@@ -91,13 +91,21 @@ func inspect(ctx context.Context, uncli *cli.CLI, opts inspectOptions) error {
return fmt.Errorf("get human state: %w", err) return fmt.Errorf("get human state: %w", err)
} }
ip := ctr.Container.UncloudNetworkIP()
ipStr := ""
// The container might not have an IP if it's not running or uses the host network.
if ip.IsValid() {
ipStr = ip.String()
}
_, err = fmt.Fprintf( _, err = fmt.Fprintf(
tw, tw,
"%s\t%s\t%s\t%s\t%s\n", "%s\t%s\t%s\t%s\t%s\t%s\n",
stringid.TruncateID(ctr.Container.ID), stringid.TruncateID(ctr.Container.ID),
ctr.Container.Config.Image, ctr.Container.Config.Image,
created, created,
state, state,
ipStr,
machine, machine,
) )
if err != nil { if err != nil {