2022-08-28 13:21:57 +02:00
|
|
|
// Package size provides the size command.
|
2016-08-05 18:12:27 +02:00
|
|
|
package size
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-04-12 16:45:50 +02:00
|
|
|
"encoding/json"
|
2016-08-05 18:12:27 +02:00
|
|
|
"fmt"
|
2018-04-12 16:45:50 +02:00
|
|
|
"os"
|
2016-08-05 18:12:27 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 17:55:04 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-08-05 18:12:27 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-04-12 16:45:50 +02:00
|
|
|
var jsonOutput bool
|
|
|
|
|
2016-08-05 18:12:27 +02:00
|
|
|
func init() {
|
2018-04-12 16:45:50 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 17:55:04 +02:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2021-08-16 11:30:01 +02:00
|
|
|
flags.BoolVarP(cmdFlags, &jsonOutput, "json", "", false, "Format output as JSON")
|
2016-08-05 18:12:27 +02:00
|
|
|
}
|
|
|
|
|
2018-04-12 16:45:50 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-05 18:12:27 +02:00
|
|
|
Use: "size remote:path",
|
|
|
|
Short: `Prints the total size and number of objects in remote:path.`,
|
2022-04-10 13:21:12 +02:00
|
|
|
Long: `
|
|
|
|
Counts objects in the path and calculates the total size. Prints the
|
|
|
|
result to standard output.
|
|
|
|
|
|
|
|
By default the output is in human-readable format, but shows values in
|
|
|
|
both human-readable format as well as the raw numbers (global option
|
|
|
|
` + "`--human-readable`" + ` is not considered). Use option ` + "`--json`" + `
|
|
|
|
to format output as JSON instead.
|
|
|
|
|
|
|
|
Recurses by default, use ` + "`--max-depth 1`" + ` to stop the
|
|
|
|
recursion.
|
|
|
|
|
|
|
|
Some backends do not always provide file sizes, see for example
|
|
|
|
[Google Photos](/googlephotos/#size) and
|
|
|
|
[Google Drive](/drive/#limitations-of-google-docs).
|
|
|
|
Rclone will then show a notice in the log indicating how many such
|
|
|
|
files were encountered, and count them in as empty files in the output
|
|
|
|
of the size command.
|
|
|
|
`,
|
2016-08-05 18:12:27 +02:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2016-12-04 17:52:24 +01:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2018-04-12 16:45:50 +02:00
|
|
|
var err error
|
|
|
|
var results struct {
|
2022-04-06 14:15:07 +02:00
|
|
|
Count int64 `json:"count"`
|
|
|
|
Bytes int64 `json:"bytes"`
|
|
|
|
Sizeless int64 `json:"sizeless"`
|
2018-04-12 16:45:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 14:15:07 +02:00
|
|
|
results.Count, results.Bytes, results.Sizeless, err = operations.Count(context.Background(), fsrc)
|
2016-08-05 18:12:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-06 14:15:07 +02:00
|
|
|
if results.Sizeless > 0 {
|
|
|
|
fs.Logf(fsrc, "Size may be underestimated due to %d objects with unknown size", results.Sizeless)
|
|
|
|
}
|
2018-04-12 16:45:50 +02:00
|
|
|
if jsonOutput {
|
|
|
|
return json.NewEncoder(os.Stdout).Encode(results)
|
|
|
|
}
|
2021-04-02 20:15:56 +02:00
|
|
|
fmt.Printf("Total objects: %s (%d)\n", fs.CountSuffix(results.Count), results.Count)
|
|
|
|
fmt.Printf("Total size: %s (%d Byte)\n", fs.SizeSuffix(results.Bytes).ByteUnit(), results.Bytes)
|
2022-04-06 14:15:07 +02:00
|
|
|
if results.Sizeless > 0 {
|
|
|
|
fmt.Printf("Total objects with unknown size: %s (%d)\n", fs.CountSuffix(results.Sizeless), results.Sizeless)
|
|
|
|
}
|
2016-08-05 18:12:27 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|