Fuzzy matching when filtering services

This commit is contained in:
cuigh 2018-04-16 17:44:44 +08:00
parent f2a4b7209c
commit adb2874e60

View File

@ -32,15 +32,21 @@ func ServiceList(name string, pageIndex, pageSize int) (infos []*model.ServiceLi
)
// acquire services
opts := types.ServiceListOptions{}
if name != "" {
opts.Filters = filters.NewArgs()
opts.Filters.Add("name", name)
}
services, err = cli.ServiceList(ctx, opts)
services, err = cli.ServiceList(ctx, types.ServiceListOptions{})
if err != nil {
return
}
if name != "" {
j := 0
name = strings.ToLower(name)
for i, l := 0, len(services); i < l; i++ {
if strings.Contains(strings.ToLower(services[i].Spec.Name), name) {
services[j] = services[i]
j++
}
}
services = services[:j]
}
sort.Slice(services, func(i, j int) bool {
return services[i].Spec.Name < services[j].Spec.Name
})