github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/cmd/about/about.go (about)

     1  package about
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/rclone/rclone/cmd"
    11  	"github.com/rclone/rclone/fs"
    12  	"github.com/rclone/rclone/fs/config/flags"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var (
    17  	jsonOutput bool
    18  	fullOutput bool
    19  )
    20  
    21  func init() {
    22  	cmd.Root.AddCommand(commandDefinition)
    23  	cmdFlags := commandDefinition.Flags()
    24  	flags.BoolVarP(cmdFlags, &jsonOutput, "json", "", false, "Format output as JSON")
    25  	flags.BoolVarP(cmdFlags, &fullOutput, "full", "", false, "Full numbers instead of SI units")
    26  }
    27  
    28  // printValue formats uv to be output
    29  func printValue(what string, uv *int64) {
    30  	what += ":"
    31  	if uv == nil {
    32  		return
    33  	}
    34  	var val string
    35  	if fullOutput {
    36  		val = fmt.Sprintf("%d", *uv)
    37  	} else {
    38  		val = fs.SizeSuffix(*uv).String()
    39  	}
    40  	fmt.Printf("%-9s%v\n", what, val)
    41  }
    42  
    43  var commandDefinition = &cobra.Command{
    44  	Use:   "about remote:",
    45  	Short: `Get quota information from the remote.`,
    46  	Long: `
    47  Get quota information from the remote, like bytes used/free/quota and bytes
    48  used in the trash. Not supported by all remotes.
    49  
    50  This will print to stdout something like this:
    51  
    52      Total:   17G
    53      Used:    7.444G
    54      Free:    1.315G
    55      Trashed: 100.000M
    56      Other:   8.241G
    57  
    58  Where the fields are:
    59  
    60    * Total: total size available.
    61    * Used: total size used
    62    * Free: total amount this user could upload.
    63    * Trashed: total amount in the trash
    64    * Other: total amount in other storage (eg Gmail, Google Photos)
    65    * Objects: total number of objects in the storage
    66  
    67  Note that not all the backends provide all the fields - they will be
    68  missing if they are not known for that backend.  Where it is known
    69  that the value is unlimited the value will also be omitted.
    70  
    71  Use the --full flag to see the numbers written out in full, eg
    72  
    73      Total:   18253611008
    74      Used:    7993453766
    75      Free:    1411001220
    76      Trashed: 104857602
    77      Other:   8849156022
    78  
    79  Use the --json flag for a computer readable output, eg
    80  
    81      {
    82          "total": 18253611008,
    83          "used": 7993453766,
    84          "trashed": 104857602,
    85          "other": 8849156022,
    86          "free": 1411001220
    87      }
    88  `,
    89  	Run: func(command *cobra.Command, args []string) {
    90  		cmd.CheckArgs(1, 1, command, args)
    91  		f := cmd.NewFsSrc(args)
    92  		cmd.Run(false, false, command, func() error {
    93  			doAbout := f.Features().About
    94  			if doAbout == nil {
    95  				return errors.Errorf("%v doesn't support about", f)
    96  			}
    97  			u, err := doAbout(context.Background())
    98  			if err != nil {
    99  				return errors.Wrap(err, "About call failed")
   100  			}
   101  			if u == nil {
   102  				return errors.New("nil usage returned")
   103  			}
   104  			if jsonOutput {
   105  				out := json.NewEncoder(os.Stdout)
   106  				out.SetIndent("", "\t")
   107  				return out.Encode(u)
   108  			}
   109  			printValue("Total", u.Total)
   110  			printValue("Used", u.Used)
   111  			printValue("Free", u.Free)
   112  			printValue("Trashed", u.Trashed)
   113  			printValue("Other", u.Other)
   114  			printValue("Objects", u.Objects)
   115  			return nil
   116  		})
   117  	},
   118  }