refactor: making app version command async

This commit is contained in:
decentral1se 2021-08-25 13:30:55 +02:00
parent ed11634abf
commit c5bb680fed
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC

View File

@ -14,6 +14,12 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
// parseVersionLabel parses a $STACK_NAME_$SERVICE_NAME service label
func parseServiceName(label string) string {
idx := strings.LastIndex(label, "_")
return label[idx+1:]
}
// parseVersionLabel parses a $VERSION-$DIGEST service label // parseVersionLabel parses a $VERSION-$DIGEST service label
func parseVersionLabel(label string) (string, string) { func parseVersionLabel(label string) (string, string) {
// versions may look like v4.2-abcd or v4.2-alpine-abcd // versions may look like v4.2-abcd or v4.2-alpine-abcd
@ -46,15 +52,24 @@ var appVersionCommand = &cli.Command{
logrus.Fatal(err) logrus.Fatal(err)
} }
ch := make(chan stack.StackStatus, len(compose.Services))
for _, service := range compose.Services {
label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name)
go func(s string, l string) {
ch <- stack.GetDeployedServicesByLabel(s, l)
}(app.Server, label)
}
tableCol := []string{"Name", "Image", "Version", "Digest"} tableCol := []string{"Name", "Image", "Version", "Digest"}
table := abraFormatter.CreateTable(tableCol) table := abraFormatter.CreateTable(tableCol)
for _, service := range compose.Services { for range compose.Services {
label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name) status := <-ch
status := stack.GetDeployedServicesByLabel(app.Server, label) for _, service := range status.Services {
for _, serviceStatus := range status.Services { serviceName := parseServiceName(service.Spec.Name)
version, digest := parseVersionLabel(serviceStatus.Spec.Labels[label]) label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), serviceName)
img, err := reference.ParseNormalizedNamed(service.Image) version, digest := parseVersionLabel(service.Spec.Labels[label])
img, err := reference.ParseNormalizedNamed(service.Spec.Labels["com.docker.stack.image"])
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
@ -62,7 +77,6 @@ var appVersionCommand = &cli.Command{
if strings.Contains(image, "library") { if strings.Contains(image, "library") {
image = strings.Split(image, "/")[1] image = strings.Split(image, "/")[1]
} }
serviceName := fmt.Sprintf("%s_%s", appEnv.StackName(), service.Name)
table.Append([]string{serviceName, image, version, digest}) table.Append([]string{serviceName, image, version, digest})
} }
} }