From 5f5d0aa4ff3988d81406f2e79e45a32610894c7b Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sat, 10 Dec 2016 18:45:52 +0100
Subject: [PATCH] cmd/swarm: subsume cmd/bzz* as subcommands under swarm

cmd/swarm: subsume cmd/bzz* under cmd/swarm as subcommands
---
 cmd/{bzzhash/main.go => swarm/hash.go} | 19 ++++---
 cmd/{bzzd => swarm}/main.go            | 71 ++++++++++++++++++++++++--
 cmd/{bzzup/main.go => swarm/upload.go} | 27 +++++-----
 3 files changed, 90 insertions(+), 27 deletions(-)
 rename cmd/{bzzhash/main.go => swarm/hash.go} (80%)
 rename cmd/{bzzd => swarm}/main.go (79%)
 rename cmd/{bzzup/main.go => swarm/upload.go} (89%)

diff --git a/cmd/bzzhash/main.go b/cmd/swarm/hash.go
similarity index 80%
rename from cmd/bzzhash/main.go
rename to cmd/swarm/hash.go
index 0ae99acc0..0a20bea82 100644
--- a/cmd/bzzhash/main.go
+++ b/cmd/swarm/hash.go
@@ -19,22 +19,21 @@ package main
 
 import (
 	"fmt"
+	"log"
 	"os"
-	"runtime"
 
 	"github.com/ethereum/go-ethereum/swarm/storage"
+	"gopkg.in/urfave/cli.v1"
 )
 
-func main() {
-	runtime.GOMAXPROCS(runtime.NumCPU())
-
-	if len(os.Args) < 2 {
-		fmt.Println("Usage: bzzhash <file name>")
-		os.Exit(0)
+func hash(ctx *cli.Context) {
+	args := ctx.Args()
+	if len(args) < 1 {
+		log.Fatal("Usage: swarm hash <file name>")
 	}
-	f, err := os.Open(os.Args[1])
+	f, err := os.Open(args[0])
 	if err != nil {
-		fmt.Println("Error opening file " + os.Args[1])
+		fmt.Println("Error opening file " + args[1])
 		os.Exit(1)
 	}
 
@@ -42,7 +41,7 @@ func main() {
 	chunker := storage.NewTreeChunker(storage.NewChunkerParams())
 	key, err := chunker.Split(f, stat.Size(), nil, nil, nil)
 	if err != nil {
-		fmt.Fprintf(os.Stderr, "%v\n", err)
+		log.Fatalf("%v\n", err)
 	} else {
 		fmt.Printf("%v\n", key)
 	}
diff --git a/cmd/bzzd/main.go b/cmd/swarm/main.go
similarity index 79%
rename from cmd/bzzd/main.go
rename to cmd/swarm/main.go
index 4bb2ca04a..a76e29c3b 100644
--- a/cmd/bzzd/main.go
+++ b/cmd/swarm/main.go
@@ -43,11 +43,14 @@ import (
 	"gopkg.in/urfave/cli.v1"
 )
 
-const clientIdentifier = "bzzd"
+const (
+	clientIdentifier = "swarm"
+	versionString    = "0.2"
+)
 
 var (
 	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 (
@@ -85,6 +88,19 @@ var (
 		Usage: "URL of the Ethereum API provider",
 		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{}
@@ -96,8 +112,39 @@ func init() {
 	utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, web3"
 
 	// Set up the cli app.
-	app.Commands = nil
 	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{
 		utils.IdentityFlag,
 		utils.DataDirFlag,
@@ -123,6 +170,10 @@ func init() {
 		SwarmAccountFlag,
 		SwarmNetworkIdFlag,
 		ChequebookAddrFlag,
+		// upload flags
+		SwarmApiFlag,
+		SwarmRecursiveUploadFlag,
+		SwarmWantManifestFlag,
 	}
 	app.Flags = append(app.Flags, debug.Flags...)
 	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 {
 	stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
 	registerBzzService(ctx, stack)
diff --git a/cmd/bzzup/main.go b/cmd/swarm/upload.go
similarity index 89%
rename from cmd/bzzup/main.go
rename to cmd/swarm/upload.go
index 7d251aadb..0aceef20b 100644
--- a/cmd/bzzup/main.go
+++ b/cmd/swarm/upload.go
@@ -20,7 +20,6 @@ package main
 import (
 	"bytes"
 	"encoding/json"
-	"flag"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -30,24 +29,24 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
+
+	"gopkg.in/urfave/cli.v1"
 )
 
-func main() {
+func upload(ctx *cli.Context) {
+	args := ctx.Args()
 	var (
-		bzzapiFlag    = flag.String("bzzapi", "http://127.0.0.1:8500", "Swarm HTTP endpoint")
-		recursiveFlag = flag.Bool("recursive", false, "Upload directories recursively")
-		manifestFlag  = flag.Bool("manifest", true, "Skip automatic manifest upload")
+		bzzapi       = ctx.GlobalString(SwarmApiFlag.Name)
+		recursive    = ctx.GlobalBool(SwarmRecursiveUploadFlag.Name)
+		wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
 	)
-	log.SetOutput(os.Stderr)
-	log.SetFlags(0)
-	flag.Parse()
-	if flag.NArg() != 1 {
+	if len(args) != 1 {
 		log.Fatal("need filename as the first and only argument")
 	}
 
 	var (
-		file   = flag.Arg(0)
-		client = &client{api: *bzzapiFlag}
+		file   = args[0]
+		client = &client{api: bzzapi}
 		mroot  manifest
 	)
 	fi, err := os.Stat(file)
@@ -55,13 +54,13 @@ func main() {
 		log.Fatal(err)
 	}
 	if fi.IsDir() {
-		if !*recursiveFlag {
+		if !recursive {
 			log.Fatal("argument is a directory and recursive upload is disabled")
 		}
 		mroot, err = client.uploadDirectory(file)
 	} else {
 		mroot, err = client.uploadFile(file, fi)
-		if *manifestFlag {
+		if wantManifest {
 			// Wrap the raw file entry in a proper manifest so both hashes get printed.
 			mroot = manifest{Entries: []manifest{mroot}}
 		}
@@ -69,7 +68,7 @@ func main() {
 	if err != nil {
 		log.Fatalln("upload failed:", err)
 	}
-	if *manifestFlag {
+	if wantManifest {
 		hash, err := client.uploadManifest(mroot)
 		if err != nil {
 			log.Fatalln("manifest upload failed:", err)
-- 
GitLab