2018-04-06 21:33:51 +02:00
|
|
|
// +build !plan9
|
2017-11-12 18:54:25 +01:00
|
|
|
|
|
|
|
package cachestats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2018-01-11 17:05:41 +01:00
|
|
|
"github.com/ncw/rclone/backend/cache"
|
2017-11-12 18:54:25 +01:00
|
|
|
"github.com/ncw/rclone/cmd"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/config"
|
2017-11-12 18:54:25 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
|
|
|
}
|
|
|
|
|
|
|
|
var commandDefinition = &cobra.Command{
|
|
|
|
Use: "cachestats source:",
|
|
|
|
Short: `Print cache stats for a remote`,
|
|
|
|
Long: `
|
|
|
|
Print cache stats for a remote in JSON format
|
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
|
|
|
|
_, configName, _, err := fs.ParseRemote(args[0])
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf("cachestats", "%s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
if !config.FileGetBool(configName, "read_only", false) {
|
|
|
|
config.FileSet(configName, "read_only", "true")
|
|
|
|
defer config.FileDeleteKey(configName, "read_only")
|
2017-11-12 18:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2017-11-24 11:21:23 +01:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2017-11-12 18:54:25 +01:00
|
|
|
var fsCache *cache.Fs
|
|
|
|
fsCache, ok := fsrc.(*cache.Fs)
|
|
|
|
if !ok {
|
2017-11-24 11:21:23 +01:00
|
|
|
unwrap := fsrc.Features().UnWrap
|
|
|
|
if unwrap != nil {
|
|
|
|
fsCache, ok = unwrap().(*cache.Fs)
|
|
|
|
}
|
2017-11-12 18:54:25 +01:00
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("%s: is not a cache remote", fsrc.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m, err := fsCache.Stats()
|
2018-05-04 16:19:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-12 18:54:25 +01:00
|
|
|
|
|
|
|
raw, err := json.MarshalIndent(m, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s\n", string(raw))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|