You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
2.5 KiB
123 lines
2.5 KiB
5 years ago
|
/*
|
||
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
5 years ago
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
5 years ago
|
"io"
|
||
5 years ago
|
"log"
|
||
5 years ago
|
"os"
|
||
|
|
||
|
"github.com/minio/cli"
|
||
|
"github.com/tinylib/msgp/msgp"
|
||
|
)
|
||
|
|
||
5 years ago
|
var xlHeader = [4]byte{'X', 'L', '2', ' '}
|
||
|
|
||
5 years ago
|
func main() {
|
||
|
app := cli.NewApp()
|
||
|
app.Copyright = "MinIO, Inc."
|
||
|
app.Usage = "xl.meta to JSON"
|
||
5 years ago
|
app.HideVersion = true
|
||
|
app.CustomAppHelpTemplate = `NAME:
|
||
|
{{.Name}} - {{.Usage}}
|
||
|
|
||
|
USAGE:
|
||
|
{{.Name}} {{if .VisibleFlags}}[FLAGS]{{end}} METAFILES...
|
||
|
{{if .VisibleFlags}}
|
||
|
GLOBAL FLAGS:
|
||
|
{{range .VisibleFlags}}{{.}}
|
||
|
{{end}}{{end}}
|
||
|
`
|
||
|
|
||
5 years ago
|
app.HideHelpCommand = true
|
||
|
|
||
|
app.Flags = []cli.Flag{
|
||
5 years ago
|
cli.BoolFlag{
|
||
|
Usage: "Print each file as a separate line without formatting",
|
||
|
Name: "ndjson",
|
||
5 years ago
|
},
|
||
|
}
|
||
|
|
||
|
app.Action = func(c *cli.Context) error {
|
||
4 years ago
|
files := c.Args()
|
||
|
if len(files) == 0 {
|
||
|
// If no args, assume xl.meta
|
||
|
files = []string{"xl.meta"}
|
||
5 years ago
|
}
|
||
4 years ago
|
for _, file := range files {
|
||
5 years ago
|
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 {
|
||
|
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))
|
||
5 years ago
|
}
|
||
5 years ago
|
return nil
|
||
|
}
|
||
|
err := app.Run(os.Args)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
5 years ago
|
}
|
||
|
}
|