Skip to content

Commit

Permalink
Feature/Allow Setting Faker Locale (#33)
Browse files Browse the repository at this point in the history
* Replace deprecated import

* Add ability to set the faker package locale

* Formatting and documentation
  • Loading branch information
PeterBooker authored Sep 21, 2024
1 parent 3a2e27d commit 9daf837
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<value>"

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
Expand Down
43 changes: 42 additions & 1 deletion internal/anonymize/anonymize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 5 additions & 3 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + `
Expand All @@ -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()
Expand All @@ -33,5 +35,5 @@ Config:
os.Exit(1)
}

return flagConfigFile
return flagConfigFile, flagLocale
}

0 comments on commit 9daf837

Please sign in to comment.