Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display service status and port maps in package info #81

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkgmgr/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
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