-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdocs.go
68 lines (59 loc) · 1.47 KB
/
docs.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
package pork
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/mspaulding06/nap"
"github.com/spf13/cobra"
)
type ReadResponse struct {
Content string `json:"content"`
}
var DocsCmd = &cobra.Command{
Use: "docs",
Short: "read the documentation for a repository",
Run: func(cmd *cobra.Command, args []string) {
if len(args) <= 0 {
log.Fatalln("You must supply repository argument")
}
if err := GetRepositoryReadme(args[0]); err != nil {
log.Fatalln("Failed to get docs: ", err)
}
},
}
func GetRepositoryReadme(repository string) error {
values := strings.Split(repository, "/")
return GitHubAPI().Call("docs", map[string]string{
"owner": values[0],
"project": values[1],
}, nil)
}
func ReadmeSuccess(resp *http.Response) error {
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
respContent := ReadResponse{}
json.Unmarshal(content, &respContent)
buff, err := base64.StdEncoding.DecodeString(respContent.Content)
if err != nil {
return err
}
fmt.Println(string(buff))
return nil
}
func ReadmeDefaultRouter(resp *http.Response) error {
return fmt.Errorf("status code %d", resp.StatusCode)
}
func GetReadmeResource() *nap.RestResource {
router := nap.NewRouter()
router.RegisterFunc(200, ReadmeSuccess)
router.DefaultRouter = ReadmeDefaultRouter
resource := nap.NewResource("/repos/{{.owner}}/{{.project}}/readme", "GET", router)
return resource
}