2016-08-06 00:12:27 +08:00
|
|
|
package size
|
|
|
|
|
|
|
|
import (
|
2018-04-12 22:45:50 +08:00
|
|
|
"encoding/json"
|
2016-08-06 00:12:27 +08:00
|
|
|
"fmt"
|
2018-04-12 22:45:50 +08:00
|
|
|
"os"
|
2016-08-06 00:12:27 +08:00
|
|
|
|
|
|
|
"github.com/ncw/rclone/cmd"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-13 00:30:54 +08:00
|
|
|
"github.com/ncw/rclone/fs/operations"
|
2016-08-06 00:12:27 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-04-12 22:45:50 +08:00
|
|
|
var jsonOutput bool
|
|
|
|
|
2016-08-06 00:12:27 +08:00
|
|
|
func init() {
|
2018-04-12 22:45:50 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
|
|
|
commandDefinition.Flags().BoolVar(&jsonOutput, "json", false, "format output as JSON")
|
2016-08-06 00:12:27 +08:00
|
|
|
}
|
|
|
|
|
2018-04-12 22:45:50 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-06 00:12:27 +08:00
|
|
|
Use: "size remote:path",
|
|
|
|
Short: `Prints the total size and number of objects in remote:path.`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2016-12-05 00:52:24 +08:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2018-04-12 22:45:50 +08:00
|
|
|
var err error
|
|
|
|
var results struct {
|
|
|
|
Count int64 `json:"count"`
|
|
|
|
Bytes int64 `json:"bytes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Count, results.Bytes, err = operations.Count(fsrc)
|
2016-08-06 00:12:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-12 22:45:50 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-08-06 00:12:27 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|