forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheebus.go
66 lines (54 loc) · 1.39 KB
/
eebus.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 cmd
import (
"crypto/x509/pkix"
"os"
"text/template"
"github.com/Masterminds/sprig/v3"
certhelper "github.com/evcc-io/eebus/cert"
"github.com/evcc-io/evcc/server"
"github.com/spf13/cobra"
)
// teslaCmd represents the vehicle command
var eebusCmd = &cobra.Command{
Use: "eebus-cert",
Short: "Generate EEBUS certificate for using EEBUS compatible chargers",
Run: runEEBUSCert,
}
func init() {
rootCmd.AddCommand(eebusCmd)
}
const tmpl = `
Add the following to the evcc config file:
eebus:
certificate:
public: |
{{ .public | indent 6 }}
private: |
{{ .private | indent 6 }}
`
func generateEEBUSCert() {
details := server.EEBUSDetails
subject := pkix.Name{
CommonName: details.DeviceCode,
Country: []string{"DE"},
Organization: []string{details.BrandName},
}
cert, err := certhelper.CreateCertificate(true, subject)
if err != nil {
log.FATAL.Fatal("could not create certificate")
}
pubKey, privKey, err := certhelper.GetX509KeyPair(cert)
if err != nil {
log.FATAL.Fatal("could not process generated certificate")
}
t := template.Must(template.New("out").Funcs(template.FuncMap(sprig.FuncMap())).Parse(tmpl))
if err := t.Execute(os.Stdout, map[string]interface{}{
"public": pubKey,
"private": privKey,
}); err != nil {
log.FATAL.Fatal("rendering failed", err)
}
}
func runEEBUSCert(cmd *cobra.Command, args []string) {
generateEEBUSCert()
}