mirror of
https://codeberg.org/scip/anydb.git
synced 2025-12-17 04:20:59 +01:00
add import+export, json output, humanize wide output, fixes
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/tlinden/anydb/app"
|
||||
"github.com/tlinden/anydb/cfg"
|
||||
)
|
||||
|
||||
func List(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
||||
// FIXME: call sort here
|
||||
// FIXME: check output mode switch to subs
|
||||
|
||||
tableString := &strings.Builder{}
|
||||
table := tablewriter.NewWriter(tableString)
|
||||
|
||||
if conf.Mode == "wide" {
|
||||
table.SetHeader([]string{"KEY", "VALUE", "TAGS", "TIMESTAMP"})
|
||||
} else {
|
||||
table.SetHeader([]string{"KEY", "VALUE"})
|
||||
}
|
||||
|
||||
for _, row := range entries {
|
||||
if row.Value == "" {
|
||||
row.Value = string(row.Bin)[0:60]
|
||||
} else if len(row.Value) > 60 {
|
||||
row.Value = row.Value[0:60]
|
||||
}
|
||||
|
||||
if conf.Mode == "wide" {
|
||||
table.Append([]string{row.Key, row.Value, strings.Join(row.Tags, ","), row.Created.Format("02.01.2006T03:04.05")})
|
||||
} else {
|
||||
table.Append([]string{row.Key, row.Value})
|
||||
}
|
||||
}
|
||||
|
||||
table.SetAutoWrapText(false)
|
||||
table.SetAutoFormatHeaders(true)
|
||||
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetCenterSeparator("")
|
||||
table.SetColumnSeparator("")
|
||||
table.SetRowSeparator("")
|
||||
table.SetHeaderLine(false)
|
||||
table.SetBorder(false)
|
||||
table.SetNoWhiteSpace(true)
|
||||
|
||||
table.SetTablePadding("\t") // pad with tabs
|
||||
|
||||
table.Render()
|
||||
|
||||
fmt.Fprint(writer, tableString.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
100
output/list.go
Normal file
100
output/list.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/tlinden/anydb/app"
|
||||
"github.com/tlinden/anydb/cfg"
|
||||
)
|
||||
|
||||
func List(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
||||
// FIXME: call sort here
|
||||
switch conf.Mode {
|
||||
case "wide":
|
||||
fallthrough
|
||||
case "":
|
||||
fallthrough
|
||||
case "table":
|
||||
return ListTable(writer, conf, entries)
|
||||
case "json":
|
||||
return ListJson(writer, conf, entries)
|
||||
default:
|
||||
return errors.New("unsupported mode")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ListJson(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
||||
jsonentries, err := json.Marshal(entries)
|
||||
if err != nil {
|
||||
return fmt.Errorf("json marshalling failure: %s", err)
|
||||
}
|
||||
|
||||
fmt.Println(string(jsonentries))
|
||||
return nil
|
||||
}
|
||||
|
||||
func ListTable(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
||||
tableString := &strings.Builder{}
|
||||
table := tablewriter.NewWriter(tableString)
|
||||
|
||||
if !conf.NoHeaders {
|
||||
if conf.Mode == "wide" {
|
||||
table.SetHeader([]string{"KEY", "TAGS", "SIZE", "AGE", "VALUE"})
|
||||
} else {
|
||||
table.SetHeader([]string{"KEY", "VALUE"})
|
||||
}
|
||||
}
|
||||
|
||||
for _, row := range entries {
|
||||
size := len(row.Value)
|
||||
|
||||
if len(row.Bin) > 0 {
|
||||
row.Value = "binary-content"
|
||||
size = len(row.Bin)
|
||||
}
|
||||
|
||||
if len(row.Value) > 60 {
|
||||
row.Value = row.Value[0:60] + "..."
|
||||
}
|
||||
|
||||
if conf.Mode == "wide" {
|
||||
table.Append([]string{
|
||||
row.Key,
|
||||
strings.Join(row.Tags, ","),
|
||||
humanize.Bytes(uint64(size)),
|
||||
//row.Created.Format("02.01.2006T03:04.05"),
|
||||
humanize.Time(row.Created),
|
||||
row.Value,
|
||||
})
|
||||
} else {
|
||||
table.Append([]string{row.Key, row.Value})
|
||||
}
|
||||
}
|
||||
|
||||
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetAutoWrapText(false)
|
||||
table.SetAutoFormatHeaders(true)
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetCenterSeparator("")
|
||||
table.SetColumnSeparator("")
|
||||
table.SetRowSeparator("")
|
||||
table.SetHeaderLine(false)
|
||||
table.SetBorder(false)
|
||||
table.SetNoWhiteSpace(true)
|
||||
|
||||
table.SetTablePadding("\t") // pad with tabs
|
||||
|
||||
table.Render()
|
||||
|
||||
fmt.Fprint(writer, tableString.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
60
output/single.go
Normal file
60
output/single.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/tlinden/anydb/app"
|
||||
"github.com/tlinden/anydb/cfg"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func Print(writer io.Writer, conf *cfg.Config, entry *app.DbEntry) error {
|
||||
if conf.Mode != "" {
|
||||
// consider this to be a file
|
||||
fd, err := os.OpenFile(conf.Mode, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
if len(entry.Bin) > 0 {
|
||||
// binary file content
|
||||
_, err = fd.Write(entry.Bin)
|
||||
} else {
|
||||
val := entry.Value
|
||||
if !strings.HasSuffix(val, "\n") {
|
||||
// always add a terminal newline
|
||||
val += "\n"
|
||||
}
|
||||
|
||||
_, err = fd.Write([]byte(val))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
isatty := term.IsTerminal(int(os.Stdout.Fd()))
|
||||
if len(entry.Bin) > 0 {
|
||||
if isatty {
|
||||
fmt.Println("binary data omitted")
|
||||
} else {
|
||||
os.Stdout.Write(entry.Bin)
|
||||
}
|
||||
} else {
|
||||
fmt.Print(entry.Value)
|
||||
|
||||
if !strings.HasSuffix(entry.Value, "\n") {
|
||||
// always add a terminal newline
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
34
output/write.go
Normal file
34
output/write.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user