polycule-connect/polyculeconnect/cmd/backend/remove.go
Melora Hugues b0e0a19c97
Some checks failed
/ docker-build-only (push) Failing after 30s
/ go-test (push) Failing after 1m14s
Replace previous backend service with new storage (#48)
2024-08-16 13:49:59 +02:00

46 lines
1.1 KiB
Go

package cmd
import (
"context"
"errors"
"fmt"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
"github.com/dexidp/dex/storage"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
var backendRemoveCmd = &cobra.Command{
Use: "remove <backend_id>",
Short: "Remove a backend",
Long: `Remove the backend with the given ID from the database.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
removeBackend(args[0])
},
}
func removeBackend(backendIDStr string) {
backendID, err := uuid.Parse(backendIDStr)
if err != nil {
utils.Failf("Invalid UUID format: %s", err.Error())
}
s, err := db.New(*utils.InitConfig(""))
if err != nil {
utils.Failf("Failed to init storage: %s", err.Error())
}
if err := s.BackendStorage().DeleteBackend(context.Background(), backendID); err != nil {
if !errors.Is(err, storage.ErrNotFound) {
utils.Failf("Failed to remove backend: %s", err.Error())
}
}
fmt.Println("Backend deleted")
}
func init() {
backendCmd.AddCommand(backendRemoveCmd)
}