-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusersearch.go
134 lines (127 loc) · 3.42 KB
/
usersearch.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/fatih/color"
)
//ShowUserResult function to print results values
func (results *RespUser) ShowUserResult() {
var output []string
count := fmt.Sprintf("%s\nResults Count: %v\n%s", line, results.Count, line)
output = append(output, count)
for _, i := range results.Items {
if ospager != "more" {
match := fmt.Sprintf("User: %v\nURL: %v\nScore: %v\n%s", color.YellowString(i.Login), i.HTMLURL, i.Score, line)
output = append(output, match)
} else {
match := fmt.Sprintf("User: %v\nURL: %v\nScore: %v\n%s", i.Login, i.HTMLURL, i.Score, line)
output = append(output, match)
}
}
pager(strings.Join(output, "\n"))
}
//searchUser function to make search for a particular user pattern
func searchUser(apiurl, pattern, paging string) RespUser {
var data RespUser
var linkHeader string
var url string
switch {
case len(language) > 0:
url = (apiurl + "users?q=" + pattern + "+language:" + language + "&per_page=" + paging)
case len(language) == 0:
url = (apiurl + "users?q=" + pattern + "&per_page=" + paging)
}
fmt.Println(line)
fmt.Println("using url:", url)
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
if linkHeader = response.Header.Get("Link"); len(linkHeader) == 0 {
fmt.Println("Showing results in one page")
}
data.NextURL, data.PreviousURL = Regexp(linkHeader)
err = json.NewDecoder(response.Body).Decode(&data)
if err != nil {
log.Fatal(err)
}
return data
}
// function to continue with next url
func continueSearchUser(url string) RespUser {
var data RespUser
var linkHeader string
fmt.Println(line)
fmt.Println("using url:", url)
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
if linkHeader = response.Header.Get("Link"); len(linkHeader) == 0 {
fmt.Println("No more results")
}
data.NextURL, data.PreviousURL = Regexp(linkHeader)
err = json.NewDecoder(response.Body).Decode(&data)
if err != nil {
log.Fatal(err)
}
return data
}
func getUserConfirm(items *RespUser) error {
var answer string
fmt.Println("Go to next/previous page, Show again or Quit? (N/P/S/Q):")
n, err := fmt.Scanf("%s\n", &answer)
if err != nil {
fmt.Println(n, err)
return err
}
switch {
case answer == "N" || answer == "n":
if items.NextURL != "" {
*items = continueSearchUser(items.NextURL)
items.ShowUserResult()
} else {
fmt.Println("No next page found")
}
return nil
case answer == "P" || answer == "p":
if items.PreviousURL != "" {
*items = continueSearchUser(items.PreviousURL)
items.ShowUserResult()
} else {
fmt.Println("No previous page found")
}
return nil
case answer == "Q" || answer == "q":
fmt.Println("Stopping")
os.Exit(0)
case answer == "S" || answer == "s":
items.ShowUserResult()
return nil
default:
fmt.Printf("%s\n--- Option '%s' no available ---\n%s\n", linebig, answer, linebig)
}
return nil
}
//RunSearchUser function to run the main process for user search
func RunSearchUser(apiurl, user, paging string) {
items := searchUser(apiurl, user, paging)
if items.Count > 0 {
items.ShowUserResult()
// loop over next page url
for items.NextURL != "" || items.PreviousURL != "" {
fmt.Println(line)
fmt.Println("Next Page ==>", items.NextURL)
fmt.Println("Previous Page ==>", items.PreviousURL)
getUserConfirm(&items)
}
fmt.Println(line)
//os.Exit(0)
}
}