Skip to content

Commit

Permalink
Adding export
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstinnett committed Feb 3, 2024
1 parent 9fb3589 commit 5d61007
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
29 changes: 29 additions & 0 deletions cmd/letseat/cmd/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"fmt"

letseat "github.com/drewstinnett/letseat/pkg"
"github.com/spf13/cobra"
)

func newExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "export entries",
RunE: runExport,
}
return cmd
}

func runExport(cmd *cobra.Command, args []string) error {
diary := letseat.New(
letseat.WithDBFilename(mustGetCmd[string](*cmd, "data")),
)
out, err := diary.Export()
if err != nil {
return err
}
fmt.Fprint(cmd.OutOrStdout(), string(out))
return diary.Close()
}
4 changes: 2 additions & 2 deletions cmd/letseat/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func runImport(cmd *cobra.Command, args []string) error {
if rerr != nil {
slog.Error("error running progressbar", "error", rerr)
}
return diary.Close()
/*
for _, entry := range entries {
entry := entry
Expand All @@ -54,7 +55,6 @@ func runImport(cmd *cobra.Command, args []string) error {
}
}
*/
return nil
}

type pbar struct {
Expand Down Expand Up @@ -131,4 +131,4 @@ func (p *pbar) log() tea.Cmd {
*p.current++
return logged(res)
}
}
}
26 changes: 26 additions & 0 deletions cmd/letseat/cmd/import_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"path"
"testing"

Expand All @@ -12,4 +13,29 @@ func TestImport(t *testing.T) {
cmd := newRootCmd()
cmd.SetArgs([]string{"import", "../testdata/import.yaml", "--data", dbf})
require.NoError(t, cmd.Execute())

b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"export", "--data", dbf})
require.NoError(t, cmd.Execute())
require.Equal(
t,
`- place: Franks Place
date: 2023-12-14T00:00:00Z
takeout: true
ratings:
andrei: 4
jeymes: 3
- place: McDonuoughs Pub
date: 2023-12-16T00:00:00Z
ratings:
andrei: 4
- place: Biggy Wings
date: 2023-12-21T00:00:00Z
takeout: true
ratings:
andrei: 3
`,
b.String(),
)
}
1 change: 1 addition & 0 deletions cmd/letseat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func newRootCmd() *cobra.Command {
newRecommendCommand(),
newConfigCmd(),
newImportCmd(),
newExportCmd(),
)

return cmd
Expand Down
37 changes: 37 additions & 0 deletions pkg/diary.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/montanaflynn/stats"
bolt "go.etcd.io/bbolt"
"gopkg.in/yaml.v2"
)

// Diary is the thing holding all of your visits and info
Expand All @@ -29,6 +30,42 @@ func (d Diary) Entries() Entries {
return *d.entries
}

// Close closes the database
func (d Diary) Close() error {
return d.db.Close()
}

func (d Diary) allEntries() (Entries, error) {
ret := Entries{}
if verr := d.db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
c := tx.Bucket([]byte(EntriesBucket)).Cursor()

for k, v := c.First(); k != nil; k, v = c.Next() {
var e Entry
err := json.Unmarshal(v, &e)
if err != nil {
return err
}
ret = append(ret, e)
// fmt.Printf("key=%s, value=%s\n", k, v)
}
return nil
}); verr != nil {
return nil, verr
}
return ret, nil
}

// Export returns all entries in yaml form
func (d Diary) Export() ([]byte, error) {
entries, err := d.allEntries()
if err != nil {
return nil, err
}
return yaml.Marshal(entries)
}

// Log logs a new entry to your diary
func (d *Diary) Log(es ...Entry) error {
if d.entries == nil {
Expand Down

0 comments on commit 5d61007

Please sign in to comment.