diff --git a/README.md b/README.md index 6785192..cc013bb 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,25 @@ You may also pipe in the content of an existing SQL dump using `cat`: cat database.sql | anonymize-mysqldump --config config.json > anonymized.sql ``` +You can also define the locale for the fake data generation (defaults to `en`): + +```sh +cat database.sql | anonymize-mysqldump --config config.json --locale nb_no > anonymized.sql ``` -usage: anonymize-mysqldump [-h|--help] -c|--config "" - Reads SQL from STDIN and replaces content for - anonymity based on the provided config. +Here is the help command output as a guide. + +```sh +Usage: + anonymize-mysqldump [flags] -Arguments: +Flags: + --help -h Outputs help text and exits. + --config The path to a custom config file. + --locale The faker locale. - -h --help Print help information - -c --config Path to config.json +Config: + The anonymizer will use a default config suitable for WordPress, but you can override this by providing your own. ``` ## Installation diff --git a/internal/anonymize/anonymize.go b/internal/anonymize/anonymize.go index a277918..fbe97ae 100644 --- a/internal/anonymize/anonymize.go +++ b/internal/anonymize/anonymize.go @@ -16,6 +16,8 @@ import ( "github.com/DekodeInteraktiv/anonymize-mysqldump/internal/helpers" "github.com/xwb1989/sqlparser" + "syreclabs.com/go/faker" + "syreclabs.com/go/faker/locales" ) var ( @@ -32,7 +34,10 @@ func Start(version, commit, date string) { log.SetOutput(os.Stderr) // Parse flags for custom config file. - configFile := flag.Parse(version, commit, date, config.ProcessName) + configFile, locale := flag.Parse(version, commit, date, config.ProcessName) + + // Set the faker locale. + setFakerLocale(*locale) // Parse config file. config.ParseConfig(*configFile) @@ -56,7 +61,43 @@ For example: for line := range lines { fmt.Print(<-line) } +} +func setFakerLocale(localeFlag string) { + // Map string identifiers to their corresponding locale maps + availableLocales := map[string]*map[string]interface{}{ + "de_at": &locales.De_AT, + "de-ch": &locales.De_CH, + "de": &locales.De, + "en_au": &locales.En_AU, + "en_au_ocker": &locales.En_AU_OCKER, + "en_ca": &locales.En_CA, + "en_gb": &locales.En_GB, + "en_us": &locales.En_US, + "en": &locales.En, + "es": &locales.Es, + "fr": &locales.Fr, + "it": &locales.It, + "ja": &locales.Ja, + "ko": &locales.Ko, + "nb_no": &locales.Nb_NO, + "nl": &locales.Nl, + "pt_br": &locales.Pt_BR, + "ru": &locales.Ru, + "sk": &locales.Sk, + "sv": &locales.Sv, + "vi": &locales.Vi, + "zh_cn": &locales.Zh_CN, + "zh_tw": &locales.Zh_TW, + } + + // Set the locale based on the command-line argument + if locale, ok := availableLocales[localeFlag]; ok { + faker.Locale = *locale + } else { + fmt.Printf("Locale '%s' is not supported.\n", localeFlag) + os.Exit(2) + } } func setupAndProcessInput(config config.Config, input io.Reader) chan chan string { diff --git a/internal/config/config.go b/internal/config/config.go index 9fa923c..96a7b37 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,7 +2,6 @@ package config import ( "encoding/json" - "io/ioutil" "log" "os" "path/filepath" @@ -70,7 +69,7 @@ func (c *Config) ParseConfig(filepath string) { jsonConfig = []byte(embed.DefaultConfig) if filepath != "" { - jsonConfig, err = ioutil.ReadFile(filepath) + jsonConfig, err = os.ReadFile(filepath) if err != nil { log.Fatalf("Failed reading config file: %s", err) } diff --git a/internal/flag/flag.go b/internal/flag/flag.go index bd966b9..dd09eba 100644 --- a/internal/flag/flag.go +++ b/internal/flag/flag.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/pflag" ) -func Parse(version, commit, date, processName string) *string { +func Parse(version, commit, date, processName string) (*string, *string) { helpText := `Anonymize MySQLDump is a database anonymization tool. Version: ` + version + ` Commit: ` + commit + ` @@ -18,12 +18,14 @@ Usage: Flags: --help -h Outputs help text and exits. - --config -c The path to a custom config file. + --config The path to a custom config file. + --locale The faker locale. Config: The anonymizer will use a default config suitable for WordPress, but you can override this by providing your own.` flagConfigFile := pflag.String("config", "", "Path to config file.") + flagLocale := pflag.String("locale", "en", "Locale for faker data.") flagHelp := pflag.BoolP("help", "h", false, "") pflag.Parse() @@ -33,5 +35,5 @@ Config: os.Exit(1) } - return flagConfigFile + return flagConfigFile, flagLocale }