Skip to content

feat: add --json support for history command#55

Merged
kavix merged 1 commit into
kavix:mainfrom
hfl0506:feat/add-json-output-format-support
Jul 5, 2026
Merged

feat: add --json support for history command#55
kavix merged 1 commit into
kavix:mainfrom
hfl0506:feat/add-json-output-format-support

Conversation

@hfl0506

@hfl0506 hfl0506 commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Description

Adds a --json flag to eko history so snapshot history can be printed as a JSON array for programmatic parsing.

The default eko history output remains unchanged.

Related Issues

Resolve #49

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring / Code quality improvement

Technical Details & Architecture Notes

  • Added a boolean Cobra flag, --json, to the history command.
  • Snapshot records are loaded into a small response struct and marshaled with encoding/json.
  • JSON output is emitted as an array, including [] when there are no history entries.
  • No database schema changes were made.

How Has This Been Tested?

  • Run go test -v ./... results: all tests passed.
  • Run go vet ./... results: no issues reported.
  • Added unit test coverage for eko history --json.

Testing Steps

go test ./...
go run . save
go run . history --json

Testing Result

# go run . history --json
[{"id":"a6a35ab3","created_at":"2026-07-04T06:23:12Z"},{"id":"0265467d","created_at":"2026-07-04T06:23:43Z"},{"id":"5338590f","created_at":"2026-07-04T18:28:12Z"}]
# go run . history --json | jq
[
  {
    "id": "a6a35ab3",
    "created_at": "2026-07-04T06:23:12Z"
  },
  {
    "id": "0265467d",
    "created_at": "2026-07-04T06:23:43Z"
  },
  {
    "id": "5338590f",
    "created_at": "2026-07-04T18:28:12Z"
  }
]

Checklist

  • My code follows the style guidelines of this project (run gofmt and go vet)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (including README.md and CONTRIBUTING.md)
  • My changes generate no new compiler or linter warnings
  • I have added/updated unit tests that prove my fix is effective or that my feature works
  • All new and existing unit tests pass locally with my changes

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a --json flag to eko history to emit snapshot history as a JSON array for programmatic consumption, while keeping the default human-readable output intact.

Changes:

  • Added --json Cobra flag to history and JSON marshaling of snapshot records.
  • Improved DB query handling in history (captures query/scan errors; ensures [] output for empty history).
  • Added unit test coverage validating that eko history --json returns valid JSON with expected fields.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cmd/history.go Adds --json output mode and structures history records for JSON serialization.
cmd/commands_test.go Adds a unit test for JSON history output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/history.go
Comment on lines +33 to 53
entries := []historyEntry{}
for rows.Next() {
var id, created string
rows.Scan(&id, &created)
fmt.Println(id, created)
var entry historyEntry
if err := rows.Scan(&entry.ID, &entry.CreatedAt); err != nil {
panic(err)
}
entries = append(entries, entry)
}

if jsonOutput {
data, err := json.Marshal(entries)
if err != nil {
panic(err)
}
fmt.Println(string(data))
return
}

for _, entry := range entries {
fmt.Println(entry.ID, entry.CreatedAt)
}
Comment thread cmd/commands_test.go
Comment on lines +155 to +160
defer func() {
jsonOutput = false
if err := historyCmd.Flags().Set("json", "false"); err != nil {
t.Fatal(err)
}
}()
@kavix kavix merged commit 1030b84 into kavix:main Jul 5, 2026
2 checks passed
@kavix

kavix commented Jul 5, 2026

Copy link
Copy Markdown
Owner

@hfl0506 Thank you for your contribution! We appreciate the time and effort you put into improving the project. We look forward to your future contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for JSON output format in 'eko history'

3 participants