Skip to content

Commit

Permalink
Do not expose config spec - all the config routines are managed by co…
Browse files Browse the repository at this point in the history
…nfig package
  • Loading branch information
vgarvardt committed Apr 3, 2020
1 parent 9398461 commit b4b46b2
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ You can set a number of keys in the configuration file. Below is a list of all c
- `Name` - The table name.
- `IgnoreData` - A flag to indicate whether data should be imported or not. If set to true, it will dump the table structure without importing data.
- `Filter` - A Klepto definition to filter results.
- `Match` - A condition field to dump only certain amount data. The value should correspond to an existing `Matchers` definition.
- `Match` - A condition field to dump only certain amount data. The value may be either expression or correspond to an existing `Matchers` definition.
- `Limit` - The number of results to be fetched.
- `Sorts` - Defines how the table is sorted.
- `Anonymise` - Indicates which columns to anonymise.
Expand Down
41 changes: 2 additions & 39 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"bufio"
"os"

"github.com/BurntSushi/toml"
wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -39,43 +37,8 @@ func RunInit() error {
return wErrors.Wrap(err, "could not create file")
}

e := toml.NewEncoder(bufio.NewWriter(f))
err = e.Encode(config.Spec{
Matchers: map[string]string{
"ActiveUsers": "users.active = TRUE",
},
Tables: []*config.Table{
{
Name: "users",
Filter: config.Filter{
Match: "users.active = TRUE",
Sorts: map[string]string{"user.id": "asc"},
Limit: 100,
},
Anonymise: map[string]string{"firstName": "FirstName", "email": "EmailAddress"},
},
{
Name: "orders",
Filter: config.Filter{
Match: "ActiveUsers",
Limit: 10,
},
Relationships: []*config.Relationship{
{
ReferencedTable: "users",
ReferencedKey: "id",
ForeignKey: "user_id",
},
},
},
{
Name: "logs",
IgnoreData: true,
},
},
})
if err != nil {
return wErrors.Wrap(err, "could not encode config")
if err := config.WriteSample(f); err != nil {
return wErrors.Wrap(err, "could not write config")
}

log.Infof("Created %s!", config.DefaultConfigFileName)
Expand Down
2 changes: 1 addition & 1 deletion cmd/steal.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewStealCmd() *cobra.Command {
Short: "Steals and anonymises databases",
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error
opts.cfgTables, err = config.LoadSpecFromFile(opts.configPath)
opts.cfgTables, err = config.LoadFromFile(opts.configPath)
if err != nil {
return err
}
Expand Down
51 changes: 46 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package config

import (
"io"
"strings"

"github.com/BurntSushi/toml"
wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand All @@ -14,8 +16,8 @@ const (
)

type (
// Spec represents the global app configuration.
Spec struct {
// spec represents the global app configuration.
spec struct {
Matchers
Tables
}
Expand Down Expand Up @@ -75,8 +77,8 @@ func (t Tables) FindByName(name string) *Table {
return nil
}

// LoadSpecFromFile loads klepto spec from file
func LoadSpecFromFile(configPath string) (Tables, error) {
// LoadFromFile loads klepto tables config from file
func LoadFromFile(configPath string) (Tables, error) {
if configPath == "" {
return nil, wErrors.New("config file path can not be empty")
}
Expand All @@ -89,7 +91,7 @@ func LoadSpecFromFile(configPath string) (Tables, error) {
return nil, wErrors.Wrap(err, "could not read configurations")
}

cfgSpec := new(Spec)
cfgSpec := new(spec)
err = viper.Unmarshal(cfgSpec)
if err != nil {
return nil, wErrors.Wrap(err, "could not unmarshal config file")
Expand All @@ -115,3 +117,42 @@ func LoadSpecFromFile(configPath string) (Tables, error) {

return cfgSpec.Tables, nil
}

// WriteSample generates and writes sample config to a writer
func WriteSample(w io.Writer) error {
e := toml.NewEncoder(w)
return e.Encode(spec{
Matchers: map[string]string{
"ActiveUsers": "users.active = TRUE",
},
Tables: []*Table{
{
Name: "users",
Filter: Filter{
Match: "users.active = TRUE",
Sorts: map[string]string{"user.id": "asc"},
Limit: 100,
},
Anonymise: map[string]string{"firstName": "FirstName", "email": "EmailAddress"},
},
{
Name: "orders",
Filter: Filter{
Match: "ActiveUsers",
Limit: 10,
},
Relationships: []*Relationship{
{
ReferencedTable: "users",
ReferencedKey: "id",
ForeignKey: "user_id",
},
},
},
{
Name: "logs",
IgnoreData: true,
},
},
})
}
52 changes: 50 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bytes"
"os"
"path/filepath"
"testing"
Expand All @@ -10,7 +11,7 @@ import (
)

func TestLoadSpecFromFile(t *testing.T) {
_, err := LoadSpecFromFile("")
_, err := LoadFromFile("")
require.Error(t, err)

cwd, err := os.Getwd()
Expand All @@ -19,7 +20,7 @@ func TestLoadSpecFromFile(t *testing.T) {
// klepto/pkg/config/../../fixtures/.klepto.toml
configPath := filepath.Join(cwd, "..", "..", "fixtures", ".klepto.toml")

cfgTables, err := LoadSpecFromFile(configPath)
cfgTables, err := LoadFromFile(configPath)
require.NoError(t, err)
require.Len(t, cfgTables, 3)

Expand All @@ -31,3 +32,50 @@ func TestLoadSpecFromFile(t *testing.T) {
require.NotNil(t, orders)
assert.Equal(t, "users.active = TRUE", orders.Filter.Match)
}

func TestWriteSample(t *testing.T) {
w := new(bytes.Buffer)

err := WriteSample(w)
require.NoError(t, err)

assert.Equal(t, sampleConfig, w.String())
}

const (
sampleConfig = `[Matchers]
ActiveUsers = "users.active = TRUE"
[[Tables]]
Name = "users"
IgnoreData = false
[Tables.Filter]
Match = "users.active = TRUE"
Limit = 100
[Tables.Filter.Sorts]
"user.id" = "asc"
[Tables.Anonymise]
email = "EmailAddress"
firstName = "FirstName"
[[Tables]]
Name = "orders"
IgnoreData = false
[Tables.Filter]
Match = "ActiveUsers"
Limit = 10
[[Tables.Relationships]]
Table = ""
ForeignKey = "user_id"
ReferencedTable = "users"
ReferencedKey = "id"
[[Tables]]
Name = "logs"
IgnoreData = true
[Tables.Filter]
Match = ""
Limit = 0
`
)

0 comments on commit b4b46b2

Please sign in to comment.