github.com/artpar/rclone@v1.67.3/cmd/cachestats/cachestats.go (about)

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