diff --git a/cmd/export/main.go b/cmd/export/main.go index db47be8..3b9a3e7 100644 --- a/cmd/export/main.go +++ b/cmd/export/main.go @@ -6,7 +6,13 @@ import ( ) func main() { - if err := hibpsync.Export(os.Stdout); err != nil { + dataDir := hibpsync.DefaultDataDir + + if len(os.Args) == 2 { + dataDir = os.Args[1] + } + + if err := hibpsync.Export(os.Stdout, hibpsync.ExportWithDataDir(dataDir)); err != nil { _, _ = os.Stderr.WriteString("Failed to export HIBP data: " + err.Error()) os.Exit(1) diff --git a/cmd/sync/main.go b/cmd/sync/main.go index 12521e0..8c1f426 100644 --- a/cmd/sync/main.go +++ b/cmd/sync/main.go @@ -11,20 +11,26 @@ import ( ) func main() { - if err := run(); err != nil { + dataDir := hibpsync.DefaultDataDir + + if len(os.Args) == 2 { + dataDir = os.Args[1] + } + + if err := run(dataDir); err != nil { _, _ = os.Stderr.WriteString("Failed to sync HIBP data: " + err.Error()) os.Exit(1) } } -func run() error { - stateFilePath := path.Dir(hibpsync.DefaultStateFile) - if err := os.MkdirAll(stateFilePath, 0o755); err != nil { +func run(dataDir string) error { + stateFilePath := path.Join(dataDir, hibpsync.DefaultStateFileName) + if err := os.MkdirAll(path.Dir(stateFilePath), 0o755); err != nil { return fmt.Errorf("creating state file directory %q: %w", stateFilePath, err) } - stateFile, err := os.OpenFile(hibpsync.DefaultStateFile, os.O_RDWR|os.O_CREATE, 0644) + stateFile, err := os.OpenFile(stateFilePath, os.O_RDWR|os.O_CREATE, 0644) if err != nil { return fmt.Errorf("opening state file: %w", err) } @@ -58,12 +64,12 @@ func run() error { return nil } - if err := hibpsync.Sync(hibpsync.SyncWithProgressFn(updateProgressBar), hibpsync.SyncWithStateFile(stateFile)); err != nil { + if err := hibpsync.Sync(hibpsync.SyncWithDataDir(dataDir), hibpsync.SyncWithProgressFn(updateProgressBar), hibpsync.SyncWithStateFile(stateFile)); err != nil { return fmt.Errorf("syncing: %w", err) } - if err := os.Remove(hibpsync.DefaultStateFile); err != nil { - return fmt.Errorf("removing state file %q: %w", hibpsync.DefaultStateFile, err) + if err := os.Remove(stateFilePath); err != nil { + return fmt.Errorf("removing state file %q: %w", stateFilePath, err) } return nil diff --git a/lib.go b/lib.go index fda06ec..fb7d914 100644 --- a/lib.go +++ b/lib.go @@ -13,11 +13,11 @@ import ( ) const ( - defaultDataDir = "./.hibp-data" - defaultEndpoint = "https://api.pwnedpasswords.com/range/" - defaultWorkers = 50 - DefaultStateFile = "./.hibp-data/state" - defaultLastRange = 0xFFFFF + DefaultDataDir = "./.hibp-data" + defaultEndpoint = "https://api.pwnedpasswords.com/range/" + defaultWorkers = 50 + DefaultStateFileName = "state" + defaultLastRange = 0xFFFFF ) type ProgressFunc func(lowest, current, to, processed, remaining int64) error @@ -25,7 +25,7 @@ type ProgressFunc func(lowest, current, to, processed, remaining int64) error func Sync(options ...SyncOption) error { config := &syncConfig{ commonConfig: commonConfig{ - dataDir: defaultDataDir, + dataDir: DefaultDataDir, }, ctx: context.Background(), endpoint: defaultEndpoint, @@ -73,7 +73,7 @@ func Sync(options ...SyncOption) error { func Export(w io.Writer, options ...ExportOption) error { config := &exportConfig{ commonConfig: commonConfig{ - dataDir: defaultDataDir, + dataDir: DefaultDataDir, }, } @@ -93,7 +93,7 @@ type RangeAPI struct { func NewRangeAPI(options ...QueryOption) *RangeAPI { config := &queryConfig{ commonConfig: commonConfig{ - dataDir: defaultDataDir, + dataDir: DefaultDataDir, }, }