add import+export, json output, humanize wide output, fixes

This commit is contained in:
2024-12-18 14:06:21 +01:00
parent 9e6bbd5419
commit 332eed679e
11 changed files with 570 additions and 122 deletions

34
output/write.go Normal file
View File

@@ -0,0 +1,34 @@
package output
import (
"encoding/json"
"fmt"
"os"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
)
func WriteFile(attr *app.DbAttr, conf *cfg.Config, entries app.DbEntries) error {
jsonentries, err := json.Marshal(entries)
if err != nil {
return fmt.Errorf("json marshalling failure: %s", err)
}
if attr.File == "-" {
fmt.Println(string(jsonentries))
} else {
fd, err := os.OpenFile(attr.File, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
if _, err := fd.Write(jsonentries); err != nil {
return err
}
fmt.Printf("database contents exported to %s\n", attr.File)
}
return nil
}