diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-14 11:31:39 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-14 11:31:39 +0300 |
| commit | 0b090c2698607fcdef039fb703c8c6bac5fceca0 (patch) | |
| tree | 77b3ba27e9f07d128115cd5a41ac6c2f6db8c01b /internal/cli/cmd_import.go | |
| parent | 0165352a63cb2f78871c2e72d4f2fd4f111637c3 (diff) | |
refactor(cli): split run* handlers into files (ask 24)
Move runImport, runQuery, runReportFromFiles, runDaemon,
runCreateClientKey, and runTests into dedicated cmd_*.go files.
Keep Execute routing in cli.go. defaultListenFromEnv stays with daemon.
Made-with: Cursor
Diffstat (limited to 'internal/cli/cmd_import.go')
| -rw-r--r-- | internal/cli/cmd_import.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/cli/cmd_import.go b/internal/cli/cmd_import.go new file mode 100644 index 0000000..f3ecad5 --- /dev/null +++ b/internal/cli/cmd_import.go @@ -0,0 +1,38 @@ +package cli + +import ( + "context" + "flag" + "fmt" + "os" + + "codeberg.org/snonux/goprecords/internal/storage" +) + +func runImport(args []string) error { + fs := flag.NewFlagSet("import", flag.ExitOnError) + statsDir := fs.String("stats-dir", "", "Directory containing .records files (required)") + dbPath := fs.String("db", "goprecords.db", "SQLite database path") + if err := fs.Parse(args); err != nil { + return err + } + if *statsDir == "" { + fmt.Fprintln(os.Stderr, "import: missing required flag: -stats-dir") + fs.Usage() + return fmt.Errorf("missing -stats-dir") + } + ctx := context.Background() + db, err := storage.Open(ctx, *dbPath) + if err != nil { + return fmt.Errorf("open db: %w", err) + } + defer db.Close() + if err := storage.CreateSchema(ctx, db); err != nil { + return fmt.Errorf("schema: %w", err) + } + if err := storage.ImportFromDir(ctx, db, *statsDir); err != nil { + return fmt.Errorf("import: %w", err) + } + fmt.Fprintf(os.Stderr, "imported %s into %s\n", *statsDir, *dbPath) + return nil +} |
