github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/cmd/size/size.go (about) 1 package size 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "os" 8 9 "github.com/ncw/rclone/cmd" 10 "github.com/ncw/rclone/fs" 11 "github.com/ncw/rclone/fs/operations" 12 "github.com/spf13/cobra" 13 ) 14 15 var jsonOutput bool 16 17 func init() { 18 cmd.Root.AddCommand(commandDefinition) 19 commandDefinition.Flags().BoolVar(&jsonOutput, "json", false, "format output as JSON") 20 } 21 22 var commandDefinition = &cobra.Command{ 23 Use: "size remote:path", 24 Short: `Prints the total size and number of objects in remote:path.`, 25 Run: func(command *cobra.Command, args []string) { 26 cmd.CheckArgs(1, 1, command, args) 27 fsrc := cmd.NewFsSrc(args) 28 cmd.Run(false, false, command, func() error { 29 var err error 30 var results struct { 31 Count int64 `json:"count"` 32 Bytes int64 `json:"bytes"` 33 } 34 35 results.Count, results.Bytes, err = operations.Count(context.Background(), fsrc) 36 if err != nil { 37 return err 38 } 39 40 if jsonOutput { 41 return json.NewEncoder(os.Stdout).Encode(results) 42 } 43 44 fmt.Printf("Total objects: %d\n", results.Count) 45 fmt.Printf("Total size: %s (%d Bytes)\n", fs.SizeSuffix(results.Bytes).Unit("Bytes"), results.Bytes) 46 47 return nil 48 }) 49 }, 50 }