/
gist.go
168 lines (145 loc) · 3.27 KB
/
gist.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package gist
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"sync"
"time"
"github.com/b4b4r07/gist/pkg/git"
"github.com/google/go-github/github"
)
type Gist struct {
User string
Token string
Editor string
Client Client
WorkDir string
Pages []Page
}
// Page represents gist page itself
type Page struct {
ID string `json:"id"`
Description string `json:"description"`
User string `json:"user"`
URL string `json:"url"`
Public bool `json:"public"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Files []File `json:"files"`
Repo *git.Repo `json:"-"`
}
// File represents a single file hosted on gist
type File struct {
Name string `json:"name"`
Content string `json:"content"`
FullPath string `json:"fullpath"`
Page `json:"-"`
}
func (g Gist) Files() []File {
var files []File
for _, page := range g.Pages {
for _, file := range page.Files {
path := filepath.Join(g.WorkDir, g.User, page.ID, file.Name)
content, _ := ioutil.ReadFile(path)
files = append(files, File{
Name: file.Name,
Content: string(content),
FullPath: path,
Page: page,
})
}
}
return files
}
func (g *Gist) Checkout() error {
ch := make(chan Page, len(g.Pages))
wg := new(sync.WaitGroup)
for _, page := range g.Pages {
page := page
wg.Add(1)
go func() {
defer func() {
ch <- page
wg.Done()
}()
repo, err := git.NewRepo(git.Config{
URL: page.URL,
WorkDir: filepath.Join(g.WorkDir, g.User, page.ID),
Username: g.User,
Token: g.Token,
})
if err != nil {
return
}
repo.CloneOrOpen(context.Background())
page.Repo = repo
}()
}
go func() {
wg.Wait()
close(ch)
}()
pages := []Page{}
for p := range ch {
pages = append(pages, p)
}
sort.Slice(pages, func(i, j int) bool {
return pages[i].CreatedAt.After(pages[j].CreatedAt)
})
g.Pages = pages
return nil
}
func (f File) HasUpdated() (bool, error) {
ctx := context.Background()
repo := f.Page.Repo
if repo == nil {
return false, fmt.Errorf("%s: repository not found", f.Name)
}
if err := repo.Open(ctx); err != nil {
return false, err
}
return !repo.IsClean(), nil
}
func (f File) Update() error {
ctx := context.Background()
repo := f.Page.Repo
if repo == nil {
return fmt.Errorf("%s: repository not found", f.Name)
}
if err := repo.Open(ctx); err != nil {
return err
}
if repo.IsClean() {
// no need to push
return nil
}
if err := repo.Add(f.Name); err != nil {
return err
}
if err := repo.Commit("update"); err != nil {
return err
}
return repo.Push(ctx)
}
func (g Gist) Create(page Page) (string, error) {
files := make(map[github.GistFilename]github.GistFile)
for _, file := range page.Files {
name := github.GistFilename(file.Name)
files[name] = github.GistFile{
Filename: github.String(file.Name),
Content: github.String(file.Content),
}
}
gist, _, err := g.Client.Gists.Create(context.Background(), &github.Gist{
Files: files,
Description: github.String(page.Description),
Public: github.Bool(page.Public),
})
return gist.GetHTMLURL(), err
}
func (g Gist) Delete(page Page) error {
_, err := g.Client.Gists.Delete(context.Background(), page.ID)
return err
}