This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.go
224 lines (179 loc) · 4.34 KB
/
git.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Operations and flow control on Git repositories managed by the application.
package main
import (
"fmt"
"runtime"
"strings"
"sync"
badger "github.com/dgraph-io/badger"
"github.com/google/uuid"
"github.com/logrusorgru/aurora"
"golang.org/x/xerrors"
)
func addURL(db *badger.DB, link string) (repo Repo, err error) {
repo, err = dbGet(db, link)
if xerrors.Is(err, badger.ErrKeyNotFound) {
return onNewRepo(db, link)
} else if err != nil {
return
}
return onOldRepo(db, repo)
}
// rmRepo completely removes any traces of a repository in the system.
func rmRepo(db *badger.DB, repo Repo) {
if repo.UUID != "" {
rm(repo.UUID)
}
if repo.URL != "" {
ipfsKeyRmName(repo.URL)
dbDelete(db, repo.URL)
}
if repo.IPFS != "" {
ipfsClusterRm(repo.IPFS)
}
if repo.Key != "" {
ipfsKeyRm(repo.Key)
}
if repo.IPNS != "" {
ipfsKeyRm(repo.IPNS)
}
}
func onAllRepos(db *badger.DB) {
fmt.Println("Refreshing all repos...")
fmt.Println()
ch := dbList(db)
var wg sync.WaitGroup
for c := 1; c <= runtime.NumCPU(); c++ {
wg.Add(1)
go func(ch chan repoerr) {
for re := range ch {
if re.err != nil {
fmt.Println("Couldn't refresh a repo.")
fmt.Println(re.err.Error())
}
onOldRepo(db, re.repo)
}
wg.Done()
}(ch)
}
wg.Wait()
fmt.Println("All repos are refreshed.")
fmt.Println()
return
}
func onOldRepo(db *badger.DB, repo Repo) (Repo, error) {
// Show old values
fmt.Println(aurora.Bold("UUID :"), repo.UUID)
fmt.Println(aurora.Bold("URL :"), aurora.Blue(repo.URL))
fmt.Println(aurora.Bold("IPFS :"), aurora.Cyan(repo.IPFS))
fmt.Println(aurora.Bold("Key :"), repo.Key)
fmt.Println(aurora.Bold("IPNS :"), aurora.Cyan(repo.IPNS))
fmt.Println()
// Pull
_, err := gitPull(repo.UUID)
if err != nil {
return repo, err
}
// Remove old IPFS
ipfsClusterRm(repo.IPFS)
// Size
size, err := dirSize(dirHome + dirGit + "/" + repo.UUID)
if err != nil {
fmt.Println("Couldn't get the size of the git repository.")
fmt.Println(err.Error())
return repo, err
}
rmin := rmin(size)
rmax := rmax(size)
// Add new IPFS
out, err := ipfsClusterAdd(repo.URL, rmin, rmax, repo.UUID)
if err != nil {
return repo, err
}
repo.IPFS = strings.TrimSpace(string(out))
fmt.Println(aurora.Bold("IPFS :"), aurora.Cyan(repo.IPFS))
// IPNS
out, err = ipfsNamePublish(repo.Key, repo.IPFS)
if err != nil {
return repo, err
}
repo.IPNS = strings.TrimSpace(string(out))
fmt.Println(aurora.Bold("IPNS :"), aurora.Cyan(repo.IPNS))
err = dbSet(db, repo)
if err != nil {
fmt.Println("Couldn't save the updated repo.")
fmt.Println(err.Error())
return repo, err
}
fmt.Println("Saved", aurora.Blue(repo.URL).String()+".")
return repo, err
}
func onNewRepo(db *badger.DB, link string) (repo Repo, err error) {
// URL
repo.URL = strings.TrimSpace(link)
fmt.Println(aurora.Bold("URL :"), aurora.Blue(repo.URL))
if repo.URL == "" {
rmRepo(db, repo)
return repo, ErrNoURL
}
// UUID
buuid, err := uuid.NewRandom()
if err != nil {
fmt.Println("Couldn't generate a new UUID.")
fmt.Println(err.Error())
rmRepo(db, repo)
return
}
repo.UUID = strings.TrimSpace(buuid.String())
fmt.Println(aurora.Bold("UUID :"), repo.UUID)
// Clone
_, err = gitClone(repo.URL, repo.UUID)
if err != nil {
rmRepo(db, repo)
return
}
// Size
size, err := dirSize(dirHome + dirGit + "/" + repo.UUID)
if err != nil {
fmt.Println("Couldn't get the size of the git repository.")
fmt.Println(err.Error())
rmRepo(db, repo)
return
}
rmin := rmin(size)
rmax := rmax(size)
// IPFS-Cluster
out, err := ipfsClusterAdd(repo.URL, rmin, rmax, repo.UUID)
if err != nil {
rmRepo(db, repo)
return
}
repo.IPFS = strings.TrimSpace(string(out))
fmt.Println(aurora.Bold("IPFS :"), aurora.Cyan(repo.IPFS))
// Key
out, err = ipfsKeyGen(repo.URL)
if err != nil {
rmRepo(db, repo)
return
}
repo.Key = strings.TrimSpace(string(out))
fmt.Println(aurora.Bold("Key :"), repo.Key)
// IPNS
out, err = ipfsNamePublish(repo.Key, repo.IPFS)
if err != nil {
rmRepo(db, repo)
return
}
repo.IPNS = strings.TrimSpace(string(out))
fmt.Println(aurora.Bold("IPNS :"), aurora.Cyan(repo.IPNS))
// Save
err = dbSet(db, repo)
if err != nil {
fmt.Println("Couldn't save the newly created repo.")
fmt.Println(err.Error())
rmRepo(db, repo)
return
}
fmt.Println("Saved", aurora.Blue(repo.URL).String()+".")
return repo, err
}