-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (53 loc) · 1.58 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"regexp"
"github.com/mattcullenmeyer/github-stargazer-client/getdata"
)
// https://levelup.gitconnected.com/tutorial-how-to-create-a-cli-tool-in-golang-a0fd980264f
// https://www.geeksforgeeks.org/command-line-arguments-in-golang/
func getRepos(repos []string, url string) string {
// Initialize response as blank string
response := ""
// Loop through each command line argument
for _, repo := range repos {
// Check to make sure command line argument includes a "/"
matched, err := regexp.MatchString(`.\/.`, repo)
if err != nil {
log.Fatal(err)
}
// Concatenate results from each repo
if matched {
stars := getdata.GetData(repo, url)
response += fmt.Sprintf("%s --> %s\n", repo, stars)
} else {
response += fmt.Sprintf("The argument '%s' is not valid. The format should be <organization>/<repository>.\n", repo)
}
}
return response
}
func cliResponse(repos []string, url string) string {
n := len(repos)
if n < 1 {
return `At least one <organization>/<repository> is required as an argument.
For example:
$ go run main.go mattcullenmeyer/tinytrader
Or, if entering more than one:
$ go run main.go mattcullenmeyer/tinytrader mattcullenmeyer/anaplan`
}
// Get all arguments afer the first, which is the name of the program itself
//repos := os.Args[1:]
txt := getRepos(repos, url)
return txt
}
func main() {
urlPtr := flag.String("url", "http://localhost:8080", "url path")
flag.Parse()
url := *urlPtr
//txt := cliResponse(os.Args, url)
repos := flag.Args()
txt := cliResponse(repos, url)
fmt.Printf(txt)
}