Searchified 🔮

This commit is contained in:
Mr. Is 2016-07-28 06:46:02 -04:00
parent 11a6580283
commit 2215750f5d
5 changed files with 205 additions and 14 deletions

View File

@ -9,8 +9,9 @@ import (
// repoCmd represents the repo command
var repoCmd = &cobra.Command{
Use: "repo",
Short: "parent command for repositories",
Aliases: []string{"r"},
Use: "repo",
Short: "parent command for repositories",
Long: `gogs repo [(new|create)|list|destroy]
$ gogs repo new my-new-repo --private
@ -20,30 +21,30 @@ var repoCmd = &cobra.Command{
$ gogs repo destroy ia/my-new-repo
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Please use: gogs repo [(new|create)|list|destroy]")
},
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("Please use: gogs repo [(new|create)|list|destroy]")
// },
}
func printRepo(repo *gogs.Repository) {
fmt.Println("------------------------------------------------")
fmt.Printf("* %v", repo.FullName)
fmt.Println()
fmt.Printf("| * %v", repo.FullName)
if repo.Private {
fmt.Printf(" (private)")
}
if repo.Fork {
fmt.Printf(" (fork)")
}
fmt.Println()
fmt.Println("")
if repo.Description != "" {
fmt.Println("[--> ", repo.Description)
fmt.Println("| --> ", repo.Description)
}
fmt.Println("Go there: ", repo.HtmlUrl)
fmt.Println("SSH: ", repo.SshUrl)
fmt.Println("Clone it: ", repo.CloneUrl)
fmt.Println("------------------------------------------------")
fmt.Println("| HTML: ", repo.HtmlUrl)
fmt.Println("| SSH : ", repo.SshUrl)
fmt.Println("| GIT : ", repo.CloneUrl)
fmt.Println()
}
func init() {

View File

@ -94,7 +94,7 @@ func init() {
}
func GetClient() *gogs.Client {
fmt.Println("Getting client....")
// fmt.Println("Getting client....")
return client
}

120
cmd/search.go Normal file
View File

@ -0,0 +1,120 @@
package cmd
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 getUserByName(userName string) (user *gogs.User, err error) {
user, err = GetClient().GetUserInfo(userName)
if err != nil {
fmt.Println(err)
return
}
return user, err
}
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("api_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]",
Long: `Search repos by keyword, with optional flags for [-l | --limit] and [-u | --user]
$ gogs repo s waldo
$ gogs repo find waldo
$ gogs repo search waldo -l 5
$ gogs repo find waldo --user=johnny --limit=100
Default limit is 10.
Default all users' repos.
** NOTE that in order to search PRIVATE repos, you'll need to provide the
USERNAME FLAG
`,
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)
var uid string
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 = "0"
}
uid = strconv.Itoa(int(u.ID))
// }()
} else {
uid = "0"
}
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")
}

70
cmd/util.go Normal file
View File

@ -0,0 +1,70 @@
package cmd
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
gogs "github.com/gogits/go-gogs-client"
"github.com/spf13/viper"
)
type expRes struct {
Data []*gogs.Repository `json:"data"`
}
func getResponse(client *http.Client, method, url string, header http.Header, body io.Reader) ([]byte, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "token "+viper.GetString("token"))
for k, v := range header {
req.Header[k] = v
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 403:
return nil, errors.New("403 Forbidden")
case 404:
return nil, errors.New("404 Not Found")
}
if resp.StatusCode/100 != 2 {
errMap := make(map[string]interface{})
if err = json.Unmarshal(data, &errMap); err != nil {
return nil, err
}
return nil, errors.New(errMap["message"].(string))
}
return data, nil
}
func getParsedResponse(client *http.Client, method, path string, header http.Header, body io.Reader) ([]*gogs.Repository, error) {
data, err := getResponse(client, method, path, header, body)
if err != nil {
return nil, err
}
var res expRes
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res.Data, err
// return json.Unmarshal(data, obj)
}

BIN
gogs

Binary file not shown.