91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/evcc-io/evcc/util/config"
|
|
"github.com/evcc-io/evcc/util/templates"
|
|
"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)
|
|
}
|
|
|
|
// setup environment
|
|
if err := configureEnvironment(cmd, &conf); err != nil {
|
|
log.FATAL.Fatal(err)
|
|
}
|
|
|
|
if err := configureVehicles(conf.Vehicles, args...); err != nil {
|
|
log.FATAL.Fatal(err)
|
|
}
|
|
|
|
vehicles := config.Vehicles().Devices()
|
|
|
|
for _, dev := range vehicles {
|
|
vehicleConf := dev.Config()
|
|
|
|
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 "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)
|
|
}
|
|
}
|
|
}
|