1
mirror of https://github.com/rclone/rclone synced 2024-11-28 06:41:41 +01:00

size: Add --json flag

This commit is contained in:
Matthew Holt 2018-04-12 08:45:50 -06:00 committed by Nick Craig-Wood
parent 2015f98f0c
commit 9e4cd55477

View File

@ -1,7 +1,9 @@
package size package size
import ( import (
"encoding/json"
"fmt" "fmt"
"os"
"github.com/ncw/rclone/cmd" "github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
@ -9,23 +11,38 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var jsonOutput bool
func init() { func init() {
cmd.Root.AddCommand(commandDefintion) cmd.Root.AddCommand(commandDefinition)
commandDefinition.Flags().BoolVar(&jsonOutput, "json", false, "format output as JSON")
} }
var commandDefintion = &cobra.Command{ var commandDefinition = &cobra.Command{
Use: "size remote:path", Use: "size remote:path",
Short: `Prints the total size and number of objects in remote:path.`, Short: `Prints the total size and number of objects in remote:path.`,
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
fsrc := cmd.NewFsSrc(args) fsrc := cmd.NewFsSrc(args)
cmd.Run(false, false, command, func() error { cmd.Run(false, false, command, func() error {
objects, size, err := operations.Count(fsrc) var err error
var results struct {
Count int64 `json:"count"`
Bytes int64 `json:"bytes"`
}
results.Count, results.Bytes, err = operations.Count(fsrc)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("Total objects: %d\n", objects)
fmt.Printf("Total size: %s (%d Bytes)\n", fs.SizeSuffix(size).Unit("Bytes"), size) if jsonOutput {
return json.NewEncoder(os.Stdout).Encode(results)
}
fmt.Printf("Total objects: %d\n", results.Count)
fmt.Printf("Total size: %s (%d Bytes)\n", fs.SizeSuffix(results.Bytes).Unit("Bytes"), results.Bytes)
return nil return nil
}) })
}, },