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
search.go · 53 lines · 1.1 KBGo Blame HistoryRaw
Initial import of the rickub CLI as a standalone public project 1a1d430 Olivier Girardot 9h ago1package cmd
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/spf13/cobra"
8)
9
10func init() {
11 searchCmd := &cobra.Command{
12 Use: "search",
13 Short: "Search rickub",
14 }
15
16 reposCmd := &cobra.Command{
17 Use: "repos <query>",
18 Short: "Search repositories",
19 Args: cobra.MinimumNArgs(1),
20 RunE: runSearchRepos,
21 }
22 addPaging(reposCmd)
23
24 searchCmd.AddCommand(reposCmd)
25 rootCmd.AddCommand(searchCmd)
26}
27
28func runSearchRepos(cmd *cobra.Command, args []string) error {
29 client, err := newClient()
30 if err != nil {
31 return err
32 }
33 q := strings.Join(args, " ")
34 page, err := client.SearchRepos(cmd.Context(), q, pageFlag, perPageFlag)
35 if err != nil {
36 return err
37 }
38 if flagJSON {
39 return printJSON(cmd.OutOrStdout(), page)
40 }
41 if len(page.Items) == 0 {
42 fmt.Fprintln(cmd.OutOrStdout(), "No repositories matched.")
43 return nil
44 }
45 tw := newTabw(cmd.OutOrStdout())
46 fmt.Fprintln(tw, "NAME\tVISIBILITY\tDESCRIPTION")
47 for _, r := range page.Items {
48 fmt.Fprintf(tw, "%s\t%s\t%s\n", r.FullName, r.Visibility, dash(r.Description))
49 }
50 tw.Flush()
51 printPageFooter(cmd, page.Page)
52 return nil
53}