From 2496b6381ad413ee76601c24402b348cad023917 Mon Sep 17 00:00:00 2001 From: Alexis Viscogliosi Date: Sun, 1 Dec 2024 14:53:30 +0100 Subject: [PATCH] fix: provider --- cmd/init.go | 6 +++--- cmd/root.go | 12 ++++++++++++ pkg/amigoconfig/cli_context.go | 5 +++++ pkg/amigoconfig/config.go | 1 + pkg/entrypoint/context.go | 10 ++++------ pkg/entrypoint/create.go | 4 ++-- pkg/entrypoint/main.go | 7 +++---- pkg/entrypoint/migration.go | 8 ++++---- pkg/entrypoint/root.go | 3 +-- pkg/entrypoint/status.go | 5 +++-- 10 files changed, 38 insertions(+), 23 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 4dd83f6..ad0bd6c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -91,15 +91,15 @@ func executeInit( return err } - openFile, err := utils.CreateOrOpenFile(path.Join(amigoFolder, "contexts.yaml")) + openFile, err := utils.CreateOrOpenFile(path.Join(amigoFolder, amigoconfig.FileName)) if err != nil { - return fmt.Errorf("unable to open contexts.yaml file: %w", err) + return fmt.Errorf("unable to open config file: %w", err) } defer openFile.Close() _, err = openFile.WriteString(string(out)) if err != nil { - return fmt.Errorf("unable to write contexts.yaml file: %w", err) + return fmt.Errorf("unable to write config file: %w", err) } return nil diff --git a/cmd/root.go b/cmd/root.go index aee511e..ffebd03 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path" + "path/filepath" "slices" "strings" @@ -33,6 +34,17 @@ var rootCmd = &cobra.Command{ mainBinaryPath := path.Join(defaultAmigoFolder, "main") migrationFolder := amigoconfig.DefaultMigrationFolder + config, err := amigoconfig.LoadYamlConfig(filepath.Join(defaultAmigoFolder, amigoconfig.FileName)) + if err == nil { + currentConfig := config.Contexts[config.CurrentContext] + if currentConfig.SchemaVersionTable != "" { + schemaVersionTable = currentConfig.SchemaVersionTable + } + if currentConfig.MigrationFolder != "" { + migrationFolder = currentConfig.MigrationFolder + } + } + if slices.Contains(args, "init") { return executeInit(mainFilePath, defaultAmigoFolder, schemaVersionTable, migrationFolder) } diff --git a/pkg/amigoconfig/cli_context.go b/pkg/amigoconfig/cli_context.go index e11e884..981f9ec 100644 --- a/pkg/amigoconfig/cli_context.go +++ b/pkg/amigoconfig/cli_context.go @@ -8,6 +8,8 @@ import ( "gopkg.in/yaml.v3" ) +var FileName = "config.yml" + type YamlConfig struct { ShellPath string `yaml:"shell-path"` Debug bool `yaml:"debug"` @@ -89,6 +91,9 @@ func (c *Config) OverrideWithYamlConfig(yaml *YamlConfig) { if yaml.ShowSQLSyntaxHighlighting { c.RootConfig.ShowSQLSyntaxHighlighting = yaml.ShowSQLSyntaxHighlighting } + if yaml.CurrentContext != "" { + c.RootConfig.CurrentContext = yaml.CurrentContext + } // Override per-driver config values if context.SchemaVersionTable != "" { diff --git a/pkg/amigoconfig/config.go b/pkg/amigoconfig/config.go index 2eb157c..6136df0 100644 --- a/pkg/amigoconfig/config.go +++ b/pkg/amigoconfig/config.go @@ -60,6 +60,7 @@ func NewConfig() *Config { } type RootConfig struct { + CurrentContext string AmigoFolderPath string DSN string JSON bool diff --git a/pkg/entrypoint/context.go b/pkg/entrypoint/context.go index 5cf1b18..8760967 100644 --- a/pkg/entrypoint/context.go +++ b/pkg/entrypoint/context.go @@ -13,8 +13,6 @@ import ( "gopkg.in/yaml.v3" ) -const contextsFileName = "contexts.yml" - // contextCmd represents the context command var contextCmd = &cobra.Command{ Use: "context", @@ -28,7 +26,7 @@ This command will create a file $amigo_folder/context.yaml with the content: dsn: "postgres://user:password@host:port/dbname?sslmode=disable" `, Run: wrapCobraFunc(func(cmd *cobra.Command, a amigo.Amigo, args []string) error { - content, err := utils.GetFileContent(filepath.Join(a.Config.AmigoFolderPath, contextsFileName)) + content, err := utils.GetFileContent(filepath.Join(a.Config.AmigoFolderPath, amigoconfig.FileName)) if err != nil { return fmt.Errorf("unable to read contexts file: %w", err) } @@ -43,7 +41,7 @@ var ContextSetCmd = &cobra.Command{ Use: "set", Short: "Set the current context", Run: wrapCobraFunc(func(cmd *cobra.Command, a amigo.Amigo, args []string) error { - yamlConfig, err := amigoconfig.LoadYamlConfig(filepath.Join(a.Config.AmigoFolderPath, contextsFileName)) + yamlConfig, err := amigoconfig.LoadYamlConfig(filepath.Join(a.Config.AmigoFolderPath, amigoconfig.FileName)) if err != nil { return fmt.Errorf("unable to read contexts file: %w", err) } @@ -58,7 +56,7 @@ var ContextSetCmd = &cobra.Command{ yamlConfig.CurrentContext = args[0] - file, err := utils.CreateOrOpenFile(filepath.Join(a.Config.AmigoFolderPath, contextsFileName)) + file, err := utils.CreateOrOpenFile(filepath.Join(a.Config.AmigoFolderPath, amigoconfig.FileName)) if err != nil { return fmt.Errorf("unable to open contexts file: %w", err) } @@ -84,7 +82,7 @@ var ContextSetCmd = &cobra.Command{ return fmt.Errorf("unable to write contexts file: %w", err) } - logger.Info(events.FileModifiedEvent{FileName: filepath.Join(a.Config.AmigoFolderPath, contextsFileName)}) + logger.Info(events.FileModifiedEvent{FileName: filepath.Join(a.Config.AmigoFolderPath, amigoconfig.FileName)}) logger.Info(events.MessageEvent{Message: "context set to " + args[0]}) return nil diff --git a/pkg/entrypoint/create.go b/pkg/entrypoint/create.go index 3f468a2..2826a6e 100644 --- a/pkg/entrypoint/create.go +++ b/pkg/entrypoint/create.go @@ -90,9 +90,9 @@ var createCmd = &cobra.Command{ logger.Info(events.FileModifiedEvent{FileName: path.Join(config.MigrationFolder, migrationsFile)}) if config.Create.Skip { - db, err := database(*am.Config) + db, _, err := provider(*am.Config) if err != nil { - return fmt.Errorf("unable to get database: %w", err) + return fmt.Errorf("unable to get needed resources from provider: %w", err) } ctx, cancelFunc := context.WithTimeout(context.Background(), am.Config.Migration.Timeout) diff --git a/pkg/entrypoint/main.go b/pkg/entrypoint/main.go index f1183b5..0337321 100644 --- a/pkg/entrypoint/main.go +++ b/pkg/entrypoint/main.go @@ -8,7 +8,7 @@ import ( "github.com/alexisvisco/amigo/pkg/schema" ) -type DatabaseProvider func(cfg amigoconfig.Config) (*sql.DB, error) +type Provider func(cfg amigoconfig.Config) (*sql.DB, []schema.Migration, error) type MainOptions struct { CustomAmigo func(a *amigo.Amigo) amigo.Amigo @@ -16,9 +16,8 @@ type MainOptions struct { type MainOptFn func(options *MainOptions) -func Main(db DatabaseProvider, migrationsList []schema.Migration, opts ...MainOptFn) { - database = db - migrations = migrationsList +func Main(resourceProvider Provider, opts ...MainOptFn) { + provider = resourceProvider options := &MainOptions{} for _, opt := range opts { diff --git a/pkg/entrypoint/migration.go b/pkg/entrypoint/migration.go index 8203c74..06e5651 100644 --- a/pkg/entrypoint/migration.go +++ b/pkg/entrypoint/migration.go @@ -20,9 +20,9 @@ var migrateCmd = &cobra.Command{ return err } - db, err := database(*am.Config) + db, migrations, err := provider(*am.Config) if err != nil { - return fmt.Errorf("unable to get database: %w", err) + return fmt.Errorf("unable to get provided resources from main: %w", err) } ctx, cancelFunc := context.WithTimeout(context.Background(), am.Config.Migration.Timeout) @@ -53,9 +53,9 @@ var rollbackCmd = &cobra.Command{ return err } - db, err := database(*am.Config) + db, migrations, err := provider(*am.Config) if err != nil { - return fmt.Errorf("unable to get database: %w", err) + return fmt.Errorf("unable to get provided resources from main: %w", err) } ctx, cancelFunc := context.WithTimeout(context.Background(), am.Config.Migration.Timeout) diff --git a/pkg/entrypoint/root.go b/pkg/entrypoint/root.go index 22372b6..8fd2898 100644 --- a/pkg/entrypoint/root.go +++ b/pkg/entrypoint/root.go @@ -16,8 +16,7 @@ import ( var ( config = amigoconfig.NewConfig() - database func(cfg amigoconfig.Config) (*sql.DB, error) - migrations []schema.Migration + provider func(cfg amigoconfig.Config) (*sql.DB, []schema.Migration, error) customAmigoFn func(a *amigo.Amigo) *amigo.Amigo migrationsFile = "migrations.go" ) diff --git a/pkg/entrypoint/status.go b/pkg/entrypoint/status.go index 50ae3ee..b481b8d 100644 --- a/pkg/entrypoint/status.go +++ b/pkg/entrypoint/status.go @@ -22,9 +22,9 @@ var statusCmd = &cobra.Command{ return err } - db, err := database(*am.Config) + db, migrations, err := provider(*am.Config) if err != nil { - return fmt.Errorf("unable to get database: %w", err) + return fmt.Errorf("unable to get provided resources from main: %w", err) } ctx, cancelFunc := context.WithTimeout(context.Background(), am.Config.Migration.Timeout) @@ -47,6 +47,7 @@ var statusCmd = &cobra.Command{ // show status of 10 last migrations b := &strings.Builder{} tw := tabwriter.NewWriter(b, 2, 0, 1, ' ', 0) + defaultMigrations := sliceArrayOrDefault(migrations, 10) for i, m := range defaultMigrations { key := fmt.Sprintf("(%s) %s", m.Date().UTC().Format(utils.FormatTime), m.Name())