2018-04-20 12:33:50 +02:00
|
|
|
package hashsum
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-04-20 12:33:50 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2019-10-26 21:27:33 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2018-04-20 12:33:50 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-10-26 21:27:33 +02:00
|
|
|
var (
|
|
|
|
outputBase64 = false
|
|
|
|
)
|
|
|
|
|
2018-04-20 12:33:50 +02:00
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-26 21:27:33 +02:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
|
|
|
flags.BoolVarP(cmdFlags, &outputBase64, "base64", "", outputBase64, "Output base64 encoded hashsum")
|
2018-04-20 12:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var commandDefinition = &cobra.Command{
|
|
|
|
Use: "hashsum <hash> remote:path",
|
|
|
|
Short: `Produces an hashsum file for all the objects in the path.`,
|
|
|
|
Long: `
|
|
|
|
Produces a hash file for all the objects in the path using the hash
|
|
|
|
named. The output is in the same format as the standard
|
|
|
|
md5sum/sha1sum tool.
|
|
|
|
|
|
|
|
Run without a hash to see the list of supported hashes, eg
|
|
|
|
|
|
|
|
$ rclone hashsum
|
|
|
|
Supported hashes are:
|
|
|
|
* MD5
|
|
|
|
* SHA-1
|
|
|
|
* DropboxHash
|
|
|
|
* QuickXorHash
|
|
|
|
|
|
|
|
Then
|
|
|
|
|
|
|
|
$ rclone hashsum MD5 remote:path
|
|
|
|
`,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(0, 2, command, args)
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Printf("Supported hashes are:\n")
|
2019-09-23 15:32:36 +02:00
|
|
|
for _, ht := range hash.Supported().Array() {
|
2018-04-20 12:33:50 +02:00
|
|
|
fmt.Printf(" * %v\n", ht)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if len(args) == 1 {
|
|
|
|
return errors.New("need hash type and remote")
|
|
|
|
}
|
|
|
|
var ht hash.Type
|
|
|
|
err := ht.Set(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fsrc := cmd.NewFsSrc(args[1:])
|
|
|
|
cmd.Run(false, false, command, func() error {
|
2019-10-26 21:27:33 +02:00
|
|
|
if outputBase64 {
|
|
|
|
return operations.HashListerBase64(context.Background(), ht, fsrc, os.Stdout)
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
return operations.HashLister(context.Background(), ht, fsrc, os.Stdout)
|
2018-04-20 12:33:50 +02:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|