97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
/*
|
|
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/services"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/services/backend"
|
|
"github.com/dexidp/dex/storage"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// backendShowCmd represents the show command for backends
|
|
var backendShowCmd = &cobra.Command{
|
|
Use: "show [backend_id]",
|
|
Short: "Display installed backends",
|
|
Long: `Display the configuration for the backends.
|
|
|
|
Pass the commands without arguments to display the list of currently installed backends
|
|
Pass the optional 'id' argument to display the configuration for this specific backend`,
|
|
Args: cobra.MaximumNArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
conf, err := config.New(configPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
s, err := services.InitStorage(conf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
showBackend(args[0], backend.New(s))
|
|
} else {
|
|
listBackends(backend.New(s))
|
|
}
|
|
},
|
|
}
|
|
|
|
func printProperty(key, value string) {
|
|
fmt.Printf("\t- %s: %s\n", key, value)
|
|
}
|
|
|
|
func showBackend(backendId string, backendService backend.Service) {
|
|
backendConfig, err := backendService.GetBackend(backendId)
|
|
if err != nil {
|
|
if errors.Is(err, storage.ErrNotFound) {
|
|
fmt.Fprintf(os.Stderr, "Backend with ID %s does not exist\n", backendId)
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Failed to get config for backend %s: %q\n", backendId, err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
fmt.Println("Backend config:")
|
|
printProperty("ID", backendConfig.Storage.ID)
|
|
printProperty("Name", backendConfig.Storage.Name)
|
|
printProperty("Issuer", backendConfig.OIDC.Issuer)
|
|
printProperty("Client ID", backendConfig.OIDC.ClientID)
|
|
printProperty("Client secret", backendConfig.OIDC.ClientSecret)
|
|
}
|
|
|
|
func listBackends(backendService backend.Service) {
|
|
backends, err := backendService.ListBackends()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Failed to list backends: %q\n", err.Error())
|
|
return
|
|
}
|
|
|
|
if len(backends) == 0 {
|
|
fmt.Println("No backend configured")
|
|
} else {
|
|
for _, b := range backends {
|
|
fmt.Printf("\t - backend %s: id %s issuer %s\n", b.Storage.Name, b.Storage.ID, b.OIDC.Issuer)
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
backendCmd.AddCommand(backendShowCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// backendShowCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// backendShowCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|