From fa20b9583b89a4199b265f1d39b59af2d14d6cdf Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sun, 29 Oct 2023 13:32:09 +0100 Subject: [PATCH] chore: refactor serve command --- polyculeconnect/cmd/{ => serve}/serve.go | 30 +++++++----------------- polyculeconnect/main.go | 1 + polyculeconnect/services/storage.go | 9 +++++++ 3 files changed, 18 insertions(+), 22 deletions(-) rename polyculeconnect/cmd/{ => serve}/serve.go (84%) diff --git a/polyculeconnect/cmd/serve.go b/polyculeconnect/cmd/serve/serve.go similarity index 84% rename from polyculeconnect/cmd/serve.go rename to polyculeconnect/cmd/serve/serve.go index c12c7fe..57badd8 100644 --- a/polyculeconnect/cmd/serve.go +++ b/polyculeconnect/cmd/serve/serve.go @@ -1,4 +1,4 @@ -package cmd +package serve import ( "context" @@ -6,12 +6,12 @@ import ( "os/signal" "time" - "git.faercol.me/faercol/polyculeconnect/polyculeconnect/config" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/connector" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/server" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/services" - "github.com/dexidp/dex/connector/oidc" dex_server "github.com/dexidp/dex/server" "github.com/prometheus/client_golang/prometheus" "github.com/spf13/cobra" @@ -35,18 +35,11 @@ variables`, func serve() { mainCtx, cancel := context.WithCancel(context.Background()) - conf, err := config.New(configPath) - if err != nil { - panic(err) - } - + conf := utils.InitConfig(configPath) logger.Init(conf.LogLevel) logger.L.Infof("Initialized logger with level %v", conf.LogLevel) - storageType, err := services.InitStorage(conf) - if err != nil { - logger.L.Fatalf("Failed to initialize storage backend: %s", err.Error()) - } + storageType := utils.InitStorage(conf) logger.L.Infof("Initialized storage backend %q", conf.StorageType) dexConf := dex_server.Config{ Web: dex_server.WebConfig{ @@ -64,7 +57,7 @@ func serve() { logger.L.Info("Initializing authentication backends") - dex_server.ConnectorsConfig["refuseAll"] = func() dex_server.ConnectorConfig { return new(connector.RefuseAllConfig) } + dex_server.ConnectorsConfig[connector.TypeRefuseAll] = func() dex_server.ConnectorConfig { return new(connector.RefuseAllConfig) } connectors, err := dexConf.Storage.ListConnectors() if err != nil { logger.L.Fatalf("Failed to get existing connectors: %s", err.Error()) @@ -74,14 +67,7 @@ func serve() { connectorIDs = append(connectorIDs, conn.ID) } - backend := config.BackendConfig{ - Config: &oidc.Config{}, - Name: "RefuseAll", - ID: "null", - Type: "refuseAll", - } - - if err := services.CreateConnector(&backend, &dexConf, connectorIDs); err != nil { + if err := services.AddDefaultBackend(storageType); err != nil { logger.L.Errorf("Failed to add connector for backend RefuseAll to stage: %s", err.Error()) } @@ -140,7 +126,7 @@ func serve() { } func init() { - rootCmd.AddCommand(serveCmd) + cmd.RootCmd.AddCommand(serveCmd) // Here you will define your flags and configuration settings. diff --git a/polyculeconnect/main.go b/polyculeconnect/main.go index d7b8e78..51395ff 100644 --- a/polyculeconnect/main.go +++ b/polyculeconnect/main.go @@ -3,6 +3,7 @@ package main import ( "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd" _ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/backend" + _ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/serve" ) func main() { diff --git a/polyculeconnect/services/storage.go b/polyculeconnect/services/storage.go index df6160a..e85d1a6 100644 --- a/polyculeconnect/services/storage.go +++ b/polyculeconnect/services/storage.go @@ -1,9 +1,11 @@ package services import ( + "errors" "fmt" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/config" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/connector" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger" "github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage/memory" @@ -29,3 +31,10 @@ func InitStorage(conf *config.AppConfig) (storage.Storage, error) { } return storageType, nil } + +func AddDefaultBackend(s storage.Storage) error { + if err := s.CreateConnector(connector.RefuseAllConnectorConfig); err != nil && !errors.Is(err, storage.ErrAlreadyExists) { + return fmt.Errorf("failed to add default backend: %w", err) + } + return nil +}