Skip to content

Commit 49c6f10

Browse files
authored
Merge pull request #3421 from ethersphere/s/swarm-cmd
cmd/swarm: one command with subcommands
2 parents 4d960f6 + 92224d2 commit 49c6f10

File tree

6 files changed

+178
-76
lines changed

6 files changed

+178
-76
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ The go-ethereum project comes with several wrappers/executables found in the `cm
3939
| `evm` | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow insolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug`). |
4040
| `gethrpctest` | Developer utility tool to support our [ethereum/rpc-test](https://github.com/ethereum/rpc-tests) test suite which validates baseline conformity to the [Ethereum JSON RPC](https://github.com/ethereum/wiki/wiki/JSON-RPC) specs. Please see the [test suite's readme](https://github.com/ethereum/rpc-tests/blob/master/README.md) for details. |
4141
| `rlpdump` | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://github.com/ethereum/wiki/wiki/RLP)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). |
42-
| `bzzd` | swarm daemon. This is the entrypoint for the swarm network. `bzzd --help` for command line options. See https://swarm-guide.readthedocs.io for swarm documentation. |
43-
| `bzzup` | swarm command line file uploader. `bzzup --help` for command line options |
44-
| `bzzhash` | command to calculate the swarm hash of a file or directory. `bzzhash --help` for command line options |
42+
| `swarm` | swarm daemon and tools. This is the entrypoint for the swarm network. `swarm --help` for command line options and subcommands. See https://swarm-guide.readthedocs.io for swarm documentation. |
4543

4644
## Running geth
4745

build/ci.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ var (
7272
executablePath("abigen"),
7373
executablePath("evm"),
7474
executablePath("geth"),
75-
executablePath("bzzd"),
76-
executablePath("bzzhash"),
77-
executablePath("bzzup"),
75+
executablePath("swarm"),
7876
executablePath("rlpdump"),
7977
}
8078

@@ -93,16 +91,8 @@ var (
9391
Description: "Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode.",
9492
},
9593
{
96-
Name: "bzzd",
97-
Description: "Ethereum Swarm daemon",
98-
},
99-
{
100-
Name: "bzzup",
101-
Description: "Ethereum Swarm command line file/directory uploader",
102-
},
103-
{
104-
Name: "bzzhash",
105-
Description: "Ethereum Swarm file/directory hash calculator",
94+
Name: "swarm",
95+
Description: "Ethereum Swarm daemon and tools",
10696
},
10797
{
10898
Name: "abigen",

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: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ 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 (
49-
gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
50-
app = utils.NewApp(gitCommit, "Ethereum Swarm server daemon")
52+
gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
53+
app = utils.NewApp(gitCommit, "Ethereum Swarm")
54+
testbetBootNodes = []string{
55+
"enode://ec8ae764f7cb0417bdfb009b9d0f18ab3818a3a4e8e7c67dd5f18971a93510a2e6f43cd0b69a27e439a9629457ea804104f37c85e41eed057d3faabbf7744cdf@13.74.157.139:30429",
56+
"enode://c2e1fceb3bf3be19dff71eec6cccf19f2dbf7567ee017d130240c670be8594bc9163353ca55dd8df7a4f161dd94b36d0615c17418b5a3cdcbb4e9d99dfa4de37@13.74.157.139:30430",
57+
"enode://fe29b82319b734ce1ec68b84657d57145fee237387e63273989d354486731e59f78858e452ef800a020559da22dcca759536e6aa5517c53930d29ce0b1029286@13.74.157.139:30431",
58+
"enode://1d7187e7bde45cf0bee489ce9852dd6d1a0d9aa67a33a6b8e6db8a4fbc6fcfa6f0f1a5419343671521b863b187d1c73bad3603bae66421d157ffef357669ddb8@13.74.157.139:30432",
59+
"enode://0e4cba800f7b1ee73673afa6a4acead4018f0149d2e3216be3f133318fd165b324cd71b81fbe1e80deac8dbf56e57a49db7be67f8b9bc81bd2b7ee496434fb5d@13.74.157.139:30433",
60+
}
5161
)
5262

5363
var (
@@ -65,7 +75,7 @@ var (
6575
}
6676
SwarmNetworkIdFlag = cli.IntFlag{
6777
Name: "bzznetworkid",
68-
Usage: "Network identifier (integer, default 322=swarm testnet)",
78+
Usage: "Network identifier (integer, default 3=swarm testnet)",
6979
Value: network.NetworkId,
7080
}
7181
SwarmConfigPathFlag = cli.StringFlag{
@@ -85,19 +95,65 @@ var (
8595
Usage: "URL of the Ethereum API provider",
8696
Value: node.DefaultIPCEndpoint("geth"),
8797
}
98+
SwarmApiFlag = cli.StringFlag{
99+
Name: "bzzapi",
100+
Usage: "Swarm HTTP endpoint",
101+
Value: "http://127.0.0.1:8500",
102+
}
103+
SwarmRecursiveUploadFlag = cli.BoolFlag{
104+
Name: "recursive",
105+
Usage: "Upload directories recursively",
106+
}
107+
SwarmWantManifestFlag = cli.BoolTFlag{
108+
Name: "manifest",
109+
Usage: "Automatic manifest upload",
110+
}
111+
SwarmUploadDefaultPath = cli.StringFlag{
112+
Name: "defaultpath",
113+
Usage: "path to file served for empty url path (none)",
114+
}
88115
)
89116

90-
var defaultBootnodes = []string{}
91-
92117
func init() {
93118
// Override flag defaults so bzzd can run alongside geth.
94119
utils.ListenPortFlag.Value = 30399
95120
utils.IPCPathFlag.Value = utils.DirectoryString{Value: "bzzd.ipc"}
96121
utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, web3"
97122

98123
// Set up the cli app.
99-
app.Commands = nil
100124
app.Action = bzzd
125+
app.HideVersion = true // we have a command to print the version
126+
app.Copyright = "Copyright 2013-2016 The go-ethereum Authors"
127+
app.Commands = []cli.Command{
128+
cli.Command{
129+
Action: version,
130+
Name: "version",
131+
Usage: "Print version numbers",
132+
ArgsUsage: " ",
133+
Description: `
134+
The output of this command is supposed to be machine-readable.
135+
`,
136+
},
137+
cli.Command{
138+
Action: upload,
139+
Name: "up",
140+
Usage: "upload a file or directory to swarm using the HTTP API",
141+
ArgsUsage: " <file>",
142+
Description: `
143+
"upload a file or directory to swarm using the HTTP API and prints the root hash",
144+
`,
145+
},
146+
cli.Command{
147+
Action: hash,
148+
Name: "hash",
149+
Usage: "print the swarm hash of a file or directory",
150+
ArgsUsage: " <file>",
151+
Description: `
152+
Prints the swarm hash of file or directory.
153+
`,
154+
},
155+
}
156+
101157
app.Flags = []cli.Flag{
102158
utils.IdentityFlag,
103159
utils.DataDirFlag,
@@ -123,6 +179,11 @@ func init() {
123179
SwarmAccountFlag,
124180
SwarmNetworkIdFlag,
125181
ChequebookAddrFlag,
182+
// upload flags
183+
SwarmApiFlag,
184+
SwarmRecursiveUploadFlag,
185+
SwarmWantManifestFlag,
186+
SwarmUploadDefaultPath,
126187
}
127188
app.Flags = append(app.Flags, debug.Flags...)
128189
app.Before = func(ctx *cli.Context) error {
@@ -142,17 +203,33 @@ func main() {
142203
}
143204
}
144205

206+
func version(ctx *cli.Context) error {
207+
fmt.Println(strings.Title(clientIdentifier))
208+
fmt.Println("Version:", versionString)
209+
if gitCommit != "" {
210+
fmt.Println("Git Commit:", gitCommit)
211+
}
212+
fmt.Println("Network Id:", ctx.GlobalInt(utils.NetworkIdFlag.Name))
213+
fmt.Println("Go Version:", runtime.Version())
214+
fmt.Println("OS:", runtime.GOOS)
215+
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
216+
fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
217+
return nil
218+
}
219+
145220
func bzzd(ctx *cli.Context) error {
146221
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
147222
registerBzzService(ctx, stack)
148223
utils.StartNode(stack)
149-
224+
networkId := ctx.GlobalUint64(SwarmNetworkIdFlag.Name)
150225
// Add bootnodes as initial peers.
151226
if ctx.GlobalIsSet(utils.BootnodesFlag.Name) {
152227
bootnodes := strings.Split(ctx.GlobalString(utils.BootnodesFlag.Name), ",")
153228
injectBootnodes(stack.Server(), bootnodes)
154229
} else {
155-
injectBootnodes(stack.Server(), defaultBootnodes)
230+
if networkId == 3 {
231+
injectBootnodes(stack.Server(), testbetBootNodes)
232+
}
156233
}
157234

158235
stack.Wait()
@@ -182,13 +259,11 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
182259

183260
boot := func(ctx *node.ServiceContext) (node.Service, error) {
184261
var client *ethclient.Client
185-
if ethapi == "" {
186-
err = fmt.Errorf("use ethapi flag to connect to a an eth client and talk to the blockchain")
187-
} else {
262+
if len(ethapi) > 0 {
188263
client, err = ethclient.Dial(ethapi)
189-
}
190-
if err != nil {
191-
utils.Fatalf("Can't connect: %v", err)
264+
if err != nil {
265+
utils.Fatalf("Can't connect: %v", err)
266+
}
192267
}
193268
return swarm.NewSwarm(ctx, client, bzzconfig, swapEnabled, syncEnabled)
194269
}

0 commit comments

Comments
 (0)