Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A set of device list cleanups #397

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions client/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,26 +782,12 @@ func (a *Api) DeviceGet(factory, device string) (*Device, error) {
return &d, nil
}

func (a *Api) DeviceList(
mine, prod, nonProd bool, matchTag, byFactory, byGroup, nameIlike, uuid, byTarget, sortBy string, page, limit uint64,
) (*DeviceList, error) {
var (
mineInt int
prodStr string
)
if mine {
mineInt = 1
}
switch {
case prod:
prodStr = "1"
case nonProd:
prodStr = "0"
}
func (a *Api) DeviceList(filterBy map[string]string, sortBy string, page, limit uint64) (*DeviceList, error) {
url := a.serverUrl + "/ota/devices/?"
url += fmt.Sprintf(
"mine=%d&prod=%s&match_tag=%s&name_ilike=%s&factory=%s&uuid=%s&group=%s&target_name=%s&sortby=%s&page=%d&limit=%d",
mineInt, prodStr, matchTag, nameIlike, byFactory, uuid, byGroup, byTarget, sortBy, page, limit)
for key, val := range filterBy {
url += fmt.Sprintf("%s=%s&", key, val)
}
url += fmt.Sprintf("sortby=%s&page=%d&limit=%d", sortBy, page, limit)
logrus.Debugf("DeviceList with url: %s", url)
return a.DeviceListCont(url)
}
Expand Down
52 changes: 19 additions & 33 deletions subcommands/devices/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,6 @@ func init() {
listCmd.MarkFlagsMutuallyExclusive("sort-by-name", "sort-by-last-seen")
}

// We allow pattern matching using filepath.Match type * and ?
// ie * matches everything and ? matches a single character.
// In sql we need * and ? to maps to % and _ respectively
// Since _ is a special character we need to escape that. And
//
// Soo... a pattern like: H?st_b* would become: H_st\_b%
// and would match stuff like host_b and hast_FOOO
func sqlLikeIfy(filePathLike string) string {
// %25 = urlencode("%")
sql := strings.Replace(filePathLike, "*", "%25", -1)
sql = strings.Replace(sql, "_", "\\_", -1)
sql = strings.Replace(sql, "?", "_", -1)
logrus.Debugf("Converted query(%s) -> %s", filePathLike, sql)
return sql
}

func assertPagination() {
// hack until: https://github.com/spf13/pflag/issues/236
for _, x := range paginationLimits {
Expand Down Expand Up @@ -220,24 +204,26 @@ func doList(cmd *cobra.Command, args []string) {
sortBy = appendSortFlagValue(sortBy, cmd, "sort-by-last-seen", "last_seen")
sortBy = appendSortFlagValue(sortBy, cmd, "sort-by-name", "name")

name_ilike := ""
filterBy := map[string]string{
"factory": factory,
"group": deviceByGroup,
"match_tag": deviceByTag,
"target_name": deviceByTarget,
"uuid": deviceUuid,
}
if len(args) == 1 {
name_ilike = sqlLikeIfy(args[0])
}
dl, err := api.DeviceList(
deviceMine,
deviceOnlyProd,
deviceOnlyNonProd,
deviceByTag,
factory,
deviceByGroup,
name_ilike,
deviceUuid,
deviceByTarget,
strings.Join(sortBy, ","),
showPage,
paginationLimit,
)
filterBy["name"] = args[0]
}
if deviceMine {
filterBy["mine"] = "1"
}
if deviceOnlyProd {
filterBy["prod"] = "1"
} else if deviceOnlyNonProd {
filterBy["prod"] = "0"
}

dl, err := api.DeviceList(filterBy, strings.Join(sortBy, ","), showPage, paginationLimit)
vkhoroz marked this conversation as resolved.
Show resolved Hide resolved
subcommands.DieNotNil(err)
showDeviceList(dl, showColumns)
}