github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/cmd/swarm/hash.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Command bzzhash computes a swarm tree hash.
    18  package main
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"os"
    24  
    25  	"github.com/ethereum/go-ethereum/cmd/utils"
    26  	"github.com/ethereum/go-ethereum/swarm/storage"
    27  	"gopkg.in/urfave/cli.v1"
    28  )
    29  
    30  var hashCommand = cli.Command{
    31  	Action:             hash,
    32  	CustomHelpTemplate: helpTemplate,
    33  	Name:               "hash",
    34  	Usage:              "print the swarm hash of a file or directory",
    35  	ArgsUsage:          "<file>",
    36  	Description:        "Prints the swarm hash of file or directory",
    37  }
    38  
    39  func hash(ctx *cli.Context) {
    40  	args := ctx.Args()
    41  	if len(args) < 1 {
    42  		utils.Fatalf("Usage: swarm hash <file name>")
    43  	}
    44  	f, err := os.Open(args[0])
    45  	if err != nil {
    46  		utils.Fatalf("Error opening file " + args[1])
    47  	}
    48  	defer f.Close()
    49  
    50  	stat, _ := f.Stat()
    51  	fileStore := storage.NewFileStore(&storage.FakeChunkStore{}, storage.NewFileStoreParams())
    52  	addr, _, err := fileStore.Store(context.TODO(), f, stat.Size(), false)
    53  	if err != nil {
    54  		utils.Fatalf("%v\n", err)
    55  	} else {
    56  		fmt.Printf("%v\n", addr)
    57  	}
    58  }