Skip to content

Commit

Permalink
feat: display service status and port maps in package info (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney authored Mar 5, 2024
1 parent 3b252c5 commit cc512bf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
18 changes: 18 additions & 0 deletions pkgmgr/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,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"`
Expand Down
59 changes: 57 additions & 2 deletions pkgmgr/pkgmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit cc512bf

Please sign in to comment.