Skip to content

Commit 408a26b

Browse files
committed
feat: refactor handler and commands
- Add new `nuke` command to reset the database. - Refactor `add`, `get`, and `delete` commands to support multiple arguments and flag-based filtering. - Introduce new error handling and time option structures. - Update view layer for command feedback.
1 parent aa0073e commit 408a26b

File tree

23 files changed

+572
-247
lines changed

23 files changed

+572
-247
lines changed

cmd/add.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import (
1111
var addOpts = &handler.AddOpts{}
1212

1313
var addCmd = &cobra.Command{
14-
Use: "add [body]",
15-
Short: "Add a log entry",
16-
Long: `Add a log entry.`,
17-
Args: cobra.ExactArgs(1),
14+
Use: "add [body]",
15+
Short: "Add log entries",
16+
Long: `Add log entries.
17+
18+
You can create multiple entries by providing as multiple arguments.
19+
`,
20+
Args: cobra.MinimumNArgs(1),
1821
PreRunE: handler.ValidateOptions(cfg, addOpts),
1922
RunE: handler.Add(cfg, db, addOpts),
2023
SilenceUsage: true,

cmd/delete.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ import (
1111
var deleteOpts = &handler.DeleteOpts{}
1212

1313
var deleteCmd = &cobra.Command{
14-
Use: "delete [id]",
15-
Short: "Delete log entries",
16-
Long: `Delete log entries. You can delete with entry id or interactively select them.`,
17-
Args: cobra.ExactArgs(1),
14+
Use: "delete [id]",
15+
Short: "Delete log entries",
16+
Long: `Delete log entries.
17+
18+
You can delete specific entries by providing IDs as arguments.
19+
Alternatively, you can delete entries by using flags to filter by date range or limit the count.
20+
21+
Note: Providing an ID is mutually exclusive with using any flags.`,
22+
Args: cobra.ArbitraryArgs,
1823
PreRunE: handler.ValidateOptions(cfg, deleteOpts),
1924
RunE: handler.Delete(cfg, db, deleteOpts),
2025
SilenceUsage: true,

cmd/get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ var getCmd = &cobra.Command{
1515
Short: "Get log entries",
1616
Long: `Get log entries.
1717
18-
You can retrieve a single entry by providing its ID as an argument.
19-
Alternatively, you can retrieve a list of entries by using flags to filter by date range or limit the count.
18+
You can retrieve specific entries by providing IDs as arguments.
19+
Alternatively, you can retrieve entries by using flags to filter by date range or limit the count.
2020
2121
Note: Providing an ID is mutually exclusive with using any flags.`,
22-
Args: cobra.MaximumNArgs(1),
22+
Args: cobra.ArbitraryArgs,
2323
PreRunE: handler.ValidateOptions(cfg, getOpts),
2424
RunE: handler.Get(cfg, db, getOpts),
2525
SilenceUsage: true,

cmd/nuke.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright © 2025 Ethan Lee <[email protected]>
3+
*/
4+
package cmd
5+
6+
import (
7+
"github.com/ethn1ee/llog/internal/handler"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var nukeOpts = &handler.NukeOpts{}
12+
13+
var nukeCmd = &cobra.Command{
14+
Use: "nuke",
15+
Short: "Delete all data",
16+
Long: `Delete all data.
17+
18+
This will nuke the entire database and clear all data.
19+
This action cannot be undone.`,
20+
Args: cobra.ArbitraryArgs,
21+
PreRunE: handler.ValidateOptions(cfg, nukeOpts),
22+
RunE: handler.Reset(cfg, db, nukeOpts),
23+
SilenceUsage: true,
24+
}
25+
26+
func init() {
27+
handler.ApplyFlags(nukeCmd, nukeOpts)
28+
rootCmd.AddCommand(nukeCmd)
29+
}

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Copyright © 2025 Ethan Lee <[email protected]>
44
package cmd
55

66
import (
7+
"log/slog"
78
"os"
89

910
"github.com/ethn1ee/llog/internal/config"
@@ -30,6 +31,7 @@ var rootCmd = &cobra.Command{
3031
func Execute() {
3132
err := rootCmd.Execute()
3233
if err != nil {
34+
slog.Error("failed to execute command", slog.Any("error", err))
3335
os.Exit(1)
3436
}
3537
_ = lg.Close()

cmd/summarize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var summarizeOpts = &handler.SummarizeOpts{}
1313
var summarizeCmd = &cobra.Command{
1414
Use: "summarize",
1515
Short: "Summarize log entries",
16-
Long: `Summarize log entries. You can specify date range with flags.`,
16+
Long: `Summarize log entries.`,
1717
PreRunE: handler.ValidateOptions(cfg, summarizeOpts),
1818
RunE: handler.Summarize(cfg, db, summarizeOpts),
1919
SilenceUsage: true,

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ require (
1919
github.com/jinzhu/now v1.1.5 // indirect
2020
github.com/mattn/go-colorable v0.1.13 // indirect
2121
github.com/mattn/go-isatty v0.0.20 // indirect
22+
github.com/mattn/go-runewidth v0.0.16 // indirect
2223
github.com/mattn/go-sqlite3 v1.14.32 // indirect
24+
github.com/olekukonko/errors v1.1.0 // indirect
25+
github.com/olekukonko/ll v0.0.9 // indirect
26+
github.com/olekukonko/tablewriter v1.0.9 // indirect
2327
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
28+
github.com/rivo/uniseg v0.2.0 // indirect
2429
github.com/sagikazarmark/locafero v0.11.0 // indirect
2530
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
2631
github.com/spf13/afero v1.15.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
2626
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
2727
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
2828
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
29+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
30+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2931
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
3032
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
33+
github.com/olekukonko/errors v1.1.0 h1:RNuGIh15QdDenh+hNvKrJkmxxjV4hcS50Db478Ou5sM=
34+
github.com/olekukonko/errors v1.1.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
35+
github.com/olekukonko/ll v0.0.9 h1:Y+1YqDfVkqMWuEQMclsF9HUR5+a82+dxJuL1HHSRpxI=
36+
github.com/olekukonko/ll v0.0.9/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g=
37+
github.com/olekukonko/tablewriter v1.0.9 h1:XGwRsYLC2bY7bNd93Dk51bcPZksWZmLYuaTHR0FqfL8=
38+
github.com/olekukonko/tablewriter v1.0.9/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo=
3139
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
3240
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
3341
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3442
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
43+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
44+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
3545
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
3646
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
3747
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

internal/config/internal.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package config
22

33
type internalConfig struct {
4-
EntryCount int64
5-
MaxEntryId uint64
6-
MaxEntryIdDigits int
4+
EntryCount int64
5+
MaxEntryId uint64
76
}

internal/db/db.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"strconv"
98

109
"github.com/ethn1ee/llog/internal/config"
1110
"github.com/ethn1ee/llog/internal/model"
@@ -14,6 +13,7 @@ import (
1413
)
1514

1615
type DB struct {
16+
db *gorm.DB
1717
Entry *entryDB
1818
}
1919

@@ -33,6 +33,7 @@ func Load(cfg *config.Config, ctx context.Context, db *DB) error {
3333
return fmt.Errorf("failed to migrate Entry: %w", err)
3434
}
3535

36+
db.db = gormdb
3637
db.Entry = &entryDB{gorm.G[model.Entry](gormdb)}
3738

3839
count, err := db.Entry.Count(ctx)
@@ -49,8 +50,11 @@ func Load(cfg *config.Config, ctx context.Context, db *DB) error {
4950
}
5051

5152
cfg.Internal.MaxEntryId = last.ID
52-
cfg.Internal.MaxEntryIdDigits = len(strconv.FormatUint(uint64(last.ID), 10))
5353
}
5454

5555
return nil
5656
}
57+
58+
func (db *DB) Nuke() error {
59+
return db.db.Migrator().DropTable(&model.Entry{})
60+
}

0 commit comments

Comments
 (0)