-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
77 lines (68 loc) · 1.93 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
69
70
71
72
73
74
75
76
77
package pork
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/katoozi/go-devops-tools/nap"
"github.com/spf13/cobra"
)
type ReadmeResponse struct {
Name string `json:"name"`
Content string `json:"content"`
}
// DocsCmd will read the repo READEME.md file
var DocsCmd = &cobra.Command{
Use: "docs",
Short: "read the documentation for a repo",
Run: func(cmd *cobra.Command, args []string) {
if len(args) <= 0 {
log.Fatal("You must provide repo argument")
}
if err := GetRepoReadme(args[0]); err != nil {
log.Fatalln("Failed to read docs: ", err)
}
},
}
// GetRepoReadme will return the README.md file of the repo
func GetRepoReadme(repo string) error {
values := strings.Split(repo, "/")
if len(values) != 2 {
return fmt.Errorf("repository name must be in this format: owner/project")
}
return GitHubAPI().Call("docs", map[string]string{
"owner": values[0],
"project": values[1],
}, nil)
}
// ReadmeSuccess will execute when request was success
func ReadmeSuccess(resp *http.Response, _ interface{}) error {
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
respContent := &ReadmeResponse{}
json.Unmarshal(content, respContent)
buff, err := base64.StdEncoding.DecodeString(respContent.Content)
if err != nil {
return err
}
fmt.Println(string(buff))
return nil
}
// ReadmeDefaultRouter is the default router for Read Readme file
func ReadmeDefaultRouter(resp *http.Response, _ interface{}) error {
return fmt.Errorf("status code: %d", resp.StatusCode)
}
// GetReadmeRestResource create nap.RestResource for Read Readme.md file from repository
func GetReadmeRestResource() *nap.RestResource {
Readrouter := nap.NewRouter()
Readrouter.DefaultRouter = ReadmeDefaultRouter
Readrouter.RegisterFunc(200, ReadmeSuccess)
docs := nap.NewResource("/repos/{{.owner}}/{{.project}}/readme", "GET", Readrouter)
return docs
}