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. 76
      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() {
if err != nil { var r io.Reader
return err 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 {
return err
}
if !bytes.Equal(tmp[:], xlHeader[:]) {
return fmt.Errorf("xlMeta: unknown XLv2 header, expected %v, got %v", xlHeader[:4], tmp[:4])
}
// 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
}
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))
} }
r.Seek(8, io.SeekStart) return nil
defer r.Close() }
_, err = msgp.CopyToJSON(os.Stdout, r) err := app.Run(os.Args)
return err if err != nil {
log.Fatal(err)
} }
app.Run(os.Args)
} }

Loading…
Cancel
Save