Extract ContainerNameFor and SplitContainerName into names.go. ContainerNameFor handles single-component services where service name equals component name (e.g., mc-proxy → "mc-proxy" not "mc-proxy-mc-proxy"). SplitContainerName checks known services from the registry before falling back to naive split on "-", fixing mc-proxy being misidentified as service "mc" component "proxy". Also fixes podman ps JSON parsing (Command field is []string not string) found during deployment. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package agent
|
|
|
|
import "strings"
|
|
|
|
// ContainerNameFor returns the expected container name for a service and
|
|
// component. For single-component services where the component name equals
|
|
// the service name, the container name is just the service name (e.g.,
|
|
// "mc-proxy" not "mc-proxy-mc-proxy").
|
|
func ContainerNameFor(service, component string) string {
|
|
if service == component {
|
|
return service
|
|
}
|
|
return service + "-" + component
|
|
}
|
|
|
|
// SplitContainerName splits a container name into service and component parts.
|
|
// It checks known service names first to handle names like "mc-proxy" where a
|
|
// naive split on "-" would produce the wrong result. If no known service
|
|
// matches, it falls back to splitting on the first "-".
|
|
func SplitContainerName(name string, knownServices map[string]bool) (service, component string) {
|
|
if knownServices[name] {
|
|
return name, name
|
|
}
|
|
for svc := range knownServices {
|
|
prefix := svc + "-"
|
|
if strings.HasPrefix(name, prefix) && len(name) > len(prefix) {
|
|
return svc, name[len(prefix):]
|
|
}
|
|
}
|
|
if i := strings.Index(name, "-"); i >= 0 {
|
|
return name[:i], name[i+1:]
|
|
}
|
|
return name, name
|
|
}
|