From c4c94bfec0556db39942ccfee4a3200452e82ddf Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Tue, 5 Mar 2024 08:51:50 -0600 Subject: [PATCH] feat: display service status and port maps in package info --- pkgmgr/package.go | 18 +++++++++++++++ pkgmgr/pkgmgr.go | 59 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/pkgmgr/package.go b/pkgmgr/package.go index 05d8616..cb11c03 100644 --- a/pkgmgr/package.go +++ b/pkgmgr/package.go @@ -171,6 +171,24 @@ func (p Package) stopService(cfg Config, context string) error { return nil } +func (p Package) services(cfg Config, context string) ([]*DockerService, error) { + var ret []*DockerService + pkgName := fmt.Sprintf("%s-%s-%s", p.Name, p.Version, context) + for _, step := range p.InstallSteps { + if step.Docker != nil { + dockerService, err := NewDockerServiceFromContainerName(fmt.Sprintf("%s-%s", pkgName, step.Docker.ContainerName), cfg.Logger) + if err != nil { + cfg.Logger.Error( + fmt.Sprintf("error initializing Docker service for container %s: %v", dockerService.ContainerName, err), + ) + continue + } + ret = append(ret, dockerService) + } + } + return ret, nil +} + type PackageInstallStep struct { Docker *PackageInstallStepDocker `yaml:"docker,omitempty"` File *PackageInstallStepFile `yaml:"file,omitempty"` diff --git a/pkgmgr/pkgmgr.go b/pkgmgr/pkgmgr.go index b5eceae..c6608c7 100644 --- a/pkgmgr/pkgmgr.go +++ b/pkgmgr/pkgmgr.go @@ -341,8 +341,63 @@ func (p *PackageManager) Info(pkgs ...string) error { infoPkg.PostInstallNotes, ) } - // TODO: list services - // TODO: list container ports + // Gather package services + services, err := infoPkg.Package.services(p.config, infoPkg.Context) + if err != nil { + return err + } + // Build service status and port output + var statusOutput string + var portOutput string + for _, svc := range services { + running, err := svc.Running() + if err != nil { + return err + } + if running { + statusOutput += fmt.Sprintf( + "%-60s RUNNING\n", + svc.ContainerName, + ) + } else { + statusOutput += fmt.Sprintf( + "%-60s NOT RUNNING\n", + svc.ContainerName, + ) + } + for _, port := range svc.Ports { + var containerPort, hostPort string + portParts := strings.Split(port, ":") + switch len(portParts) { + case 1: + containerPort = portParts[0] + hostPort = portParts[0] + case 2: + containerPort = portParts[1] + hostPort = portParts[0] + case 3: + containerPort = portParts[2] + hostPort = portParts[1] + } + portOutput += fmt.Sprintf( + "%-5s (host) => %-5s (container)\n", + hostPort, + containerPort, + ) + } + } + if statusOutput != "" { + infoOutput += fmt.Sprintf( + "\n\nServices:\n\n%s", + strings.TrimSuffix(statusOutput, "\n"), + ) + } + if portOutput != "" { + infoOutput += fmt.Sprintf( + "\n\nMapped ports:\n\n%s", + strings.TrimSuffix(portOutput, "\n"), + ) + } if idx < len(infoPkgs)-1 { infoOutput += "\n\n---\n\n" }