package main import ( "fmt" "net/http" "strconv" "strings" "github.com/gogits/go-gogs-client" "github.com/spf13/cobra" "github.com/spf13/viper" ) // Flags. var limit string // max num results var userName string func searchRepos(uid, query, limit string) ([]*gogs.Repository, error) { client := &http.Client{} path := "/api/v1/repos/search?q=" + query + "&uid=" + uid + "&limit=" + limit repos, err := getParsedResponse(client, "GET", viper.GetString("GOGS_URL")+path, nil, nil) return repos, err } // searchCmd represents the search command var searchCmd = &cobra.Command{ Aliases: []string{"s", "find", "f"}, Use: "search", Short: "search myquery [-l -u]", Example: ` gogs repo s waldo gogs repo find waldo gogs repo search waldo -l 5 gogs repo find waldo --user=johnny --limit=100 `, Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { fmt.Println("Please argue me something to search... like: $ gogs repo search golang") fmt.Println("For help, run $ gogs repo search --help") return } // uid := make(chan string) uid := "0" var u *gogs.User var e error // flag for user name, we'll need to get that user's uid if userName != "" { // go func() { u, e = getUserByName(userName) if e != nil { fmt.Println(e) } uid = strconv.Itoa(int(u.ID)) // }() } repos, err := searchRepos(uid, args[0], limit) if err != nil { fmt.Println(err) return } var describeUserScope string if uid != "0" { describeUserScope = u.UserName + "'s repos" } else { describeUserScope = "all repos" } fmt.Println("Searching '" + args[0] + "' in " + describeUserScope + "...") for _, repo := range repos { // get (mostly) empty data in, need to make n more calls to GetRepo... shit. splitter := strings.Split(repo.FullName, "/") owner := splitter[0] reponame := splitter[1] r, e := GetClient().GetRepo(owner, reponame) if e != nil { fmt.Println(e) return } printRepo(r) } }, } func init() { repoCmd.AddCommand(searchCmd) searchCmd.Flags().StringVarP(&userName, "user", "u", "", "whose repos to search in") searchCmd.Flags().StringVarP(&limit, "limit", "l", "10", "limit number of results") }