From 265ef47f4149ae675a75a49636ae0be0b902aa81 Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Tue, 12 Mar 2024 22:07:35 -0500 Subject: [PATCH] feat: allow configuring registry URL/dir Fixes #99 --- cmd/cardano-up/context.go | 24 ++++-------------------- cmd/cardano-up/down.go | 8 +------- cmd/cardano-up/info.go | 8 +------- cmd/cardano-up/install.go | 7 +------ cmd/cardano-up/list.go | 13 ++----------- cmd/cardano-up/main.go | 23 +++++++++++++++++++++++ cmd/cardano-up/uninstall.go | 8 +------- cmd/cardano-up/up.go | 8 +------- cmd/cardano-up/upgrade.go | 8 +------- 9 files changed, 35 insertions(+), 72 deletions(-) diff --git a/cmd/cardano-up/context.go b/cmd/cardano-up/context.go index a4158d4..b934fa5 100644 --- a/cmd/cardano-up/context.go +++ b/cmd/cardano-up/context.go @@ -50,11 +50,7 @@ func contextListCommand() *cobra.Command { Use: "list", Short: "List available contexts", Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() activeContext, _ := pm.ActiveContext() contexts := pm.Contexts() slog.Info("Contexts (* is active):\n") @@ -106,11 +102,7 @@ func contextSelectCommand() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() if err := pm.SetActiveContext(args[0]); err != nil { slog.Error(fmt.Sprintf("failed to set active context: %s", err)) os.Exit(1) @@ -139,11 +131,7 @@ func contextCreateCommand() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() tmpContextName := args[0] tmpContext := pkgmgr.Context{ Description: contextFlags.description, @@ -174,11 +162,7 @@ func contextDeleteCommand() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() if err := pm.DeleteContext(args[0]); err != nil { slog.Error(fmt.Sprintf("failed to delete context: %s", err)) os.Exit(1) diff --git a/cmd/cardano-up/down.go b/cmd/cardano-up/down.go index 024fc05..391d1d8 100644 --- a/cmd/cardano-up/down.go +++ b/cmd/cardano-up/down.go @@ -1,11 +1,9 @@ package main import ( - "fmt" "log/slog" "os" - "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" ) @@ -15,11 +13,7 @@ func downCommand() *cobra.Command { Short: "Stops all Docker containers", Long: `Stops all running Docker containers for installed packages in the current context.`, RunE: func(cmd *cobra.Command, args []string) error { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() if err := pm.Down(); err != nil { slog.Error(err.Error()) os.Exit(1) diff --git a/cmd/cardano-up/info.go b/cmd/cardano-up/info.go index da2a8b0..cf57d2f 100644 --- a/cmd/cardano-up/info.go +++ b/cmd/cardano-up/info.go @@ -16,11 +16,9 @@ package main import ( "errors" - "fmt" "log/slog" "os" - "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" ) @@ -39,11 +37,7 @@ func infoCommand() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() if err := pm.Info(args[0]); err != nil { slog.Error(err.Error()) os.Exit(1) diff --git a/cmd/cardano-up/install.go b/cmd/cardano-up/install.go index 3a464c6..530bc47 100644 --- a/cmd/cardano-up/install.go +++ b/cmd/cardano-up/install.go @@ -20,7 +20,6 @@ import ( "log/slog" "os" - "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" ) @@ -47,11 +46,7 @@ func installCommand() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() activeContextName, activeContext := pm.ActiveContext() // Update context network if specified if installFlags.network != "" { diff --git a/cmd/cardano-up/list.go b/cmd/cardano-up/list.go index 9e790b9..f7babd9 100644 --- a/cmd/cardano-up/list.go +++ b/cmd/cardano-up/list.go @@ -17,7 +17,6 @@ package main import ( "fmt" "log/slog" - "os" "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" @@ -32,11 +31,7 @@ func listAvailableCommand() *cobra.Command { Use: "list-available", Short: "List available packages", Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() packages := pm.AvailablePackages() slog.Info("Available packages:\n") slog.Info( @@ -76,11 +71,7 @@ func listCommand() *cobra.Command { Use: "list", Short: "List installed packages", Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() activeContextName, _ := pm.ActiveContext() var packages []pkgmgr.InstalledPackage if listFlags.all { diff --git a/cmd/cardano-up/main.go b/cmd/cardano-up/main.go index 99f4f8c..05edb5b 100644 --- a/cmd/cardano-up/main.go +++ b/cmd/cardano-up/main.go @@ -15,10 +15,12 @@ package main import ( + "fmt" "log/slog" "os" "github.com/blinklabs-io/cardano-up/internal/consolelog" + "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" ) @@ -80,3 +82,24 @@ func main() { os.Exit(1) } } + +func createPackageManager() *pkgmgr.PackageManager { + cfg, err := pkgmgr.NewDefaultConfig() + if err != nil { + slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) + os.Exit(1) + } + // Allow setting registry URL/dir via env var + if url, ok := os.LookupEnv("REGISTRY_URL"); ok { + cfg.RegistryUrl = url + } + if dir, ok := os.LookupEnv("REGISTRY_DIR"); ok { + cfg.RegistryDir = dir + } + pm, err := pkgmgr.NewPackageManager(cfg) + if err != nil { + slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) + os.Exit(1) + } + return pm +} diff --git a/cmd/cardano-up/uninstall.go b/cmd/cardano-up/uninstall.go index ab61928..9452465 100644 --- a/cmd/cardano-up/uninstall.go +++ b/cmd/cardano-up/uninstall.go @@ -16,11 +16,9 @@ package main import ( "errors" - "fmt" "log/slog" "os" - "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" ) @@ -42,11 +40,7 @@ func uninstallCommand() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() // Uninstall package if err := pm.Uninstall(uninstallFlags.keepData, args[0]); err != nil { slog.Error(err.Error()) diff --git a/cmd/cardano-up/up.go b/cmd/cardano-up/up.go index 7c17ee7..8b6d4a8 100644 --- a/cmd/cardano-up/up.go +++ b/cmd/cardano-up/up.go @@ -1,11 +1,9 @@ package main import ( - "fmt" "log/slog" "os" - "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" ) @@ -15,11 +13,7 @@ func upCommand() *cobra.Command { Short: "Starts all Docker containers", Long: `Starts all stopped Docker containers for installed packages in the current context.`, RunE: func(cmd *cobra.Command, args []string) error { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() if err := pm.Up(); err != nil { slog.Error(err.Error()) os.Exit(1) diff --git a/cmd/cardano-up/upgrade.go b/cmd/cardano-up/upgrade.go index d1dad9e..8071a65 100644 --- a/cmd/cardano-up/upgrade.go +++ b/cmd/cardano-up/upgrade.go @@ -16,11 +16,9 @@ package main import ( "errors" - "fmt" "log/slog" "os" - "github.com/blinklabs-io/cardano-up/pkgmgr" "github.com/spf13/cobra" ) @@ -38,11 +36,7 @@ func upgradeCommand() *cobra.Command { return nil }, Run: func(cmd *cobra.Command, args []string) { - pm, err := pkgmgr.NewDefaultPackageManager() - if err != nil { - slog.Error(fmt.Sprintf("failed to create package manager: %s", err)) - os.Exit(1) - } + pm := createPackageManager() // Upgrade requested package if err := pm.Upgrade(args[0]); err != nil { slog.Error(err.Error())