2023-10-29 12:31:52 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-08-16 11:49:59 +00:00
|
|
|
"context"
|
2023-10-29 12:31:52 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
|
2024-08-16 11:49:59 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db/backend"
|
2023-10-29 12:31:52 +00:00
|
|
|
"github.com/dexidp/dex/storage"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var backendShowCmd = &cobra.Command{
|
|
|
|
Use: "show [backend_id]",
|
|
|
|
Short: "Display installed backends",
|
|
|
|
Long: `Display the configuration for the backends.
|
|
|
|
|
2024-08-10 14:27:49 +00:00
|
|
|
Optional parameters:
|
|
|
|
- app-id: id of the backend to display. If empty, display the list of available backends instead`,
|
2023-10-29 12:31:52 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-08-16 11:49:59 +00:00
|
|
|
s, err := db.New(*utils.InitConfig(""))
|
|
|
|
if err != nil {
|
|
|
|
utils.Failf("Failed to init storage: %s", err.Error())
|
|
|
|
}
|
2023-10-29 12:31:52 +00:00
|
|
|
|
|
|
|
if len(args) > 0 {
|
2024-08-16 11:49:59 +00:00
|
|
|
showBackend(args[0], s.BackendStorage())
|
2023-10-29 12:31:52 +00:00
|
|
|
} else {
|
2024-08-16 11:49:59 +00:00
|
|
|
listBackends(s.BackendStorage())
|
2023-10-29 12:31:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-08-16 11:49:59 +00:00
|
|
|
func showBackend(backendName string, backendService backend.BackendDB) {
|
|
|
|
backendConfig, err := backendService.GetBackendByName(context.Background(), backendName)
|
2023-10-29 12:31:52 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, storage.ErrNotFound) {
|
2024-08-16 11:49:59 +00:00
|
|
|
utils.Failf("Backend with name %s does not exist\n", backendName)
|
2023-10-29 12:31:52 +00:00
|
|
|
}
|
2024-08-16 11:49:59 +00:00
|
|
|
utils.Failf("Failed to get config for backend %s: %q\n", backendName, err.Error())
|
2023-10-29 12:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Backend config:")
|
2024-08-16 11:49:59 +00:00
|
|
|
printProperty("ID", backendConfig.ID.String())
|
2023-10-29 12:31:52 +00:00
|
|
|
printProperty("Name", backendConfig.Name)
|
2024-10-06 20:11:58 +00:00
|
|
|
printProperty("Issuer", backendConfig.Config.Issuer)
|
|
|
|
printProperty("Client ID", backendConfig.Config.ClientID)
|
|
|
|
printProperty("Client secret", backendConfig.Config.ClientSecret)
|
|
|
|
printProperty("Redirect URI", backendConfig.Config.RedirectURI)
|
2023-10-29 12:31:52 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 11:49:59 +00:00
|
|
|
func listBackends(backendStorage backend.BackendDB) {
|
|
|
|
backends, err := backendStorage.GetAllBackends(context.Background())
|
2023-10-29 12:31:52 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Failf("Failed to list backends: %q\n", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(backends) == 0 {
|
|
|
|
fmt.Println("No backend configured")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, b := range backends {
|
2024-10-06 20:11:58 +00:00
|
|
|
fmt.Printf("\t - %s: (%s) - %s\n", b.ID, b.Name, b.Config.Issuer)
|
2023-10-29 12:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
backendCmd.AddCommand(backendShowCmd)
|
|
|
|
}
|