Skip to content

Commit

Permalink
🚧 cli parsing and handling of subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Sep 4, 2024
1 parent 7b5f9f9 commit 649819b
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 15 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,32 @@ graph TD
## Run

```sh
zig build run
Usage: btczee [command] [args]

Commands:
node <subcommand>
wallet <subcommand>
```

### Node

```sh
Usage: btczee node <subcommand>

Subcommands:
run Run the node
help Display help for node
```

### Wallet

```sh
Usage: btczee wallet <subcommand>

Subcommands:
create Create a new wallet
load Load an existing wallet
help Display help for wallet
```

## Test
Expand Down
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const package_path = "src/lib.zig";
// List of external dependencies that this package requires.
const external_dependencies = [_]build_helpers.Dependency{
.{
.name = "zig-cli",
.module_name = "zig-cli",
.name = "clap",
.module_name = "clap",
},
.{
.name = "httpz",
Expand Down
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
.url = "https://github.com/karlseguin/zul/archive/ae0c27350c0db6b460f22cba30b6b0c4a02d1ffd.zip",
.hash = "1220457e2c8867f6734520d9b335f01e1d851d6fe7adaa7f6f0756158acaf6c5e87f",
},
.@"zig-cli" = .{
.url = "https://github.com/sam701/zig-cli/archive/9a94c4803a52e54c26b198096d63fb5bde752da2.zip",
.hash = "1220ab73fb7cc11b2308edc3364988e05efcddbcac31b707f55e6216d1b9c0da13f1",
},
.httpz = .{
.url = "git+https://github.com/karlseguin/http.zig#d3e3fb3cf2f3caa2432338282dbe750f85e24254",
.hash = "12205748a52926d9e6dbb1ac07462d8772c1e2fd6f52e4d822d8bf282f425efd1330",
},
.clap = .{
.url = "git+https://github.com/Hejsil/zig-clap#2d9db156ae928860a9acf2f1260750d3b44a4c98",
.hash = "122005e589ab3b6bff8e589b45f5b12cd27ce79f266bdac17e9f33ebfe2fbaff7fe3",
},
},
.paths = .{
"build.zig",
Expand Down
133 changes: 125 additions & 8 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,147 @@ const Storage = @import("storage/storage.zig").Storage;
const P2P = @import("network/p2p.zig").P2P;
const RPC = @import("network/rpc.zig").RPC;
const node = @import("node/node.zig");
const ArgParser = @import("util/ArgParser.zig");

pub fn main() !void {
// Initialize the allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var gpa_state = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = gpa_state.allocator();
defer _ = gpa_state.deinit();

const args = try std.process.argsAlloc(gpa);
defer std.process.argsFree(gpa, args);

var stdout_buffered = std.io.bufferedWriter(std.io.getStdOut().writer());
const stdout = stdout_buffered.writer();

try mainFull(.{
.allocator = gpa,
.args = args[1..],
.stdout = stdout.any(),
});

return stdout_buffered.flush();
}

pub fn mainFull(options: struct {
allocator: std.mem.Allocator,
args: []const []const u8,
stdout: std.io.AnyWriter,
}) !void {
var program = Program{
.allocator = options.allocator,
.args = .{ .args = options.args },
.stdout = options.stdout,
};

return program.mainCommand();
}

const Program = @This();

allocator: std.mem.Allocator,
args: ArgParser,
stdout: std.io.AnyWriter,

const main_usage =
\\Usage: btczee [command] [args]
\\
\\Commands:
\\ node <subcommand>
\\ wallet <subcommand>
\\ help Display this message
\\
;

pub fn mainCommand(program: *Program) !void {
while (program.args.next()) {
if (program.args.flag(&.{"node"}))
return program.nodeSubCommand();
if (program.args.flag(&.{"wallet"}))
return program.walletSubCommand();
if (program.args.flag(&.{ "-h", "--help", "help" }))
return program.stdout.writeAll(main_usage);
if (program.args.positional()) |_| {
try std.io.getStdErr().writeAll(main_usage);
return error.InvalidArgument;
}
}
try std.io.getStdErr().writeAll(main_usage);
return error.InvalidArgument;
}

const node_sub_usage =
\\Usage:
\\ btczee node [command] [args]
\\ btczee node [options] [ids]...
\\
\\Commands:
\\ help Display this message
\\
;

fn nodeSubCommand(program: *Program) !void {
// Handle potential node subcommands here
if (program.args.next()) {
if (program.args.flag(&.{ "-h", "--help", "help" }))
return program.stdout.writeAll(node_sub_usage);
}

// Otherwise, run the node
return program.runNodeCommand();
}

fn runNodeCommand(program: *Program) !void {
// Load configuration
var config = try Config.load(allocator, "bitcoin.conf.example");
var config = try Config.load(program.allocator, "bitcoin.conf.example");
defer config.deinit();

// Initialize components
var mempool = try Mempool.init(allocator, &config);
var mempool = try Mempool.init(program.allocator, &config);
defer mempool.deinit();

var storage = try Storage.init(allocator, &config);
var storage = try Storage.init(program.allocator, &config);
defer storage.deinit();

var p2p = try P2P.init(allocator, &config);
var p2p = try P2P.init(program.allocator, &config);
defer p2p.deinit();

var rpc = try RPC.init(allocator, &config, &mempool, &storage);
var rpc = try RPC.init(program.allocator, &config, &mempool, &storage);
defer rpc.deinit();

// Start the node
try node.startNode(&mempool, &storage, &p2p, &rpc);
}

const wallet_sub_usage =
\\Usage:
\\ btczee wallet [command] [args]
\\
\\Commands:
\\ create Create a new wallet
\\ load Load an existing wallet
\\ help Display this message
\\
;

fn walletSubCommand(program: *Program) !void {
if (program.args.next()) {
if (program.args.flag(&.{"create"}))
return program.walletCreateCommand();
if (program.args.flag(&.{"load"}))
return program.walletLoadCommand();
if (program.args.flag(&.{ "-h", "--help", "help" }))
return program.stdout.writeAll(wallet_sub_usage);
}
try std.io.getStdErr().writeAll(wallet_sub_usage);
return error.InvalidArgument;
}

fn walletCreateCommand(program: *Program) !void {
return program.stdout.writeAll("Not implemented yet\n");
}

fn walletLoadCommand(program: *Program) !void {
return program.stdout.writeAll("Not implemented yet\n");
}
Loading

0 comments on commit 649819b

Please sign in to comment.