diff --git a/config/config.go b/config/config.go deleted file mode 100644 index bef73c6..0000000 --- a/config/config.go +++ /dev/null @@ -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 -} diff --git a/config/database_config.go b/config/database_config.go new file mode 100644 index 0000000..6cd37f2 --- /dev/null +++ b/config/database_config.go @@ -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 +} diff --git a/config/schema_config.go b/config/schema_config.go new file mode 100644 index 0000000..c02b2f5 --- /dev/null +++ b/config/schema_config.go @@ -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 +}