2023-11-08 18:31:34 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-10-19 12:58:00 +00:00
|
|
|
"context"
|
2023-11-08 18:31:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
|
2024-10-19 12:58:00 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db/client"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
|
2023-11-08 18:31:34 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var appShowCmd = &cobra.Command{
|
|
|
|
Use: "show [app_id]",
|
|
|
|
Short: "Display installed apps",
|
|
|
|
Long: `Display the configuration for the apps.
|
|
|
|
|
2024-08-10 14:27:49 +00:00
|
|
|
Optional parameters:
|
|
|
|
- app-id: id of the application to display. If empty, display the list of available apps instead`,
|
2023-11-08 18:31:34 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-10-19 12:58:00 +00:00
|
|
|
c := utils.InitConfig("")
|
|
|
|
logger.Init(c.LogLevel)
|
|
|
|
|
|
|
|
s, err := db.New(*c)
|
|
|
|
if err != nil {
|
|
|
|
utils.Failf("failed to init storage: %s", err.Error())
|
|
|
|
}
|
2023-11-08 18:31:34 +00:00
|
|
|
|
|
|
|
if len(args) > 0 {
|
2024-10-19 12:58:00 +00:00
|
|
|
showApp(args[0], s.ClientStorage())
|
2023-11-08 18:31:34 +00:00
|
|
|
} else {
|
2024-10-19 12:58:00 +00:00
|
|
|
listApps(s.ClientStorage())
|
2023-11-08 18:31:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-10-19 12:58:00 +00:00
|
|
|
func showApp(appID string, st client.ClientDB) {
|
|
|
|
appConfig, err := st.GetClientByID(context.Background(), appID)
|
2023-11-08 18:31:34 +00:00
|
|
|
if err != nil {
|
2024-10-19 12:58:00 +00:00
|
|
|
if errors.Is(err, client.ErrNotFound) {
|
2023-11-08 18:31:34 +00:00
|
|
|
utils.Failf("App with ID %s does not exist\n", appID)
|
|
|
|
}
|
|
|
|
utils.Failf("Failed to get config for app %s: %q\n", appID, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("App config:")
|
|
|
|
printProperty("Name", appConfig.Name, 1)
|
|
|
|
printProperty("ID", appConfig.ID, 1)
|
|
|
|
printProperty("Client secret", appConfig.Secret, 1)
|
|
|
|
printProperty("Redirect URIs", "", 1)
|
2024-10-19 12:58:00 +00:00
|
|
|
for _, uri := range appConfig.RedirectURIs() {
|
2023-11-08 18:31:34 +00:00
|
|
|
printProperty("", uri, 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-19 12:58:00 +00:00
|
|
|
func listApps(st client.ClientDB) {
|
|
|
|
apps, err := st.GetAllClients(context.Background())
|
2023-11-08 18:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Failf("Failed to list apps: %q\n", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(apps) == 0 {
|
|
|
|
fmt.Println("No app configured")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, b := range apps {
|
|
|
|
fmt.Printf("\t - %s: (%s)\n", b.ID, b.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
appCmd.AddCommand(appShowCmd)
|
|
|
|
}
|