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

     1  // +build !plan9
     2  
     3  package cachestats
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/rclone/rclone/backend/cache"
    11  	"github.com/rclone/rclone/cmd"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  func init() {
    16  	cmd.Root.AddCommand(commandDefinition)
    17  }
    18  
    19  var commandDefinition = &cobra.Command{
    20  	Use:   "cachestats source:",
    21  	Short: `Print cache stats for a remote`,
    22  	Long: `
    23  Print cache stats for a remote in JSON format
    24  `,
    25  	Run: func(command *cobra.Command, args []string) {
    26  		cmd.CheckArgs(1, 1, command, args)
    27  
    28  		fsrc := cmd.NewFsSrc(args)
    29  		cmd.Run(false, false, command, func() error {
    30  			var fsCache *cache.Fs
    31  			fsCache, ok := fsrc.(*cache.Fs)
    32  			if !ok {
    33  				unwrap := fsrc.Features().UnWrap
    34  				if unwrap != nil {
    35  					fsCache, ok = unwrap().(*cache.Fs)
    36  				}
    37  				if !ok {
    38  					return errors.Errorf("%s: is not a cache remote", fsrc.Name())
    39  				}
    40  			}
    41  			m, err := fsCache.Stats()
    42  			if err != nil {
    43  				return err
    44  			}
    45  
    46  			raw, err := json.MarshalIndent(m, "", "  ")
    47  			if err != nil {
    48  				return err
    49  			}
    50  
    51  			fmt.Printf("%s\n", string(raw))
    52  			return nil
    53  		})
    54  	},
    55  }