refactor: making app version command async
This commit is contained in:
		@ -14,6 +14,12 @@ import (
 | 
			
		||||
	"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
 | 
			
		||||
func parseVersionLabel(label string) (string, string) {
 | 
			
		||||
	// versions may look like v4.2-abcd or v4.2-alpine-abcd
 | 
			
		||||
@ -46,15 +52,24 @@ var appVersionCommand = &cli.Command{
 | 
			
		||||
			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"}
 | 
			
		||||
		table := abraFormatter.CreateTable(tableCol)
 | 
			
		||||
 | 
			
		||||
		for _, service := range compose.Services {
 | 
			
		||||
			label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name)
 | 
			
		||||
			status := stack.GetDeployedServicesByLabel(app.Server, label)
 | 
			
		||||
			for _, serviceStatus := range status.Services {
 | 
			
		||||
				version, digest := parseVersionLabel(serviceStatus.Spec.Labels[label])
 | 
			
		||||
				img, err := reference.ParseNormalizedNamed(service.Image)
 | 
			
		||||
		for range compose.Services {
 | 
			
		||||
			status := <-ch
 | 
			
		||||
			for _, service := range status.Services {
 | 
			
		||||
				serviceName := parseServiceName(service.Spec.Name)
 | 
			
		||||
				label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), serviceName)
 | 
			
		||||
				version, digest := parseVersionLabel(service.Spec.Labels[label])
 | 
			
		||||
				img, err := reference.ParseNormalizedNamed(service.Spec.Labels["com.docker.stack.image"])
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					logrus.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
@ -62,7 +77,6 @@ var appVersionCommand = &cli.Command{
 | 
			
		||||
				if strings.Contains(image, "library") {
 | 
			
		||||
					image = strings.Split(image, "/")[1]
 | 
			
		||||
				}
 | 
			
		||||
				serviceName := fmt.Sprintf("%s_%s", appEnv.StackName(), service.Name)
 | 
			
		||||
				table.Append([]string{serviceName, image, version, digest})
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user