sha1sum.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Package sha1sum provides the sha1sum command.
  2. package sha1sum
  3. import (
  4. "context"
  5. "github.com/rclone/rclone/cmd"
  6. "github.com/rclone/rclone/cmd/hashsum"
  7. "github.com/rclone/rclone/fs/hash"
  8. "github.com/rclone/rclone/fs/operations"
  9. "github.com/spf13/cobra"
  10. )
  11. func init() {
  12. cmd.Root.AddCommand(commandDefinition)
  13. cmdFlags := commandDefinition.Flags()
  14. hashsum.AddHashsumFlags(cmdFlags)
  15. }
  16. var commandDefinition = &cobra.Command{
  17. Use: "sha1sum remote:path",
  18. Short: `Produces an sha1sum file for all the objects in the path.`,
  19. Long: `
  20. Produces an sha1sum file for all the objects in the path. This
  21. is in the same format as the standard sha1sum tool produces.
  22. By default, the hash is requested from the remote. If SHA-1 is
  23. not supported by the remote, no hash will be returned. With the
  24. download flag, the file will be downloaded from the remote and
  25. hashed locally enabling SHA-1 for any remote.
  26. For other algorithms, see the [hashsum](/commands/rclone_hashsum/)
  27. command. Running ` + "`rclone sha1sum remote:path`" + ` is equivalent
  28. to running ` + "`rclone hashsum SHA1 remote:path`" + `.
  29. This command can also hash data received on standard input (stdin),
  30. by not passing a remote:path, or by passing a hyphen as remote:path
  31. when there is data to read (if not, the hyphen will be treated literally,
  32. as a relative path).
  33. This command can also hash data received on STDIN, if not passing
  34. a remote:path.
  35. `,
  36. Annotations: map[string]string{
  37. "versionIntroduced": "v1.27",
  38. "groups": "Filter,Listing",
  39. },
  40. RunE: func(command *cobra.Command, args []string) error {
  41. cmd.CheckArgs(0, 1, command, args)
  42. if found, err := hashsum.CreateFromStdinArg(hash.SHA1, args, 0); found {
  43. return err
  44. }
  45. fsrc := cmd.NewFsSrc(args)
  46. cmd.Run(false, false, command, func() error {
  47. if hashsum.ChecksumFile != "" {
  48. fsum, sumFile := cmd.NewFsFile(hashsum.ChecksumFile)
  49. return operations.CheckSum(context.Background(), fsrc, fsum, sumFile, hash.SHA1, nil, hashsum.DownloadFlag)
  50. }
  51. if hashsum.HashsumOutfile == "" {
  52. return operations.HashLister(context.Background(), hash.SHA1, hashsum.OutputBase64, hashsum.DownloadFlag, fsrc, nil)
  53. }
  54. output, close, err := hashsum.GetHashsumOutput(hashsum.HashsumOutfile)
  55. if err != nil {
  56. return err
  57. }
  58. defer close()
  59. return operations.HashLister(context.Background(), hash.SHA1, hashsum.OutputBase64, hashsum.DownloadFlag, fsrc, output)
  60. })
  61. return nil
  62. },
  63. }