Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new locale config option #39

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ An example config for anonymizing a WordPress database is provided at [`config.e

```json
{
"locale": "en",
"patterns": [
{
"tableName": "wp_users",
Expand All @@ -101,6 +102,7 @@ An example config for anonymizing a WordPress database is provided at [`config.e

The config is composed of many objects in the `patterns` array:

- `locale`: A string declaring what locale the Faker library should use. This is optional and defaults to `en`.
- `patterns`: an array of objects defining what modifications should be made.
- `tableName`: the name of the table the data will be stored in (used to parse `INSERT` statements to determine if the query should be modified.). You can also use regex to identify the relevant tables, required for multisite compatibility e.g. `.*_comments`.
- `purge`: Optional `boolean` field, if set to `true` then any `INSERT` query matching the table name will be stripped out, avoiding accidentally including data that can't be anonymized in your final result.
Expand Down
16 changes: 13 additions & 3 deletions internal/anonymize/anonymize.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ func Start(version, commit, date string) {
// Parse flags for custom config file.
configFile, locale := flag.Parse(version, commit, date, config.ProcessName)

// Set the faker locale.
setFakerLocale(*locale)

// Parse config file.
config.ParseConfig(*configFile)

// Set the locale to be the flag value if it was passed.
if *locale != "" {
config.Locale = *locale
}

// If no locale is defined in the config file or flag, set it to English by default.
if config.Locale == "" {
config.Locale = "en"
}

// Set the faker locale.
setFakerLocale(config.Locale)

// Error if Stdin is the terminal instead of pipe.
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
Commit string
Date string
WD string
Locale string `json:"locale"`
Patterns []ConfigPattern `json:"patterns"`
}

Expand Down
1 change: 1 addition & 0 deletions internal/embed/files/config.default.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"locale": "en",
"patterns": [
{
"tableName": ".*_users",
Expand Down
2 changes: 1 addition & 1 deletion internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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.")
flagLocale := pflag.String("locale", "", "Locale for faker data.")
flagHelp := pflag.BoolP("help", "h", false, "")

pflag.Parse()
Expand Down
Loading