github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/cmd/hashsum/hashsum.go (about)

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