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

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/swarmkit/api"
     8  )
     9  
    10  func getConfig(ctx context.Context, c api.ControlClient, input string) (*api.Config, error) {
    11  	// not sure what it is, match by name or id prefix
    12  	resp, err := c.ListConfigs(ctx,
    13  		&api.ListConfigsRequest{
    14  			Filters: &api.ListConfigsRequest_Filters{
    15  				Names:      []string{input},
    16  				IDPrefixes: []string{input},
    17  			},
    18  		},
    19  	)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	switch len(resp.Configs) {
    25  	case 0:
    26  		return nil, fmt.Errorf("config %s not found", input)
    27  	case 1:
    28  		return resp.Configs[0], nil
    29  	default:
    30  		// ok, multiple matches.  Prefer exact ID over exact name.  If no exact matches, return an error
    31  		for _, s := range resp.Configs {
    32  			if s.ID == input {
    33  				return s, nil
    34  			}
    35  		}
    36  		for _, s := range resp.Configs {
    37  			if s.Spec.Annotations.Name == input {
    38  				return s, nil
    39  			}
    40  		}
    41  		return nil, fmt.Errorf("config %s is ambiguous (%d matches found)", input, len(resp.Configs))
    42  	}
    43  }