github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/swarm/hash.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:33</date>
    10  //</624450071022276608>
    11  
    12  
    13  //
    14  package main
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/ethereum/go-ethereum/cmd/utils"
    22  	"github.com/ethereum/go-ethereum/swarm/storage"
    23  	"gopkg.in/urfave/cli.v1"
    24  )
    25  
    26  var hashCommand = cli.Command{
    27  	Action:             hash,
    28  	CustomHelpTemplate: helpTemplate,
    29  	Name:               "hash",
    30  	Usage:              "print the swarm hash of a file or directory",
    31  	ArgsUsage:          "<file>",
    32  	Description:        "Prints the swarm hash of file or directory",
    33  }
    34  
    35  func hash(ctx *cli.Context) {
    36  	args := ctx.Args()
    37  	if len(args) < 1 {
    38  		utils.Fatalf("Usage: swarm hash <file name>")
    39  	}
    40  	f, err := os.Open(args[0])
    41  	if err != nil {
    42  		utils.Fatalf("Error opening file " + args[1])
    43  	}
    44  	defer f.Close()
    45  
    46  	stat, _ := f.Stat()
    47  	fileStore := storage.NewFileStore(&storage.FakeChunkStore{}, storage.NewFileStoreParams())
    48  	addr, _, err := fileStore.Store(context.TODO(), f, stat.Size(), false)
    49  	if err != nil {
    50  		utils.Fatalf("%v\n", err)
    51  	} else {
    52  		fmt.Printf("%v\n", addr)
    53  	}
    54  }
    55