summaryrefslogtreecommitdiff
path: root/src/cmd/mkdir.zig
blob: 59ed431d6c1638fca4dc1112b3c57b7f7457a4b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const std = @import("std");
const Allocator = std.mem.Allocator;
const print = std.debug.print;

const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
const CommandContext = types.CommandContext;

pub const Mkdir = struct {
    path: []const u8,

    pub fn eval(mkdir: Mkdir, ctx: CommandContext) !CommandStatus {
        _ = ctx.allocator;
        _ = ctx.input_source;

        const dir_path = mkdir.path;

        std.fs.cwd().makeDir(dir_path) catch |err| {
            const error_msg = switch (err) {
                error.PathAlreadyExists => "A subdirectory or file already exists\n",
                error.AccessDenied => "Access denied\n",
                error.FileNotFound => "The system cannot find the path specified\n",
                error.NotDir => "The system cannot find the path specified\n",
                else => "Unable to create directory\n",
            };
            if (ctx.output_capture) |capture| {
                try capture.write(error_msg);
            } else {
                print("{s}", .{error_msg});
            }
            return CommandStatus{ .Code = 1 };
        };

        // No output for successful creation (DOS style)
        return CommandStatus{ .Code = 0 };
    }
};