github.com/mg98/scriptup@v0.1.0/pkg/scriptup/config.go (about)

     1  package scriptup
     2  
     3  import (
     4  	"errors"
     5  	"github.com/mg98/scriptup/pkg/scriptup/migration_state"
     6  	"github.com/mg98/scriptup/pkg/scriptup/storage"
     7  	"gopkg.in/yaml.v3"
     8  	"log"
     9  	"os"
    10  )
    11  
    12  type Config struct {
    13  	FileDB       string `yaml:"file_db,omitempty"`
    14  	Dialect      string `yaml:"dialect"`
    15  	Host         string `yaml:"host"`
    16  	Port         int    `yaml:"port"`
    17  	User         string `yaml:"user"`
    18  	Password     string `yaml:"pass,omitempty"`
    19  	SSLMode      string `yaml:"sslmode,omitempty"`
    20  	DatabaseName string `yaml:"db_name"`
    21  	Table        string `yaml:"table"`
    22  	Directory    string `yaml:"dir"`
    23  	Executor     string `yaml:"executor"`
    24  }
    25  
    26  // GetConfig loads the config for the given environment from the yaml file.
    27  func GetConfig(env string) *Config {
    28  	yamlFile, err := os.ReadFile("./scriptup.yaml")
    29  	if errors.Is(err, os.ErrNotExist) {
    30  		yamlFile, err = os.ReadFile("./scriptup.yml")
    31  	}
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  	cfg := map[string]*Config{}
    36  	err = yaml.Unmarshal(yamlFile, cfg)
    37  	if err != nil {
    38  		log.Fatalf("Unmarshal: %v", err)
    39  	}
    40  	return cfg[env]
    41  }
    42  
    43  // InitMigrationState sets up the storage and the MigrationState according to the config.
    44  func (cfg *Config) InitMigrationState() (*migration_state.MigrationState, error) {
    45  	var s storage.Storage
    46  	if cfg.FileDB != "" {
    47  		s = storage.NewFileStorage(cfg.FileDB)
    48  	} else {
    49  		s = storage.NewSQLStorage(&storage.SQLConnectionDetails{
    50  			Dialect:      cfg.Dialect,
    51  			Host:         cfg.Host,
    52  			Port:         cfg.Port,
    53  			User:         cfg.User,
    54  			Password:     cfg.Password,
    55  			DatabaseName: cfg.DatabaseName,
    56  			TableName:    cfg.Table,
    57  			SSLMode:      cfg.SSLMode,
    58  		})
    59  		if err := s.Open(); err != nil {
    60  			return nil, err
    61  		}
    62  	}
    63  
    64  	return migration_state.New(cfg.Directory, s), nil
    65  }
    66  
    67  // InitStorage retrieves and opens the storage according to the configuration.
    68  func (cfg *Config) InitStorage() (storage.Storage, error) {
    69  	var s storage.Storage
    70  	if cfg.FileDB != "" {
    71  		s = storage.NewFileStorage(cfg.FileDB)
    72  	} else {
    73  		s = storage.NewSQLStorage(&storage.SQLConnectionDetails{
    74  			Dialect:      cfg.Dialect,
    75  			Host:         cfg.Host,
    76  			Port:         cfg.Port,
    77  			User:         cfg.User,
    78  			Password:     cfg.Password,
    79  			DatabaseName: cfg.DatabaseName,
    80  			TableName:    cfg.Table,
    81  			SSLMode:      cfg.SSLMode,
    82  		})
    83  		if err := s.Open(); err != nil {
    84  			return nil, err
    85  		}
    86  	}
    87  	return s, nil
    88  }