Skip to content

Commit 5f5d0aa

Browse files
committed
cmd/swarm: subsume cmd/bzz* as subcommands under swarm
cmd/swarm: subsume cmd/bzz* under cmd/swarm as subcommands
1 parent a98e8c0 commit 5f5d0aa

File tree

3 files changed

+90
-27
lines changed

3 files changed

+90
-27
lines changed

cmd/bzzhash/main.go renamed to cmd/swarm/hash.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,29 @@ package main
1919

2020
import (
2121
"fmt"
22+
"log"
2223
"os"
23-
"runtime"
2424

2525
"github.com/ethereum/go-ethereum/swarm/storage"
26+
"gopkg.in/urfave/cli.v1"
2627
)
2728

28-
func main() {
29-
runtime.GOMAXPROCS(runtime.NumCPU())
30-
31-
if len(os.Args) < 2 {
32-
fmt.Println("Usage: bzzhash <file name>")
33-
os.Exit(0)
29+
func hash(ctx *cli.Context) {
30+
args := ctx.Args()
31+
if len(args) < 1 {
32+
log.Fatal("Usage: swarm hash <file name>")
3433
}
35-
f, err := os.Open(os.Args[1])
34+
f, err := os.Open(args[0])
3635
if err != nil {
37-
fmt.Println("Error opening file " + os.Args[1])
36+
fmt.Println("Error opening file " + args[1])
3837
os.Exit(1)
3938
}
4039

4140
stat, _ := f.Stat()
4241
chunker := storage.NewTreeChunker(storage.NewChunkerParams())
4342
key, err := chunker.Split(f, stat.Size(), nil, nil, nil)
4443
if err != nil {
45-
fmt.Fprintf(os.Stderr, "%v\n", err)
44+
log.Fatalf("%v\n", err)
4645
} else {
4746
fmt.Printf("%v\n", key)
4847
}

cmd/bzzd/main.go renamed to cmd/swarm/main.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ import (
4343
"gopkg.in/urfave/cli.v1"
4444
)
4545

46-
const clientIdentifier = "bzzd"
46+
const (
47+
clientIdentifier = "swarm"
48+
versionString = "0.2"
49+
)
4750

4851
var (
4952
gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
50-
app = utils.NewApp(gitCommit, "Ethereum Swarm server daemon")
53+
app = utils.NewApp(gitCommit, "Ethereum Swarm")
5154
)
5255

5356
var (
@@ -85,6 +88,19 @@ var (
8588
Usage: "URL of the Ethereum API provider",
8689
Value: node.DefaultIPCEndpoint("geth"),
8790
}
91+
SwarmApiFlag = cli.StringFlag{
92+
Name: "bzzapi",
93+
Usage: "Swarm HTTP endpoint",
94+
Value: "http://127.0.0.1:8500",
95+
}
96+
SwarmRecursiveUploadFlag = cli.BoolFlag{
97+
Name: "recursive",
98+
Usage: "Upload directories recursively",
99+
}
100+
SwarmWantManifestFlag = cli.BoolTFlag{
101+
Name: "manifest",
102+
Usage: "Automatic manifest upload",
103+
}
88104
)
89105

90106
var defaultBootnodes = []string{}
@@ -96,8 +112,39 @@ func init() {
96112
utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, web3"
97113

98114
// Set up the cli app.
99-
app.Commands = nil
100115
app.Action = bzzd
116+
app.HideVersion = true // we have a command to print the version
117+
app.Copyright = "Copyright 2013-2016 The go-ethereum Authors"
118+
app.Commands = []cli.Command{
119+
cli.Command{
120+
Action: version,
121+
Name: "version",
122+
Usage: "Print version numbers",
123+
ArgsUsage: " ",
124+
Description: `
125+
The output of this command is supposed to be machine-readable.
126+
`,
127+
},
128+
cli.Command{
129+
Action: upload,
130+
Name: "up",
131+
Usage: "upload a file or directory to swarm using the HTTP API",
132+
ArgsUsage: " <file>",
133+
Description: `
134+
"upload a file or directory to swarm using the HTTP API and prints the root hash",
135+
`,
136+
},
137+
cli.Command{
138+
Action: hash,
139+
Name: "hash",
140+
Usage: "print the swarm hash of a file or directory",
141+
ArgsUsage: " <file>",
142+
Description: `
143+
Prints the swarm hash of file or directory.
144+
`,
145+
},
146+
}
147+
101148
app.Flags = []cli.Flag{
102149
utils.IdentityFlag,
103150
utils.DataDirFlag,
@@ -123,6 +170,10 @@ func init() {
123170
SwarmAccountFlag,
124171
SwarmNetworkIdFlag,
125172
ChequebookAddrFlag,
173+
// upload flags
174+
SwarmApiFlag,
175+
SwarmRecursiveUploadFlag,
176+
SwarmWantManifestFlag,
126177
}
127178
app.Flags = append(app.Flags, debug.Flags...)
128179
app.Before = func(ctx *cli.Context) error {
@@ -142,6 +193,20 @@ func main() {
142193
}
143194
}
144195

196+
func version(ctx *cli.Context) error {
197+
fmt.Println(strings.Title(clientIdentifier))
198+
fmt.Println("Version:", versionString)
199+
if gitCommit != "" {
200+
fmt.Println("Git Commit:", gitCommit)
201+
}
202+
fmt.Println("Network Id:", ctx.GlobalInt(utils.NetworkIdFlag.Name))
203+
fmt.Println("Go Version:", runtime.Version())
204+
fmt.Println("OS:", runtime.GOOS)
205+
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
206+
fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
207+
return nil
208+
}
209+
145210
func bzzd(ctx *cli.Context) error {
146211
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
147212
registerBzzService(ctx, stack)

cmd/bzzup/main.go renamed to cmd/swarm/upload.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package main
2020
import (
2121
"bytes"
2222
"encoding/json"
23-
"flag"
2423
"fmt"
2524
"io"
2625
"io/ioutil"
@@ -30,46 +29,46 @@ import (
3029
"os"
3130
"path/filepath"
3231
"strings"
32+
33+
"gopkg.in/urfave/cli.v1"
3334
)
3435

35-
func main() {
36+
func upload(ctx *cli.Context) {
37+
args := ctx.Args()
3638
var (
37-
bzzapiFlag = flag.String("bzzapi", "http://127.0.0.1:8500", "Swarm HTTP endpoint")
38-
recursiveFlag = flag.Bool("recursive", false, "Upload directories recursively")
39-
manifestFlag = flag.Bool("manifest", true, "Skip automatic manifest upload")
39+
bzzapi = ctx.GlobalString(SwarmApiFlag.Name)
40+
recursive = ctx.GlobalBool(SwarmRecursiveUploadFlag.Name)
41+
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
4042
)
41-
log.SetOutput(os.Stderr)
42-
log.SetFlags(0)
43-
flag.Parse()
44-
if flag.NArg() != 1 {
43+
if len(args) != 1 {
4544
log.Fatal("need filename as the first and only argument")
4645
}
4746

4847
var (
49-
file = flag.Arg(0)
50-
client = &client{api: *bzzapiFlag}
48+
file = args[0]
49+
client = &client{api: bzzapi}
5150
mroot manifest
5251
)
5352
fi, err := os.Stat(file)
5453
if err != nil {
5554
log.Fatal(err)
5655
}
5756
if fi.IsDir() {
58-
if !*recursiveFlag {
57+
if !recursive {
5958
log.Fatal("argument is a directory and recursive upload is disabled")
6059
}
6160
mroot, err = client.uploadDirectory(file)
6261
} else {
6362
mroot, err = client.uploadFile(file, fi)
64-
if *manifestFlag {
63+
if wantManifest {
6564
// Wrap the raw file entry in a proper manifest so both hashes get printed.
6665
mroot = manifest{Entries: []manifest{mroot}}
6766
}
6867
}
6968
if err != nil {
7069
log.Fatalln("upload failed:", err)
7170
}
72-
if *manifestFlag {
71+
if wantManifest {
7372
hash, err := client.uploadManifest(mroot)
7473
if err != nil {
7574
log.Fatalln("manifest upload failed:", err)

0 commit comments

Comments
 (0)