// Case insensitive fuzzy match. func (l *Ltxref) FilterPackages(like string, tag string) []*Package { if like == "" && tag == "" { return l.Packages } else { like = strings.ToLower(like) tag = strings.ToLower(tag) } var itemsThatMatch []*Package for _, item := range l.Packages { if fuzzy.Match(like, item.Name) && (tag == "" || hasTag(item.Label, tag)) { itemsThatMatch = append(itemsThatMatch, item) } else { for _, command := range item.Commands { if (like == "" || fuzzy.Match(like, command.Name)) && (tag == "" || hasTag(command.Label, tag)) { itemsThatMatch = append(itemsThatMatch, item) break } } } } return itemsThatMatch }
// Case insensitive fuzzy match. func (l *Ltxref) FilterCommands(like string, tag string, showexpert bool) Commands { var commandsThatMatch Commands like = strings.ToLower(like) tag = strings.ToLower(tag) for _, command := range l.Commands { if (like == "" || fuzzy.Match(like, command.Name)) && (tag == "" || hasTag(command.Label, tag)) { if !showexpert && command.Level != "expert" || showexpert { commandsThatMatch = append(commandsThatMatch, command) } } } return commandsThatMatch }
// Case insensitive fuzzy match. func (l *Ltxref) FilterDocumentClasses(like string, tag string, showexpert bool) DocumentClasses { if like == "" && tag == "" && showexpert { return l.DocumentClasses } else { like = strings.ToLower(like) tag = strings.ToLower(tag) } var itemsThatMatch DocumentClasses for _, item := range l.DocumentClasses { if fuzzy.Match(like, item.Name) && (tag == "" || hasTag(item.Label, tag)) { if !showexpert && item.Level != "expert" || showexpert { itemsThatMatch = append(itemsThatMatch, item) } } } return itemsThatMatch }
// RunSearch is the handler for 'scw search' func RunSearch(ctx CommandContext, args SearchArgs) error { // FIXME: parallelize API calls term := strings.ToLower(args.Term) w := tabwriter.NewWriter(ctx.Stdout, 10, 1, 3, ' ', 0) defer w.Flush() fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n") var entries = []api.ScalewayImageInterface{} images, err := ctx.API.GetImages() if err != nil { return fmt.Errorf("unable to fetch images from the Scaleway API: %v", err) } for _, val := range *images { if fuzzy.Match(term, strings.ToLower(val.Name)) { entries = append(entries, api.ScalewayImageInterface{ Type: "image", Name: val.Name, Public: val.Public, }) } } snapshots, err := ctx.API.GetSnapshots() if err != nil { return fmt.Errorf("unable to fetch snapshots from the Scaleway API: %v", err) } for _, val := range *snapshots { if fuzzy.Match(term, strings.ToLower(val.Name)) { entries = append(entries, api.ScalewayImageInterface{ Type: "snapshot", Name: val.Name, Public: false, }) } } for _, image := range entries { // name field name := utils.TruncIf(utils.Wordify(image.Name), 45, !args.NoTrunc) // description field var description string switch image.Type { case "image": if image.Public { description = "public image" } else { description = "user image" } case "snapshot": description = "user snapshot" } description = utils.TruncIf(utils.Wordify(description), 45, !args.NoTrunc) // official field var official string if image.Public { official = "[OK]" } else { official = "" } fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\n", name, description, 0, official, "") } return nil }