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:
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
|
||||
}
|
||||
Reference in New Issue
Block a user