Skip to content

Commit 9b7b9e4

Browse files
committed
massive refactor
1 parent 5228a23 commit 9b7b9e4

File tree

16 files changed

+496
-412
lines changed

16 files changed

+496
-412
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Currently POSIX only to ease development.
1313

1414
## Progress
1515

16-
### Subcommands completed:
16+
### Commands completed:
1717
* basename
1818
* clear
1919
* dirname
@@ -25,7 +25,7 @@ Currently POSIX only to ease development.
2525
* whoami
2626
* yes
2727

28-
### Subcommands todo:
28+
### Commands todo:
2929
* [
3030
* b2sum
3131
* base32

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn build(b: *std.Build) !void {
3434
// exe
3535
{
3636
const coreutils_exe = b.addExecutable(.{
37-
.name = "coreutils",
37+
.name = "zig-coreutils",
3838
.root_module = coreutils_module,
3939
});
4040
b.installArtifact(coreutils_exe);
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn execute(
3636
args: *shared.ArgIterator,
3737
cwd: std.fs.Dir,
3838
exe_path: []const u8,
39-
) subcommands.Error!void {
39+
) shared.CommandError!void {
4040
const z: tracy.Zone = .begin(.{ .src = @src(), .name = name });
4141
defer z.end();
4242

@@ -330,7 +330,7 @@ fn parseArguments(
330330
}
331331

332332
test "basename no args" {
333-
try subcommands.testError(
333+
try shared.testError(
334334
@This(),
335335
&.{},
336336
.{},
@@ -342,7 +342,7 @@ test "basename single" {
342342
var stdout = std.ArrayList(u8).init(std.testing.allocator);
343343
defer stdout.deinit();
344344

345-
try subcommands.testExecute(
345+
try shared.testExecute(
346346
@This(),
347347
&.{"hello/world"},
348348
.{ .stdout = stdout.writer() },
@@ -355,7 +355,7 @@ test "basename multiple" {
355355
var stdout = std.ArrayList(u8).init(std.testing.allocator);
356356
defer stdout.deinit();
357357

358-
try subcommands.testExecute(
358+
try shared.testExecute(
359359
@This(),
360360
&.{
361361
"-a",
@@ -375,17 +375,16 @@ test "basename multiple" {
375375
}
376376

377377
test "basename help" {
378-
try subcommands.testHelp(@This(), true);
378+
try shared.testHelp(@This(), true);
379379
}
380380

381381
test "basename version" {
382-
try subcommands.testVersion(@This());
382+
try shared.testVersion(@This());
383383
}
384384

385385
const log = std.log.scoped(.basename);
386386
const shared = @import("../shared.zig");
387387
const std = @import("std");
388-
const subcommands = @import("../subcommands.zig");
389388
const tracy = @import("tracy");
390389

391390
comptime {
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn execute(
2121
args: *shared.ArgIterator,
2222
cwd: std.fs.Dir,
2323
exe_path: []const u8,
24-
) subcommands.Error!void {
24+
) shared.CommandError!void {
2525
const z: tracy.Zone = .begin(.{ .src = @src(), .name = name });
2626
defer z.end();
2727

@@ -138,7 +138,7 @@ test "clear no args" {
138138
var stdout = std.ArrayList(u8).init(std.testing.allocator);
139139
defer stdout.deinit();
140140

141-
try subcommands.testExecute(
141+
try shared.testExecute(
142142
@This(),
143143
&.{},
144144
.{ .stdout = stdout.writer() },
@@ -151,7 +151,7 @@ test "clear - don't clear scrollback" {
151151
var stdout = std.ArrayList(u8).init(std.testing.allocator);
152152
defer stdout.deinit();
153153

154-
try subcommands.testExecute(
154+
try shared.testExecute(
155155
@This(),
156156
&.{"-x"},
157157
.{ .stdout = stdout.writer() },
@@ -161,17 +161,16 @@ test "clear - don't clear scrollback" {
161161
}
162162

163163
test "clear help" {
164-
try subcommands.testHelp(@This(), true);
164+
try shared.testHelp(@This(), true);
165165
}
166166

167167
test "clear version" {
168-
try subcommands.testVersion(@This());
168+
try shared.testVersion(@This());
169169
}
170170

171171
const log = std.log.scoped(.clear);
172172
const shared = @import("../shared.zig");
173173
const std = @import("std");
174-
const subcommands = @import("../subcommands.zig");
175174
const tracy = @import("tracy");
176175

177176
comptime {
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn execute(
3131
args: *shared.ArgIterator,
3232
cwd: std.fs.Dir,
3333
exe_path: []const u8,
34-
) subcommands.Error!void {
34+
) shared.CommandError!void {
3535
const z: tracy.Zone = .begin(.{ .src = @src(), .name = name });
3636
defer z.end();
3737

@@ -186,14 +186,14 @@ fn parseArguments(
186186
}
187187

188188
test "dirname no args" {
189-
try subcommands.testError(@This(), &.{}, .{}, "missing operand");
189+
try shared.testError(@This(), &.{}, .{}, "missing operand");
190190
}
191191

192192
test "dirname single" {
193193
var stdout = std.ArrayList(u8).init(std.testing.allocator);
194194
defer stdout.deinit();
195195

196-
try subcommands.testExecute(
196+
try shared.testExecute(
197197
@This(),
198198
&.{
199199
"hello/world",
@@ -208,7 +208,7 @@ test "dirname multiple" {
208208
var stdout = std.ArrayList(u8).init(std.testing.allocator);
209209
defer stdout.deinit();
210210

211-
try subcommands.testExecute(
211+
try shared.testExecute(
212212
@This(),
213213
&.{
214214
"hello/world",
@@ -227,17 +227,16 @@ test "dirname multiple" {
227227
}
228228

229229
test "dirname help" {
230-
try subcommands.testHelp(@This(), true);
230+
try shared.testHelp(@This(), true);
231231
}
232232

233233
test "dirname version" {
234-
try subcommands.testVersion(@This());
234+
try shared.testVersion(@This());
235235
}
236236

237237
const log = std.log.scoped(.dirname);
238238
const shared = @import("../shared.zig");
239239
const std = @import("std");
240-
const subcommands = @import("../subcommands.zig");
241240
const tracy = @import("tracy");
242241

243242
comptime {
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn execute(
2121
args: *shared.ArgIterator,
2222
cwd: std.fs.Dir,
2323
exe_path: []const u8,
24-
) subcommands.Error!void {
24+
) shared.CommandError!void {
2525
const z: tracy.Zone = .begin(.{ .src = @src(), .name = name });
2626
defer z.end();
2727

@@ -32,14 +32,14 @@ pub fn execute(
3232

3333
_ = try args.nextWithHelpOrVersion(true);
3434

35-
// FIXME: This is weird, is this acceptable to allow the other subcommands to not have to worry about u8 return value?
35+
// FIXME: This is weird, is this acceptable to allow the other shared to not have to worry about u8 return value?
3636
return error.AlreadyHandled;
3737
}
3838

3939
test "false no args" {
4040
try std.testing.expectError(
4141
error.AlreadyHandled,
42-
subcommands.testExecute(
42+
shared.testExecute(
4343
@This(),
4444
&.{},
4545
.{},
@@ -53,7 +53,7 @@ test "false ignores args" {
5353

5454
try std.testing.expectError(
5555
error.AlreadyHandled,
56-
subcommands.testExecute(
56+
shared.testExecute(
5757
@This(),
5858
&.{ "these", "arguments", "are", "ignored" },
5959
.{},
@@ -64,17 +64,16 @@ test "false ignores args" {
6464
}
6565

6666
test "false help" {
67-
try subcommands.testHelp(@This(), true);
67+
try shared.testHelp(@This(), true);
6868
}
6969

7070
test "false version" {
71-
try subcommands.testVersion(@This());
71+
try shared.testVersion(@This());
7272
}
7373

7474
const log = std.log.scoped(.false);
7575
const shared = @import("../shared.zig");
7676
const std = @import("std");
77-
const subcommands = @import("../subcommands.zig");
7877
const tracy = @import("tracy");
7978

8079
comptime {
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-FileCopyrightText: 2025 Lee Cannon <leecannon@leecannon.xyz>
33

44
// TODO: Extended help examples https://github.com/leecannon/zig-coreutils/issues/3
5+
// TODO: How do we test this without introducing the amount of complexity that https://github.com/leecannon/zsw does?
6+
// https://github.com/leecannon/zig-coreutils/issues/1
57

68
pub const name = "groups";
79

@@ -24,7 +26,7 @@ pub fn execute(
2426
args: *shared.ArgIterator,
2527
cwd: std.fs.Dir,
2628
exe_path: []const u8,
27-
) subcommands.Error!void {
29+
) shared.CommandError!void {
2830
const z: tracy.Zone = .begin(.{ .src = @src(), .name = name });
2931
defer z.end();
3032

@@ -46,7 +48,7 @@ fn currentUser(
4648
io: shared.IO,
4749
passwd_file_contents: []const u8,
4850
cwd: std.fs.Dir,
49-
) subcommands.Error!void {
51+
) shared.CommandError!void {
5052
const z: tracy.Zone = .begin(.{ .src = @src(), .name = "current user" });
5153
defer z.end();
5254

@@ -102,7 +104,7 @@ fn otherUser(
102104
arg: shared.Arg,
103105
passwd_file_contents: []const u8,
104106
cwd: std.fs.Dir,
105-
) subcommands.Error!void {
107+
) shared.CommandError!void {
106108
const z: tracy.Zone = .begin(.{ .src = @src(), .name = "other user" });
107109
defer z.end();
108110
z.text(arg.raw);
@@ -199,21 +201,17 @@ fn printGroups(
199201
return shared.unableToWriteTo("stdout", io, err);
200202
}
201203

202-
// TODO: How do we test this without introducing the amount of complexity that https://github.com/leecannon/zsw does?
203-
// https://github.com/leecannon/zig-coreutils/issues/1
204-
205204
test "groups help" {
206-
try subcommands.testHelp(@This(), true);
205+
try shared.testHelp(@This(), true);
207206
}
208207

209208
test "groups version" {
210-
try subcommands.testVersion(@This());
209+
try shared.testVersion(@This());
211210
}
212211

213212
const log = std.log.scoped(.groups);
214213
const shared = @import("../shared.zig");
215214
const std = @import("std");
216-
const subcommands = @import("../subcommands.zig");
217215
const tracy = @import("tracy");
218216

219217
comptime {
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// SPDX-FileCopyrightText: 2025 Lee Cannon <leecannon@leecannon.xyz>
33
// SPDX-FileCopyrightText: 2024 Leon Henrik Plickat
44

5+
// TODO: How do we test this without introducing the amount of complexity that https://github.com/leecannon/zsw does?
6+
// https://github.com/leecannon/zig-coreutils/issues/1
7+
58
pub const name = "nproc";
69

710
pub const short_help =
@@ -22,7 +25,7 @@ pub fn execute(
2225
args: *shared.ArgIterator,
2326
cwd: std.fs.Dir,
2427
exe_path: []const u8,
25-
) subcommands.Error!void {
28+
) shared.CommandError!void {
2629
const z: tracy.Zone = .begin(.{ .src = @src(), .name = name });
2730
defer z.end();
2831

@@ -81,17 +84,16 @@ test getLastCpuIndex {
8184
}
8285

8386
test "nproc help" {
84-
try subcommands.testHelp(@This(), true);
87+
try shared.testHelp(@This(), true);
8588
}
8689

8790
test "nproc version" {
88-
try subcommands.testVersion(@This());
91+
try shared.testVersion(@This());
8992
}
9093

9194
const log = std.log.scoped(.nproc);
9295
const shared = @import("../shared.zig");
9396
const std = @import("std");
94-
const subcommands = @import("../subcommands.zig");
9597
const tracy = @import("tracy");
9698

9799
comptime {
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub const short_help =
77
\\Usage: {0s} [ignored command line arguments]
88
\\ or: {0s} OPTION
99
\\
10-
\\A template subcommand
10+
\\A template command
1111
\\
1212
\\ -h display the short help and exit
1313
\\ --help display the full help and exit
@@ -25,7 +25,7 @@ pub fn execute(
2525
args: *shared.ArgIterator,
2626
cwd: std.fs.Dir,
2727
exe_path: []const u8,
28-
) subcommands.Error!void {
28+
) shared.Error!void {
2929
const z: tracy.Zone = .begin(.{ .src = @src(), .name = name });
3030
defer z.end();
3131

@@ -123,21 +123,20 @@ fn parseArguments(
123123
}
124124

125125
test "template no args" {
126-
try subcommands.testExecute(@This(), &.{}, .{});
126+
try shared.testExecute(@This(), &.{}, .{});
127127
}
128128

129129
test "template help" {
130-
try subcommands.testHelp(@This(), true);
130+
try shared.testHelp(@This(), true);
131131
}
132132

133133
test "template version" {
134-
try subcommands.testVersion(@This());
134+
try shared.testVersion(@This());
135135
}
136136

137137
const log = std.log.scoped(.template);
138138
const shared = @import("../shared.zig");
139139
const std = @import("std");
140-
const subcommands = @import("../subcommands.zig");
141140
const tracy = @import("tracy");
142141

143142
comptime {

0 commit comments

Comments
 (0)