Skip to content

Commit

Permalink
refactor: clean-up configuration code
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLoch committed Feb 15, 2024
1 parent 696cb85 commit e27c671
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 66 deletions.
2 changes: 1 addition & 1 deletion cmd/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func run() error {
return nil
}

if err := hibpsync.Sync(hibpsync.WithProgressFn(updateProgressBar), hibpsync.WithStateFile(stateFile)); err != nil {
if err := hibpsync.Sync(hibpsync.SyncWithProgressFn(updateProgressBar), hibpsync.SyncWithStateFile(stateFile)); err != nil {
return fmt.Errorf("syncing: %w", err)
}

Expand Down
97 changes: 97 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package hibpsync

import "io"

type commonConfig struct {
dataDir string
noCompression bool
}

type syncConfig struct {
commonConfig
endpoint string
minWorkers int
progressFn ProgressFunc
stateFile io.ReadWriteSeeker
lastRange int64
}

type SyncOption func(config *syncConfig)

func SyncWithDataDir(dataDir string) SyncOption {
return func(c *syncConfig) {
c.dataDir = dataDir
}
}

func SyncWithEndpoint(endpoint string) SyncOption {
return func(c *syncConfig) {
c.endpoint = endpoint
}
}

func SyncWithMinWorkers(workers int) SyncOption {
return func(c *syncConfig) {
c.minWorkers = workers
}
}

func SyncWithStateFile(stateFile io.ReadWriteSeeker) SyncOption {
return func(c *syncConfig) {
c.stateFile = stateFile
}
}

func SyncWithProgressFn(progressFn ProgressFunc) SyncOption {
return func(c *syncConfig) {
c.progressFn = progressFn
}
}

func SyncWithNoCompression() SyncOption {
return func(c *syncConfig) {
c.noCompression = true
}
}

func SyncWithLastRange(to int64) SyncOption {
return func(c *syncConfig) {
c.lastRange = to
}
}

type exportConfig struct {
commonConfig
}

type ExportOption func(*exportConfig)

func ExportWithDataDir(dataDir string) ExportOption {
return func(c *exportConfig) {
c.dataDir = dataDir
}
}

func ExportWithNoCompression() ExportOption {
return func(c *exportConfig) {
c.noCompression = true
}
}

type queryConfig struct {
commonConfig
}

type QueryOption func(*queryConfig)

func QueryWithDataDir(dataDir string) QueryOption {
return func(c *queryConfig) {
c.dataDir = dataDir
}
}

func QueryWithNoCompression() QueryOption {
return func(c *queryConfig) {
c.noCompression = true
}
}
86 changes: 23 additions & 63 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,11 @@ const (

type ProgressFunc func(lowest, current, to, processed, remaining int64) error

type config struct {
dataDir string
endpoint string
minWorkers int
progressFn ProgressFunc
stateFile io.ReadWriteSeeker
noCompression bool
lastRange int64
}

type Option func(*config)

func WithDataDir(dataDir string) Option {
return func(c *config) {
c.dataDir = dataDir
}
}

func WithEndpoint(endpoint string) Option {
return func(c *config) {
c.endpoint = endpoint
}
}

func WithMinWorkers(workers int) Option {
return func(c *config) {
c.minWorkers = workers
}
}

func WithStateFile(stateFile io.ReadWriteSeeker) Option {
return func(c *config) {
c.stateFile = stateFile
}
}

func WithProgressFn(progressFn ProgressFunc) Option {
return func(c *config) {
c.progressFn = progressFn
}
}

func WithNoCompression() Option {
return func(c *config) {
c.noCompression = true
}
}

func WithLastRange(to int64) Option {
return func(c *config) {
c.lastRange = to
}
}

func Sync(options ...Option) error {
config := &config{
dataDir: defaultDataDir,
func Sync(options ...SyncOption) error {
config := &syncConfig{
commonConfig: commonConfig{
dataDir: defaultDataDir,
},
endpoint: defaultEndpoint,
minWorkers: defaultWorkers,
progressFn: func(_, _, _, _, _ int64) error { return nil },
Expand Down Expand Up @@ -121,9 +69,11 @@ func Sync(options ...Option) error {
return sync(from, config.lastRange+1, client, storage, pool, config.progressFn)
}

func Export(w io.Writer, options ...Option) error {
config := &config{
dataDir: defaultDataDir,
func Export(w io.Writer, options ...ExportOption) error {
config := &exportConfig{
commonConfig: commonConfig{
dataDir: defaultDataDir,
},
}

for _, option := range options {
Expand All @@ -142,11 +92,21 @@ type RangeAPI struct {
storage storage
}

func NewRangeAPI(dataDir string, dataIsCompressed bool) *RangeAPI {
func NewRangeAPI(options ...QueryOption) *RangeAPI {
config := &queryConfig{
commonConfig: commonConfig{
dataDir: defaultDataDir,
},
}

for _, option := range options {
option(config)
}

return &RangeAPI{
storage: &fsStorage{
dataDir: dataDir,
doNotUseCompression: !dataIsCompressed,
dataDir: config.dataDir,
doNotUseCompression: config.noCompression,
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func BenchmarkQuery(b *testing.B) {

dataDir := b.TempDir()

if err := Sync(WithDataDir(dataDir), WithLastRange(lastRange)); err != nil {
if err := Sync(SyncWithDataDir(dataDir), SyncWithLastRange(lastRange)); err != nil {
b.Fatalf("unexpected error: %v", err)
}

querier := NewRangeAPI(dataDir, true)
querier := NewRangeAPI(QueryWithDataDir(dataDir))

b.ResetTimer()

Expand Down

0 comments on commit e27c671

Please sign in to comment.