package cmd import ( "errors" "fmt" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/services/backend" "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. Optional parameters: - app-id: id of the backend to display. If empty, display the list of available backends instead`, Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { s := utils.InitStorage(utils.InitConfig("")) if len(args) > 0 { showBackend(args[0], backend.New(s)) } else { listBackends(backend.New(s)) } }, } func showBackend(backendId string, backendService backend.Service) { backendConfig, err := backendService.GetBackend(backendId) if err != nil { if errors.Is(err, storage.ErrNotFound) { utils.Failf("Backend with ID %s does not exist\n", backendId) } utils.Failf("Failed to get config for backend %s: %q\n", backendId, err.Error()) } fmt.Println("Backend config:") printProperty("ID", backendConfig.ID) printProperty("Name", backendConfig.Name) printProperty("Issuer", backendConfig.Issuer) printProperty("Client ID", backendConfig.ClientID) printProperty("Client secret", backendConfig.ClientSecret) printProperty("Redirect URI", backendConfig.RedirectURI) } func listBackends(backendService backend.Service) { backends, err := backendService.ListBackends() 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 { fmt.Printf("\t - %s: (%s) - %s\n", b.ID, b.Name, b.Issuer) } } func init() { backendCmd.AddCommand(backendShowCmd) }