rclone/cmd/cachestats/cachestats.go

56 lines
1.1 KiB
Go
Raw Normal View History

// +build !plan9
2017-11-13 01:54:25 +08:00
package cachestats
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/rclone/rclone/backend/cache"
"github.com/rclone/rclone/cmd"
2017-11-13 01:54:25 +08:00
"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)
fsrc := cmd.NewFsSrc(args)
cmd.Run(false, false, command, func() error {
2017-11-13 01:54:25 +08:00
var fsCache *cache.Fs
fsCache, ok := fsrc.(*cache.Fs)
if !ok {
unwrap := fsrc.Features().UnWrap
if unwrap != nil {
fsCache, ok = unwrap().(*cache.Fs)
}
2017-11-13 01:54:25 +08:00
if !ok {
return errors.Errorf("%s: is not a cache remote", fsrc.Name())
}
}
m, err := fsCache.Stats()
if err != nil {
return err
}
2017-11-13 01:54:25 +08:00
raw, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
fmt.Printf("%s\n", string(raw))
return nil
})
},
}