forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_pgp.go
113 lines (92 loc) · 2.27 KB
/
build_pgp.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"io/ioutil"
"os"
"text/template"
"github.com/elastic/beats/v7/licenses"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/packer"
)
var (
input string
output string
license string
)
func init() {
flag.StringVar(&input, "in", "", "Source of input. \"-\" means reading from stdin")
flag.StringVar(&output, "out", "-", "Output path. \"-\" means writing to stdout")
flag.StringVar(&license, "license", "Elastic", "License header for generated file.")
}
var tmplPgp = template.Must(template.New("pgp").Parse(`
{{ .License }}
// Code generated by x-pack/dev-tools/cmd/buildspec/buildPgp.go - DO NOT EDIT.
package release
import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/packer"
)
// pgp bytes is a packed in public gpg key
var pgpBytes []byte
func init() {
// Packed Files
{{ range $i, $f := .Files -}}
// {{ $f }}
{{ end -}}
pgpBytes = packer.MustUnpack("{{ .Pack }}")["GPG-KEY-elasticsearch"]
}
// PGP return pgpbytes and a flag describing whether or not no pgp is valid.
func PGP() (bool, []byte) {
return allowEmptyPgp == "true", pgpBytes
}
`))
func main() {
flag.Parse()
if len(input) == 0 {
fmt.Fprintln(os.Stderr, "Invalid input source")
os.Exit(1)
}
l, err := licenses.Find(license)
if err != nil {
fmt.Fprintf(os.Stderr, "problem to retrieve the license, error: %+v", err)
os.Exit(1)
return
}
data, err := gen(input, l)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while generating the file, err: %+v\n", err)
os.Exit(1)
}
if output == "-" {
os.Stdout.Write(data)
return
} else {
ioutil.WriteFile(output, data, 0640)
}
return
}
func gen(path string, l string) ([]byte, error) {
pack, files, err := packer.Pack(input)
if err != nil {
return nil, err
}
var buf bytes.Buffer
tmplPgp.Execute(&buf, struct {
Pack string
Files []string
License string
}{
Pack: pack,
Files: files,
License: l,
})
formatted, err := format.Source(buf.Bytes())
if err != nil {
return nil, err
}
return formatted, nil
}