-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatest-yaml.go
115 lines (103 loc) · 2.03 KB
/
latest-yaml.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
package main
import (
"crypto/sha512"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"time"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
func cmdLatestYAML() *cli.Command {
return &cli.Command{
Name: "latest-yaml",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "version",
Usage: "version",
},
&cli.StringFlag{
Name: "in",
Usage: "in",
Value: "release",
},
&cli.StringFlag{
Name: "out",
Usage: "out",
Value: "release",
},
&cli.StringFlag{
Name: "platform",
Usage: "platform",
Value: runtime.GOOS,
},
},
Action: func(c *cli.Context) error {
return latestYAML(c.String("platform"), c.String("version"), c.String("in"), c.String("out"))
},
}
}
type pkg struct {
In string
Out string
}
func latestYAML(platform string, version string, in string, out string) error {
if version == "" {
return errors.Errorf("no version specified")
}
var pkgs []pkg
switch platform {
case "darwin":
pkgs = []pkg{
pkg{
In: fmt.Sprintf("Keys-%s-mac.zip", version),
Out: "latest-mac.yml",
},
}
case "windows":
pkgs = []pkg{
pkg{
In: fmt.Sprintf("Keys-%s.msi", version),
Out: "latest-windows.yml",
},
}
case "linux":
pkgs = []pkg{
pkg{
In: fmt.Sprintf("Keys-%s.AppImage", version),
Out: "latest-linux.yml",
},
}
}
for _, pkg := range pkgs {
inPath := filepath.Join(in, pkg.In)
outPath := filepath.Join(out, pkg.Out)
f, err := os.Open(inPath)
if err != nil {
return err
}
defer f.Close()
hasher := sha512.New()
if _, err := io.Copy(hasher, f); err != nil {
log.Fatal(err)
}
encoded := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
sha512 := encoded
releaseDate := time.Now().Format(time.RFC3339)
s := fmt.Sprintf(`version: %s
path: %s
sha512: %s
releaseDate: '%s'
`, version, pkg.In, sha512, releaseDate)
log.Printf("Writing %s:\n%s\n", outPath, s)
if err := ioutil.WriteFile(outPath, []byte(s), 0644); err != nil {
return err
}
}
return nil
}