Skip to content

Commit

Permalink
Adding import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstinnett committed Feb 3, 2024
1 parent 5d61007 commit d16df98
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/letseat/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func runExport(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
defer dclose(diary)
fmt.Fprint(cmd.OutOrStdout(), string(out))
return diary.Close()
return nil
}
11 changes: 2 additions & 9 deletions cmd/letseat/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func runImport(cmd *cobra.Command, args []string) error {
diary := letseat.New(
letseat.WithDBFilename(mustGetCmd[string](*cmd, "data")),
)
defer dclose(diary)
eb, err := os.ReadFile(args[0])
if err != nil {
return err
Expand All @@ -46,15 +47,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
if err := diary.Log(entry); err != nil {
return err
}
}
*/
return nil
}

type pbar struct {
Expand Down
8 changes: 8 additions & 0 deletions cmd/letseat/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"errors"
"fmt"
"io"
"log/slog"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -104,3 +106,9 @@ func exists(path string) bool {
}
return false
}

func dclose(c io.Closer) {
if err := c.Close(); err != nil {
slog.Error("error closing file")
}
}
3 changes: 2 additions & 1 deletion pkg/person.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package letseat

import (
"encoding/json"
"sort"
)

Expand All @@ -11,13 +10,15 @@ type Person struct {
PlaceAvgRatings map[string]float64
}

/*
func (p Person) mustMarshal() []byte {
b, err := json.Marshal(p)
if err != nil {
panic(err)
}
return b
}
*/

// FavoriteN returns the persons N favorite restaurants
func (p *Person) FavoriteN(n int) []string {
Expand Down

0 comments on commit d16df98

Please sign in to comment.