Add neater metadata printer (#9898)

* Just read files from args (more than 1 now supported)
* Pretty print by default. `-ndjson` will disable.
* Check header.
* Support stdin as '-'
* Don't just ignore errors.
master
Klaus Post 4 years ago committed by GitHub
parent e92434c2e7
commit 691dc04fac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 70
      docs/bucket/versioning/xl-meta-to-json.go

@ -1,13 +1,19 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"fmt"
"io" "io"
"log"
"os" "os"
"github.com/minio/cli" "github.com/minio/cli"
"github.com/tinylib/msgp/msgp" "github.com/tinylib/msgp/msgp"
) )
var xlHeader = [4]byte{'X', 'L', '2', ' '}
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Copyright = "MinIO, Inc." app.Copyright = "MinIO, Inc."
@ -16,21 +22,69 @@ func main() {
app.HideHelpCommand = true app.HideHelpCommand = true
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.StringFlag{ cli.BoolFlag{
Usage: "path to xl.meta file", Usage: "Print each file as a separate line without formatting",
Name: "f, file", Name: "ndjson",
}, },
} }
app.Action = func(c *cli.Context) error { app.Action = func(c *cli.Context) error {
r, err := os.Open(c.String("file")) for _, file := range c.Args() {
var r io.Reader
switch file {
case "-":
r = os.Stdin
default:
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
r = f
}
// Read header
var tmp [4]byte
_, err := io.ReadFull(r, tmp[:])
if err != nil { if err != nil {
return err return err
} }
r.Seek(8, io.SeekStart) if !bytes.Equal(tmp[:], xlHeader[:]) {
defer r.Close() return fmt.Errorf("xlMeta: unknown XLv2 header, expected %v, got %v", xlHeader[:4], tmp[:4])
_, err = msgp.CopyToJSON(os.Stdout, r) }
// Skip version check for now
_, err = io.ReadFull(r, tmp[:])
if err != nil {
return err
}
var buf bytes.Buffer
_, err = msgp.CopyToJSON(&buf, r)
if err != nil {
return err return err
} }
app.Run(os.Args) if c.Bool("ndjson") {
fmt.Println(buf.String())
continue
}
var msi map[string]interface{}
dec := json.NewDecoder(&buf)
// Use number to preserve integers.
dec.UseNumber()
err = dec.Decode(&msi)
if err != nil {
return err
}
b, err := json.MarshalIndent(msi, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
} }

Loading…
Cancel
Save