cmd/swarm: subsume cmd/bzz* as subcommands under swarm

cmd/swarm: subsume cmd/bzz* under cmd/swarm as subcommands
This commit is contained in:
zelig 2016-12-10 18:45:52 +01:00
parent a98e8c0889
commit 5f5d0aa4ff
3 changed files with 90 additions and 27 deletions

@ -19,22 +19,21 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"runtime"
"github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/storage"
"gopkg.in/urfave/cli.v1"
) )
func main() { func hash(ctx *cli.Context) {
runtime.GOMAXPROCS(runtime.NumCPU()) args := ctx.Args()
if len(args) < 1 {
if len(os.Args) < 2 { log.Fatal("Usage: swarm hash <file name>")
fmt.Println("Usage: bzzhash <file name>")
os.Exit(0)
} }
f, err := os.Open(os.Args[1]) f, err := os.Open(args[0])
if err != nil { if err != nil {
fmt.Println("Error opening file " + os.Args[1]) fmt.Println("Error opening file " + args[1])
os.Exit(1) os.Exit(1)
} }
@ -42,7 +41,7 @@ func main() {
chunker := storage.NewTreeChunker(storage.NewChunkerParams()) chunker := storage.NewTreeChunker(storage.NewChunkerParams())
key, err := chunker.Split(f, stat.Size(), nil, nil, nil) key, err := chunker.Split(f, stat.Size(), nil, nil, nil)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) log.Fatalf("%v\n", err)
} else { } else {
fmt.Printf("%v\n", key) fmt.Printf("%v\n", key)
} }

@ -43,11 +43,14 @@ import (
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
const clientIdentifier = "bzzd" const (
clientIdentifier = "swarm"
versionString = "0.2"
)
var ( var (
gitCommit string // Git SHA1 commit hash of the release (set via linker flags) gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
app = utils.NewApp(gitCommit, "Ethereum Swarm server daemon") app = utils.NewApp(gitCommit, "Ethereum Swarm")
) )
var ( var (
@ -85,6 +88,19 @@ var (
Usage: "URL of the Ethereum API provider", Usage: "URL of the Ethereum API provider",
Value: node.DefaultIPCEndpoint("geth"), Value: node.DefaultIPCEndpoint("geth"),
} }
SwarmApiFlag = cli.StringFlag{
Name: "bzzapi",
Usage: "Swarm HTTP endpoint",
Value: "http://127.0.0.1:8500",
}
SwarmRecursiveUploadFlag = cli.BoolFlag{
Name: "recursive",
Usage: "Upload directories recursively",
}
SwarmWantManifestFlag = cli.BoolTFlag{
Name: "manifest",
Usage: "Automatic manifest upload",
}
) )
var defaultBootnodes = []string{} var defaultBootnodes = []string{}
@ -96,8 +112,39 @@ func init() {
utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, web3" utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, web3"
// Set up the cli app. // Set up the cli app.
app.Commands = nil
app.Action = bzzd app.Action = bzzd
app.HideVersion = true // we have a command to print the version
app.Copyright = "Copyright 2013-2016 The go-ethereum Authors"
app.Commands = []cli.Command{
cli.Command{
Action: version,
Name: "version",
Usage: "Print version numbers",
ArgsUsage: " ",
Description: `
The output of this command is supposed to be machine-readable.
`,
},
cli.Command{
Action: upload,
Name: "up",
Usage: "upload a file or directory to swarm using the HTTP API",
ArgsUsage: " <file>",
Description: `
"upload a file or directory to swarm using the HTTP API and prints the root hash",
`,
},
cli.Command{
Action: hash,
Name: "hash",
Usage: "print the swarm hash of a file or directory",
ArgsUsage: " <file>",
Description: `
Prints the swarm hash of file or directory.
`,
},
}
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
utils.IdentityFlag, utils.IdentityFlag,
utils.DataDirFlag, utils.DataDirFlag,
@ -123,6 +170,10 @@ func init() {
SwarmAccountFlag, SwarmAccountFlag,
SwarmNetworkIdFlag, SwarmNetworkIdFlag,
ChequebookAddrFlag, ChequebookAddrFlag,
// upload flags
SwarmApiFlag,
SwarmRecursiveUploadFlag,
SwarmWantManifestFlag,
} }
app.Flags = append(app.Flags, debug.Flags...) app.Flags = append(app.Flags, debug.Flags...)
app.Before = func(ctx *cli.Context) error { app.Before = func(ctx *cli.Context) error {
@ -142,6 +193,20 @@ func main() {
} }
} }
func version(ctx *cli.Context) error {
fmt.Println(strings.Title(clientIdentifier))
fmt.Println("Version:", versionString)
if gitCommit != "" {
fmt.Println("Git Commit:", gitCommit)
}
fmt.Println("Network Id:", ctx.GlobalInt(utils.NetworkIdFlag.Name))
fmt.Println("Go Version:", runtime.Version())
fmt.Println("OS:", runtime.GOOS)
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
return nil
}
func bzzd(ctx *cli.Context) error { func bzzd(ctx *cli.Context) error {
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
registerBzzService(ctx, stack) registerBzzService(ctx, stack)

@ -20,7 +20,6 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -30,24 +29,24 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"gopkg.in/urfave/cli.v1"
) )
func main() { func upload(ctx *cli.Context) {
args := ctx.Args()
var ( var (
bzzapiFlag = flag.String("bzzapi", "http://127.0.0.1:8500", "Swarm HTTP endpoint") bzzapi = ctx.GlobalString(SwarmApiFlag.Name)
recursiveFlag = flag.Bool("recursive", false, "Upload directories recursively") recursive = ctx.GlobalBool(SwarmRecursiveUploadFlag.Name)
manifestFlag = flag.Bool("manifest", true, "Skip automatic manifest upload") wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
) )
log.SetOutput(os.Stderr) if len(args) != 1 {
log.SetFlags(0)
flag.Parse()
if flag.NArg() != 1 {
log.Fatal("need filename as the first and only argument") log.Fatal("need filename as the first and only argument")
} }
var ( var (
file = flag.Arg(0) file = args[0]
client = &client{api: *bzzapiFlag} client = &client{api: bzzapi}
mroot manifest mroot manifest
) )
fi, err := os.Stat(file) fi, err := os.Stat(file)
@ -55,13 +54,13 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
if fi.IsDir() { if fi.IsDir() {
if !*recursiveFlag { if !recursive {
log.Fatal("argument is a directory and recursive upload is disabled") log.Fatal("argument is a directory and recursive upload is disabled")
} }
mroot, err = client.uploadDirectory(file) mroot, err = client.uploadDirectory(file)
} else { } else {
mroot, err = client.uploadFile(file, fi) mroot, err = client.uploadFile(file, fi)
if *manifestFlag { if wantManifest {
// Wrap the raw file entry in a proper manifest so both hashes get printed. // Wrap the raw file entry in a proper manifest so both hashes get printed.
mroot = manifest{Entries: []manifest{mroot}} mroot = manifest{Entries: []manifest{mroot}}
} }
@ -69,7 +68,7 @@ func main() {
if err != nil { if err != nil {
log.Fatalln("upload failed:", err) log.Fatalln("upload failed:", err)
} }
if *manifestFlag { if wantManifest {
hash, err := client.uploadManifest(mroot) hash, err := client.uploadManifest(mroot)
if err != nil { if err != nil {
log.Fatalln("manifest upload failed:", err) log.Fatalln("manifest upload failed:", err)