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

Task/Preset Configs #14

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions internal/anonymize/anonymize.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,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, presets := flag.Parse(version, commit, date, config.ProcessName)

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

// Error if Stdin is the terminal instead of pipe.
stat, _ := os.Stdin.Stat()
Expand Down
3 changes: 2 additions & 1 deletion internal/anonymize/anonymize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func init() {
faker.Seed(432)

jsonConfig = *config.New("", "", "")
jsonConfig.ParseConfig("")
presets := make([]string, 0)
jsonConfig.ParseConfig("", presets)

// Get map of faker helper functions.
transformationFunctionMap = helpers.GetFakerFuncs()
Expand Down
42 changes: 35 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,20 @@ func New(version, commit, date string) *Config {
}

// ParseConfig parses a default or user provided config file.
func (c *Config) ParseConfig(filepath string) {
func (c *Config) ParseConfig(filepath string, presets []string) {
var jsonConfig []byte
var defaultConfig []byte
var jsonParser *json.Decoder
var jsonReader *strings.Reader
var err error

jsonConfig = []byte(embed.DefaultConfig)
// Load default config.
defaultConfig, err = embed.Content.ReadFile("files/default.json")
if err != nil {
log.Fatalln("Failed reading default config.")
}

jsonConfig = []byte(defaultConfig)

if filepath != "" {
jsonConfig, err = ioutil.ReadFile(filepath)
Expand All @@ -76,12 +85,31 @@ func (c *Config) ParseConfig(filepath string) {
}
}

jsonReader := strings.NewReader(string(jsonConfig))
jsonParser := json.NewDecoder(jsonReader)
jsonReader = strings.NewReader(string(jsonConfig))
jsonParser = json.NewDecoder(jsonReader)
err = jsonParser.Decode(c)

// Make sure the JSON read is valid.
if err != nil {
log.Fatalf("JSON file not valid!")
log.Fatalln("JSON file not valid!")
}

if len(presets) != 0 {
var presetConfig []byte
for _, preset := range presets {
presetConfig, err = embed.Content.ReadFile("files/config." + preset + ".json")
if err != nil {
log.Fatalf("No preset config found for: %s.", preset)
}

tempConfig := &Config{}

jsonReader = strings.NewReader(string(presetConfig))
jsonParser = json.NewDecoder(jsonReader)
err = jsonParser.Decode(tempConfig)
if err != nil {
log.Fatalln("JSON file not valid!")
}

c.Patterns = append(c.Patterns, tempConfig.Patterns...)
}
}
}
8 changes: 4 additions & 4 deletions internal/embed/embed.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package embed

import (
_ "embed"
"embed"
)

//go:embed files/config.default.json
// DefaultConfig contains the contents of the default config file.
var DefaultConfig string
//go:embed files/*
// Content contains a filesystem of config files.
var Content embed.FS
204 changes: 204 additions & 0 deletions internal/embed/files/config.give.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
{
"patterns": [
{
"tableName": ".*_give_donationmeta",
"fields": [
{
"field": "meta_value",
"position": 4,
"type": "firstName",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_first_name"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "lastName",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_last_name"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "email",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_payment_donor_email"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "ipv4",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_payment_donor_ip"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "addressStreet",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_address1"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "addressStreet",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_address2"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "addressCity",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_city"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "addressPostCode",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_zip"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "addressState",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_state"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "addressCountry",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_billing_country"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "companyName",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donation_company"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "shortString",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_payment_transaction_id"
}
]
}
]
},
{
"tableName": ".*_give_donormeta",
"fields": [
{
"field": "meta_value",
"position": 4,
"type": "firstName",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_first_name"
}
]
},
{
"field": "meta_value",
"position": 4,
"type": "lastName",
"constraints": [
{
"field": "meta_key",
"position": 3,
"value": "_give_donor_last_name"
}
]
}
]
},
{
"tableName": ".*_give_donors",
"fields": [
{
"field": "email",
"position": 3,
"type": "email",
"constraints": null
},
{
"field": "name",
"position": 4,
"type": "name",
"constraints": null
}
]
},
{
"tableName": ".*_give_sessions",
"purge": true,
"fields": []
}
]
}
Loading