2016-08-05 16:12:27 +00:00
|
|
|
package size
|
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2018-04-12 14:45:50 +00:00
|
|
|
"encoding/json"
|
2016-08-05 16:12:27 +00:00
|
|
|
"fmt"
|
2018-04-12 14:45:50 +00:00
|
|
|
"os"
|
2016-08-05 16:12:27 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 15:55:04 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-08-05 16:12:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-04-12 14:45:50 +00:00
|
|
|
var jsonOutput bool
|
|
|
|
|
2016-08-05 16:12:27 +00:00
|
|
|
func init() {
|
2018-04-12 14:45:50 +00:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 15:55:04 +00:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2021-08-16 09:30:01 +00:00
|
|
|
flags.BoolVarP(cmdFlags, &jsonOutput, "json", "", false, "Format output as JSON")
|
2016-08-05 16:12:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 14:45:50 +00:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-05 16:12:27 +00: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-04 16:52:24 +00:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2018-04-12 14:45:50 +00:00
|
|
|
var err error
|
|
|
|
var results struct {
|
|
|
|
Count int64 `json:"count"`
|
|
|
|
Bytes int64 `json:"bytes"`
|
|
|
|
}
|
|
|
|
|
2019-06-17 08:34:30 +00:00
|
|
|
results.Count, results.Bytes, err = operations.Count(context.Background(), fsrc)
|
2016-08-05 16:12:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-12 14:45:50 +00:00
|
|
|
|
|
|
|
if jsonOutput {
|
|
|
|
return json.NewEncoder(os.Stdout).Encode(results)
|
|
|
|
}
|
2021-04-02 18:15:56 +00: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)
|
2016-08-05 16:12:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|