rickub/clipublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/rickub/cli.git
git clone ssh://git@rickub.com/rickub/cli.git
Initial import of the rickub CLI as a standalone public project 1a1d430Unverified · on main · Olivier Girardot · 7h ago
search.go · 53 lines · 1.1 KBGo Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cmd

import (
	"fmt"
	"strings"

	"github.com/spf13/cobra"
)

func init() {
	searchCmd := &cobra.Command{
		Use:   "search",
		Short: "Search rickub",
	}

	reposCmd := &cobra.Command{
		Use:   "repos <query>",
		Short: "Search repositories",
		Args:  cobra.MinimumNArgs(1),
		RunE:  runSearchRepos,
	}
	addPaging(reposCmd)

	searchCmd.AddCommand(reposCmd)
	rootCmd.AddCommand(searchCmd)
}

func runSearchRepos(cmd *cobra.Command, args []string) error {
	client, err := newClient()
	if err != nil {
		return err
	}
	q := strings.Join(args, " ")
	page, err := client.SearchRepos(cmd.Context(), q, pageFlag, perPageFlag)
	if err != nil {
		return err
	}
	if flagJSON {
		return printJSON(cmd.OutOrStdout(), page)
	}
	if len(page.Items) == 0 {
		fmt.Fprintln(cmd.OutOrStdout(), "No repositories matched.")
		return nil
	}
	tw := newTabw(cmd.OutOrStdout())
	fmt.Fprintln(tw, "NAME\tVISIBILITY\tDESCRIPTION")
	for _, r := range page.Items {
		fmt.Fprintf(tw, "%s\t%s\t%s\n", r.FullName, r.Visibility, dash(r.Description))
	}
	tw.Flush()
	printPageFooter(cmd, page.Page)
	return nil
}