/
get.go
108 lines (89 loc) · 2.53 KB
/
get.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
package cmd
import (
"context"
"fmt"
"io"
"os"
"github.com/google/go-github/github"
"github.com/juju/errors"
"github.com/spf13/cobra"
)
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get",
}
func init() {
RootCmd.AddCommand(getCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
var (
tag string
dst string
)
getCmd.Flags().StringVarP(&tag, "tag", "", "", "tag name of release")
getCmd.Flags().StringVarP(&dst, "dst", "d", "", "dst dir")
getCmd.Run = func(cmd *cobra.Command, args []string) {
dst = defaultValue(dst, IsEmptyString, ".").(string)
passphare, err := getPassphare()
if err != nil {
packetCmd.Usage()
exit(1, "require passphare\n", errors.ErrorStack(errors.Trace(err)))
}
d := newDataboard()
// get release assets by tag or latest release assets
var (
ctx = context.Background()
release *github.RepositoryRelease
)
if tag != "" {
release, err = d.GetReleaseByTag(ctx, tag)
if err != nil {
exit(2, errors.ErrorStack(errors.Trace(err)))
}
} else { // latest tag
release, err = d.GetLatestRelease(ctx)
if err != nil {
exit(3, errors.ErrorStack(errors.Trace(err)))
}
tag = release.GetTagName()
}
// download asset named "data.tar.gz.gpg"
var asset *github.ReleaseAsset
for _, a := range release.Assets {
if a.GetName() == "data.tar.gz.gpg" {
asset = &a
}
}
if asset == nil {
exitf(4, "no file named data.tar.gz.gpg in release tag %q", tag)
}
tmpdir := fmt.Sprintf("/tmp/databoard-%d", os.Getpid())
os.Mkdir(tmpdir, 0700)
defer os.RemoveAll(tmpdir)
resp, err := getHttpClient().Get(asset.GetBrowserDownloadURL())
if err != nil {
exit(5, errors.ErrorStack(errors.Trace(err)))
}
defer resp.Body.Close()
srcfile := tmpdir + "/data.tar.gz.gpg"
out, err := os.OpenFile(srcfile, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
exit(6, errors.ErrorStack(errors.Trace(err)))
}
_, err = io.Copy(out, resp.Body)
if err != nil {
exit(7, errors.ErrorStack(errors.Trace(err)))
}
out.Close()
// unpack "data.tar.gz.gpg" to dst dir
err = Unpack(srcfile, passphare, dst)
if err != nil {
exit(8, errors.ErrorStack(errors.Trace(err)))
}
}
}