Skip to content

Commit

Permalink
feat: rename and clean-up CLI for syncing, add another one for exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLoch committed Feb 14, 2024
1 parent 75b0ac8 commit 3687ca9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
14 changes: 14 additions & 0 deletions cmd/export/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
hibpsync "github.com/exaring/go-hibp-sync"
"os"
)

func main() {
if err := hibpsync.Export(os.Stdout); err != nil {
_, _ = os.Stderr.WriteString("Failed to export HIBP data: " + err.Error())

os.Exit(1)
}
}
25 changes: 20 additions & 5 deletions cmd/cli/main.go → cmd/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ package main

import (
"fmt"
hibpsync "github.com/exaring/go-hibp-sync"
"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
"os"
"path"
"time"

hibpsync "github.com/exaring/go-hibp-sync"
)

func main() {
if err := run(); 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 {
return fmt.Errorf("creating state file directory %q: %w", stateFilePath, err)
}

stateFile, err := os.OpenFile(hibpsync.DefaultStateFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Printf("opening state file error: %q", err)
return fmt.Errorf("opening state file: %w", err)
}
defer stateFile.Close()

Expand Down Expand Up @@ -46,10 +59,12 @@ func main() {
}

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

if err := os.Remove(hibpsync.DefaultStateFile); err != nil {
fmt.Printf("removing state file error: %q", err)
return fmt.Errorf("removing state file %q: %w", hibpsync.DefaultStateFile, err)
}

return nil
}

0 comments on commit 3687ca9

Please sign in to comment.