From 4f95246130a310d9b30f470226bae0bf44b3f361 Mon Sep 17 00:00:00 2001 From: Mike Goelzer Date: Sun, 9 Sep 2018 07:31:10 -0700 Subject: [PATCH] Added CLI args parsing --- README.md | 7 +++++-- content-dht-provide-find/dht-interop.go | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1457ae6..91b7de6 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,13 @@ ``` cd content-dht-provide-find make -./dht-interop +./dht-interop -b ../util/private_key.bin ``` +`-b` means bootstrap mode. In this example, the go program is always the bootstrap node, so `-b` is always required. + +Note that the node ID of `dht-interop` is always `Qm...6aJ9oRuEzWa` because it is being read in from `../util/private_key.bin` (a private key marshalled to X.509 generated by `util/private-key-gen`). This is to keep the peer id of the bootstrap server stable across invocations. + **Second terminal:** run the command printed out by dht-interop, replacing 127.0.0.1 with the IP of the server where dht-interop is listening. Example: First time only: @@ -26,7 +30,6 @@ Running the Node.js program: node js-dht-test/index.js /ip4/127.0.0.1/tcp/9876/ipfs/QmehVYruznbyDZuHBV4vEHESpDevMoAovET6aJ9oRuEzWa ``` -Note that the node ID of `dht-interop` is always `Qm...6aJ9oRuEzWa` because it is being read in from `util/private_key.bin` (a private key marshalled to X.509 generated by `util/private-key-gen`). This is to keep the peer id of the bootstrap server stable across invocations. ## Demo 2: PubSub diff --git a/content-dht-provide-find/dht-interop.go b/content-dht-provide-find/dht-interop.go index c1f635a..a64708d 100644 --- a/content-dht-provide-find/dht-interop.go +++ b/content-dht-provide-find/dht-interop.go @@ -63,14 +63,37 @@ func handleConn(conn net.Conn) { } } +func parseArgs() (bool, string) { + usage := fmt.Sprintf("Usage: %s [-b] [PRIVATE_KEY]\n\n-b is bootstrap mode (creates DHT)\nPRIVATE_KEY is the path to a private key like '../util/private_key.bin'\n", os.Args[0]) + var bBootstrap bool = false + var privKeyFilePath string + var args []string = os.Args[1:] + if (len(args) == 0) || (len(args) > 2) { + fmt.Printf("Error: wrong number of arguments\n\n%s", usage) + os.Exit(1) + } + if args[0] == "-b" { + bBootstrap = true + args = args[1:] + } + privKeyFilePath = args[0] + return bBootstrap, privKeyFilePath +} + func main() { ctx := context.Background() + bBootstrap, privKeyFilePath := parseArgs() + if !bBootstrap { + fmt.Printf("Error: bootstrap mode required in this example") + os.Exit(1) + } + // // Read the private key // var privBytes []byte - privBytes, err := ioutil.ReadFile("../util/private_key.bin") + privBytes, err := ioutil.ReadFile(privKeyFilePath) if err != nil { fmt.Println("ioutil.ReadFile: failed: %v", err) panic(err)