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

     1  package cluster
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/swarmkit/api"
     8  )
     9  
    10  func getCluster(ctx context.Context, c api.ControlClient, input string) (*api.Cluster, error) {
    11  	rg, err := c.GetCluster(ctx, &api.GetClusterRequest{ClusterID: input})
    12  	if err == nil {
    13  		return rg.Cluster, nil
    14  	}
    15  	rl, err := c.ListClusters(ctx,
    16  		&api.ListClustersRequest{
    17  			Filters: &api.ListClustersRequest_Filters{
    18  				Names: []string{input},
    19  			},
    20  		},
    21  	)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	if len(rl.Clusters) == 0 {
    27  		return nil, fmt.Errorf("cluster %s not found", input)
    28  	}
    29  
    30  	if l := len(rl.Clusters); l > 1 {
    31  		return nil, fmt.Errorf("cluster %s is ambiguous (%d matches found)", input, l)
    32  	}
    33  
    34  	return rl.Clusters[0], nil
    35  }