/
cmd_dl_ls.go
99 lines (81 loc) · 2.39 KB
/
cmd_dl_ls.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
package main
import (
"fmt"
"net/http"
"path"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/sbinet/go-github-client/client"
)
func git_make_cmd_dl_ls() *commander.Command {
cmd := &commander.Command{
Run: git_run_cmd_dl_ls,
UsageLine: "dl-ls [options] repo",
Short: "lists the available downloads of a github repository",
Long: `
dl-ls lists the available downloads of a github repository.
ex:
$ goctogit dl-ls mana-core
$ goctogit dl-ls -org my-organization mana-core
`,
Flag: *flag.NewFlagSet("git-dl-ls", flag.ExitOnError),
}
cmd.Flag.String("u", "", "github user account")
cmd.Flag.String("org", "", "github organization account")
return cmd
}
func git_run_cmd_dl_ls(cmd *commander.Command, args []string) {
n := "github-" + cmd.Name()
if len(args) != 1 {
err := fmt.Errorf("%s: needs a github repository name", n)
handle_err(err)
}
repo_name := args[0]
user := cmd.Flag.Lookup("u").Value.Get().(string)
org := cmd.Flag.Lookup("org").Value.Get().(string)
if user == "" {
v, err := Cfg.String("go-octogit", "username")
handle_err(err)
user = v
}
password, err := Cfg.String("go-octogit", "password")
handle_err(err)
ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
handle_err(err)
account := user
// GET /repos/:owner/:repo/downloads
if org != "" {
account = org
}
url := path.Join("repos", account, repo_name, "downloads")
fmt.Printf("%s: listing downloads for %s/%s...\n",
n, account, repo_name)
req, err := ghc.NewAPIRequest("GET", url, nil)
handle_err(err)
resp, err := ghc.RunRequest(req, new(http.Client))
handle_err(err)
if !resp.IsSuccess() {
err = fmt.Errorf("%s: request did not succeed. got (status=%d) %v\n", n, resp.RawHttpResponse.StatusCode, resp.RawHttpResponse)
handle_err(err)
}
json_arr, err := resp.JsonArray()
if err != nil {
handle_err(err)
}
for _, elmt := range json_arr {
json := client.JsonMap(elmt.(map[string]interface{}))
fmt.Printf("=== %s\n",
json.GetString("name"),
)
fmt.Printf("%3s id=%v\n", "", int64(json.GetFloat("id")))
fmt.Printf("%3s sz=%v bytes\n", "", int64(json.GetFloat("size")))
descr := json.GetString("description")
if descr != "" {
fmt.Printf("%3s descr=%q\n", "", descr)
}
fmt.Printf("%3s %s\n", "", json.GetString("html_url"))
}
fmt.Printf("%s: listing downloads for %s/%s... [done]\n",
n, account, repo_name)
}
// EOF