rclone/cmd/size/size.go

57 lines
1.6 KiB
Go
Raw Normal View History

package size
import (
"context"
2018-04-12 16:45:50 +02:00
"encoding/json"
"fmt"
2018-04-12 16:45:50 +02:00
"os"
"github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/operations"
"github.com/spf13/cobra"
)
2018-04-12 16:45:50 +02:00
var jsonOutput bool
func init() {
2018-04-12 16:45:50 +02:00
cmd.Root.AddCommand(commandDefinition)
cmdFlags := commandDefinition.Flags()
2021-08-16 11:30:01 +02:00
flags.BoolVarP(cmdFlags, &jsonOutput, "json", "", false, "Format output as JSON")
}
2018-04-12 16:45:50 +02:00
var commandDefinition = &cobra.Command{
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)
cmd.Run(false, false, command, func() error {
2018-04-12 16:45:50 +02:00
var err error
var results struct {
Count int64 `json:"count"`
Bytes int64 `json:"bytes"`
Sizeless int64 `json:"sizeless"`
2018-04-12 16:45:50 +02:00
}
results.Count, results.Bytes, results.Sizeless, err = operations.Count(context.Background(), fsrc)
if err != nil {
return err
}
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)
if results.Sizeless > 0 {
fmt.Printf("Total objects with unknown size: %s (%d)\n", fs.CountSuffix(results.Sizeless), results.Sizeless)
}
return nil
})
},
}