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 0
/
buildhash.go
72 lines (59 loc) · 1.87 KB
/
buildhash.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
package main
import (
"fmt"
"os/exec"
"github.com/logrusorgru/aurora"
)
// BuildHash is a build and its hash.
type BuildHash struct {
Build *Build
IPFS string
}
// Save the BuildHash to the database.
func (bh *BuildHash) Save() {
if bh.Build == nil {
fmt.Println("Build is null.")
return
}
// Insert
_, err := db.Exec("insert into `builds`(`device`, `date`, `datetime`, `filename`, `filepath`, `sha1`, `sha256`, `size`, `type`, `version`, `ipfs`) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", bh.Build.Device, bh.Build.Date.Time, bh.Build.Datetime.Time, bh.Build.Filename, bh.Build.Filepath, bh.Build.Sha1, bh.Build.Sha256, bh.Build.Size, bh.Build.Type, bh.Build.Version, bh.IPFS)
if err != nil {
fmt.Println("Couldn't save build", aurora.Green(bh.Build.Filename).String()+".")
fmt.Println(err.Error())
return
}
// Pin
news, err := getLatestBuildsFromDevice(bh)
if err != nil {
return
}
for _, new := range news {
go new.Pin()
}
// Unpin
olds, err := getOldBuildsFromDevice(bh)
if err != nil {
return
}
for _, old := range olds {
go old.Unpin()
}
}
// Pin a build to the local IPFS cluster.
func (bh *BuildHash) Pin() {
out, err := exec.Command("ipfs-cluster-ctl", "pin", "add", bh.IPFS, "--name", bh.Build.Filename, "--replication-min", bh.Build.RMin(), "--replication-max", bh.Build.RMax()).Output()
if err != nil {
fmt.Println("Failed to pin a build.")
fmt.Println(aurora.Bold("Command :"), "ipfs-cluster-ctl", "pin", "add", aurora.Cyan(bh.IPFS), "--name", aurora.Green(bh.Build.Filename), "--replication-min", bh.Build.RMin(), "--replication-max", bh.Build.RMax())
// Log the error from the command
ee, ok := err.(*exec.ExitError)
if ok {
fmt.Println(string(ee.Stderr))
}
}
fmt.Println(string(out))
}
// Unpin a build from the local IPFS cluster.
func (bh *BuildHash) Unpin() {
exec.Command("ipfs-cluster-ctl", "pin", "rm", bh.IPFS).Run()
}