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

     1  package hashsum
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/rclone/rclone/cmd"
    10  	"github.com/rclone/rclone/fs/config/flags"
    11  	"github.com/rclone/rclone/fs/hash"
    12  	"github.com/rclone/rclone/fs/operations"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var (
    17  	outputBase64 = false
    18  )
    19  
    20  func init() {
    21  	cmd.Root.AddCommand(commandDefinition)
    22  	cmdFlags := commandDefinition.Flags()
    23  	flags.BoolVarP(cmdFlags, &outputBase64, "base64", "", outputBase64, "Output base64 encoded hashsum")
    24  }
    25  
    26  var commandDefinition = &cobra.Command{
    27  	Use:   "hashsum <hash> remote:path",
    28  	Short: `Produces an hashsum file for all the objects in the path.`,
    29  	Long: `
    30  Produces a hash file for all the objects in the path using the hash
    31  named.  The output is in the same format as the standard
    32  md5sum/sha1sum tool.
    33  
    34  Run without a hash to see the list of supported hashes, eg
    35  
    36      $ rclone hashsum
    37      Supported hashes are:
    38        * MD5
    39        * SHA-1
    40        * DropboxHash
    41        * QuickXorHash
    42  
    43  Then
    44  
    45      $ rclone hashsum MD5 remote:path
    46  `,
    47  	RunE: func(command *cobra.Command, args []string) error {
    48  		cmd.CheckArgs(0, 2, command, args)
    49  		if len(args) == 0 {
    50  			fmt.Printf("Supported hashes are:\n")
    51  			for _, ht := range hash.Supported().Array() {
    52  				fmt.Printf("  * %v\n", ht)
    53  			}
    54  			return nil
    55  		} else if len(args) == 1 {
    56  			return errors.New("need hash type and remote")
    57  		}
    58  		var ht hash.Type
    59  		err := ht.Set(args[0])
    60  		if err != nil {
    61  			return err
    62  		}
    63  		fsrc := cmd.NewFsSrc(args[1:])
    64  		cmd.Run(false, false, command, func() error {
    65  			if outputBase64 {
    66  				return operations.HashListerBase64(context.Background(), ht, fsrc, os.Stdout)
    67  			}
    68  			return operations.HashLister(context.Background(), ht, fsrc, os.Stdout)
    69  		})
    70  		return nil
    71  	},
    72  }