github.com/moby/docker@v26.1.3+incompatible/client/service_list.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/filters"
    10  	"github.com/docker/docker/api/types/swarm"
    11  )
    12  
    13  // ServiceList returns the list of services.
    14  func (cli *Client) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
    15  	query := url.Values{}
    16  
    17  	if options.Filters.Len() > 0 {
    18  		filterJSON, err := filters.ToJSON(options.Filters)
    19  		if err != nil {
    20  			return nil, err
    21  		}
    22  
    23  		query.Set("filters", filterJSON)
    24  	}
    25  
    26  	if options.Status {
    27  		query.Set("status", "true")
    28  	}
    29  
    30  	resp, err := cli.get(ctx, "/services", query, nil)
    31  	defer ensureReaderClosed(resp)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	var services []swarm.Service
    37  	err = json.NewDecoder(resp.body).Decode(&services)
    38  	return services, err
    39  }