Skip to content

Commit

Permalink
fix: provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed Dec 1, 2024
1 parent 8a53849 commit 2496b63
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 23 deletions.
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"slices"
"strings"

Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/amigoconfig/cli_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 != "" {
Expand Down
1 change: 1 addition & 0 deletions pkg/amigoconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func NewConfig() *Config {
}

type RootConfig struct {
CurrentContext string
AmigoFolderPath string
DSN string
JSON bool
Expand Down
10 changes: 4 additions & 6 deletions pkg/entrypoint/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"gopkg.in/yaml.v3"
)

const contextsFileName = "contexts.yml"

// contextCmd represents the context command
var contextCmd = &cobra.Command{
Use: "context",
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/entrypoint/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions pkg/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ 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
}

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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/entrypoint/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pkg/entrypoint/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/entrypoint/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down

0 comments on commit 2496b63

Please sign in to comment.