MCPcopy Index your code
hub / github.com/docker/cli / createFilter

Function createFilter

cli/command/service/ps.go:83–139  ·  view source on GitHub ↗
(ctx context.Context, apiClient client.APIClient, options psOptions)

Source from the content-addressed store, hash-verified

81}
82
83func createFilter(ctx context.Context, apiClient client.APIClient, options psOptions) (client.Filters, []string, error) {
84 filter := options.filter.Value()
85
86 serviceIDFilter := make(client.Filters)
87 serviceNameFilter := make(client.Filters)
88 for _, service := range options.services {
89 serviceIDFilter.Add("id", service)
90 serviceNameFilter.Add("name", service)
91 }
92 serviceByID, err := apiClient.ServiceList(ctx, client.ServiceListOptions{Filters: serviceIDFilter})
93 if err != nil {
94 return filter, nil, err
95 }
96 serviceByName, err := apiClient.ServiceList(ctx, client.ServiceListOptions{Filters: serviceNameFilter})
97 if err != nil {
98 return filter, nil, err
99 }
100
101 var notfound []string
102 serviceCount := 0
103loop:
104 // Match services by 1. Full ID, 2. Full name, 3. ID prefix. An error is returned if the ID-prefix match is ambiguous
105 for _, service := range options.services {
106 for _, s := range serviceByID.Items {
107 if s.ID == service {
108 filter.Add("service", s.ID)
109 serviceCount++
110 continue loop
111 }
112 }
113 for _, s := range serviceByName.Items {
114 if s.Spec.Annotations.Name == service {
115 filter.Add("service", s.ID)
116 serviceCount++
117 continue loop
118 }
119 }
120 found := false
121 for _, s := range serviceByID.Items {
122 if strings.HasPrefix(s.ID, service) {
123 if found {
124 return filter, nil, errors.New("multiple services found with provided prefix: " + service)
125 }
126 filter.Add("service", s.ID)
127 serviceCount++
128 found = true
129 }
130 }
131 if !found {
132 notfound = append(notfound, "no such service: "+service)
133 }
134 }
135 if serviceCount == 0 {
136 return filter, nil, errors.New(strings.Join(notfound, "\n"))
137 }
138 return filter, notfound, err
139}
140

Callers 4

runPSFunction · 0.85
TestCreateFilterFunction · 0.85

Calls 2

ValueMethod · 0.65
ServiceListMethod · 0.45

Tested by 3

TestCreateFilterFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…