forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
100 lines (84 loc) · 2.33 KB
/
token.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
package cmd
import (
"fmt"
"slices"
"strings"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/templates"
"github.com/samber/lo"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
)
// tokenCmd represents the vehicle command
var tokenCmd = &cobra.Command{
Use: "token [vehicle name]",
Short: "Generate token credentials",
Run: runToken,
}
func init() {
rootCmd.AddCommand(tokenCmd)
}
func runToken(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}
var vehicleConf config.Named
if len(conf.Vehicles) == 1 {
vehicleConf = conf.Vehicles[0]
} else if len(args) == 1 {
idx := slices.IndexFunc(conf.Vehicles, func(v config.Named) bool {
return strings.EqualFold(v.Name, args[0])
})
if idx >= 0 {
vehicleConf = conf.Vehicles[idx]
}
}
if vehicleConf.Name == "" {
vehicles := lo.Map(conf.Vehicles, func(v config.Named, _ int) string {
return v.Name
})
log.FATAL.Fatalf("vehicle not found, have %v", vehicles)
}
var token *oauth2.Token
var err error
isTemplate := strings.ToLower(vehicleConf.Type) == "template"
if isTemplate {
instance, err := templates.RenderInstance(templates.Vehicle, vehicleConf.Other)
if err != nil {
log.FATAL.Fatalf("rendering template failed: %v", err)
}
vehicleConf.Type = instance.Type
vehicleConf.Other = instance.Other
}
typ := strings.ToLower(vehicleConf.Type)
switch typ {
case "mercedes":
token, err = mercedesToken()
case "ford", "ford-connect":
token, err = fordConnectToken(vehicleConf)
case "tronity":
token, err = tronityToken(conf, vehicleConf)
case "citroen", "ds", "opel", "peugeot":
token, err = psaToken(typ)
default:
log.FATAL.Fatalf("vehicle type '%s' does not support token authentication", vehicleConf.Type)
}
if err != nil {
log.FATAL.Fatal(err)
}
fmt.Println()
fmt.Println("Add the following tokens to the vehicle config:")
fmt.Println()
if isTemplate {
fmt.Println(" type: template")
fmt.Println(" template:", typ)
fmt.Println(" accesstoken:", token.AccessToken)
fmt.Println(" refreshtoken:", token.RefreshToken)
} else {
fmt.Println(" type:", typ)
fmt.Println(" tokens:")
fmt.Println(" access:", token.AccessToken)
fmt.Println(" refresh:", token.RefreshToken)
}
}