github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/cmd/swarmctl/service/common.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/swarmkit/api"
     8  )
     9  
    10  func getService(ctx context.Context, c api.ControlClient, input string) (*api.Service, error) {
    11  	// GetService to match via full ID.
    12  	rg, err := c.GetService(ctx, &api.GetServiceRequest{ServiceID: input})
    13  	if err != nil {
    14  		// If any error (including NotFound), ListServices to match via full name.
    15  		rl, err := c.ListServices(ctx,
    16  			&api.ListServicesRequest{
    17  				Filters: &api.ListServicesRequest_Filters{
    18  					Names: []string{input},
    19  				},
    20  			},
    21  		)
    22  		if err != nil {
    23  			return nil, err
    24  		}
    25  
    26  		if len(rl.Services) == 0 {
    27  			return nil, fmt.Errorf("service %s not found", input)
    28  		}
    29  
    30  		if l := len(rl.Services); l > 1 {
    31  			return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)
    32  		}
    33  
    34  		return rl.Services[0], nil
    35  	}
    36  	return rg.Service, nil
    37  }
    38  
    39  func getServiceReplicasTxt(s *api.Service, running int) string {
    40  	switch t := s.Spec.GetMode().(type) {
    41  	case *api.ServiceSpec_Global:
    42  		return "global"
    43  	case *api.ServiceSpec_Replicated:
    44  		return fmt.Sprintf("%d/%d", running, t.Replicated.Replicas)
    45  	}
    46  	return ""
    47  }