-
Notifications
You must be signed in to change notification settings - Fork 51
/
gen_installer_cmd.go
66 lines (59 loc) · 1.59 KB
/
gen_installer_cmd.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
package app
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"strings"
"text/template"
"github.com/cashapp/hermit"
"github.com/cashapp/hermit/errors"
)
var (
installerTemplateSource = hermit.InstallerTemplateSource
installerTemplate = template.Must(template.New("install.sh").Funcs(template.FuncMap{
"string": func(b []byte) string { return string(b) },
"words": func(s []string) string { return strings.Join(s, " ") },
}).Parse(installerTemplateSource))
)
type genInstallerCmd struct {
Dest string `required:"" placeholder:"FILE" help:"Where to write the installer script."`
}
type params struct {
DistURL string
InstallPaths []string
}
// GenInstaller generates an instaler script from the app configuration.
// It returns a byte slice of the generated installer script, its
// SHA-256 digest as a hexadecimal string, and any error encountered.
func GenInstaller(config Config) ([]byte, string, error) {
var b bytes.Buffer
p := params{
DistURL: config.BaseDistURL,
InstallPaths: config.InstallPaths,
}
err := installerTemplate.Execute(&b, p)
if err != nil {
return nil, "", errors.WithStack(err)
}
sha256sum := sha256.Sum256(b.Bytes())
return b.Bytes(), hex.EncodeToString(sha256sum[:]), nil
}
func (g *genInstallerCmd) Run(config Config) error {
w, err := os.Create(g.Dest)
if err != nil {
return errors.WithStack(err)
}
defer w.Close() // nolint
script, sum, err := GenInstaller(config)
if err != nil {
return errors.WithStack(err)
}
_, err = w.Write(script)
if err != nil {
return errors.WithStack(err)
}
fmt.Println(sum)
return nil
}