Skip to content

Commit

Permalink
feat: allow using different data dir within the tiny cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLoch committed Feb 16, 2024
1 parent 7704cb7 commit 4dfe0b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
8 changes: 7 additions & 1 deletion cmd/export/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 14 additions & 8 deletions cmd/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ 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

func Sync(options ...SyncOption) error {
config := &syncConfig{
commonConfig: commonConfig{
dataDir: defaultDataDir,
dataDir: DefaultDataDir,
},
ctx: context.Background(),
endpoint: defaultEndpoint,
Expand Down Expand Up @@ -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,
},
}

Expand All @@ -93,7 +93,7 @@ type RangeAPI struct {
func NewRangeAPI(options ...QueryOption) *RangeAPI {
config := &queryConfig{
commonConfig: commonConfig{
dataDir: defaultDataDir,
dataDir: DefaultDataDir,
},
}

Expand Down

0 comments on commit 4dfe0b3

Please sign in to comment.