add config loaders
This commit is contained in:
parent
f4d9f834c3
commit
bec658a701
|
@ -1,40 +0,0 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Type string
|
||||
Dsn string
|
||||
}
|
||||
|
||||
type AnonymizationConfig struct {
|
||||
}
|
||||
|
||||
func LoadDatabaseConfig(c *cli.Context) (error, DatabaseConfig) {
|
||||
config := DatabaseConfig{
|
||||
Type: c.String("type"),
|
||||
Dsn: c.String("dsn"),
|
||||
}
|
||||
|
||||
if config.Type == "" {
|
||||
return errors.New("You must specify a database type"), config
|
||||
}
|
||||
|
||||
if config.Dsn == "" {
|
||||
return errors.New("You must specify a database DSN"), config
|
||||
}
|
||||
|
||||
return nil, config
|
||||
}
|
||||
|
||||
func LoadAnonymizationConfig(c *cli.Context) (error, AnonymizationConfig) {
|
||||
config := AnonymizationConfig{
|
||||
Type: c.String("type"),
|
||||
Dsn: c.String("dsn"),
|
||||
}
|
||||
|
||||
return nil, config
|
||||
}
|
37
config/database_config.go
Normal file
37
config/database_config.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Type string
|
||||
Dsn string
|
||||
}
|
||||
|
||||
func LoadDatabaseConfig(dsn string) (DatabaseConfig, error) {
|
||||
config := DatabaseConfig{}
|
||||
|
||||
if dsn == "" {
|
||||
return config, errors.New("[0001] You must specify a database DSN")
|
||||
}
|
||||
|
||||
elements := strings.Split(dsn, ":")
|
||||
|
||||
if len(elements) == 0 {
|
||||
return config, errors.New("[0002] Invalid DSN")
|
||||
}
|
||||
|
||||
if elements[0] != "mysql" && elements[0] != "postgres" {
|
||||
return config, errors.New("[0003] Unsupported connection type")
|
||||
}
|
||||
|
||||
dbType := elements[0]
|
||||
|
||||
config.Dsn = strings.Replace(dsn, fmt.Sprintf("%s://", dbType), "", 1)
|
||||
config.Type = elements[0]
|
||||
|
||||
return config, nil
|
||||
}
|
36
config/schema_config.go
Normal file
36
config/schema_config.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"gitnet.fr/deblan/database-anonymizer/logger"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
)
|
||||
|
||||
type SchemaConfigData struct {
|
||||
Query string `yaml:"query"`
|
||||
VirtualColumns map[string]string `yaml:"virtual_columns"`
|
||||
Generators map[string][]string `yaml:"generators"`
|
||||
Columns map[string]string `yaml:"columns"`
|
||||
}
|
||||
|
||||
type SchemaConfigRules struct {
|
||||
Columns map[string]string `yaml:"columns"`
|
||||
Generators map[string][]string `yaml:"generators"`
|
||||
Datas []SchemaConfigData `yaml:"datas"`
|
||||
}
|
||||
|
||||
type SchemaConfig struct {
|
||||
Rules SchemaConfigRules `yaml:rules`
|
||||
}
|
||||
|
||||
func LoadSchemaConfigFromFile(file string) (SchemaConfig, error) {
|
||||
value := SchemaConfig{}
|
||||
|
||||
data, err := os.ReadFile(file)
|
||||
logger.LogFatalExitIf(err)
|
||||
|
||||
err = yaml.Unmarshal(data, &value)
|
||||
logger.LogFatalExitIf(err)
|
||||
|
||||
return value, nil
|
||||
}
|
Loading…
Reference in a new issue