Skip to content

Commit 5a69da9

Browse files
committed
util: drop does_folder_exist
1 parent 0df96b6 commit 5a69da9

File tree

4 files changed

+10
-23
lines changed

4 files changed

+10
-23
lines changed

src/cmd/fetch.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const std = @import("std");
22
const string = []const u8;
33
const ansi = @import("ansi");
4+
const extras = @import("extras");
45

56
const zigmod = @import("../lib.zig");
67
const u = @import("./../util/index.zig");
@@ -201,7 +202,7 @@ const DiffChange = struct {
201202
fn diff_lockfile(alloc: std.mem.Allocator) !void {
202203
const max = std.math.maxInt(usize);
203204

204-
if (try u.does_folder_exist(".git")) {
205+
if (try extras.doesFolderExist(null, ".git")) {
205206
const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" });
206207
var stdout = std.io.fixedBufferStream(result.stdout);
207208
const r = stdout.reader();

src/cmd/init.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
135135
}
136136

137137
// ask about .gitignore
138-
if (try u.does_folder_exist(".git")) {
138+
if (try extras.doesFolderExist(null, ".git")) {
139139
const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitignore?", gpa);
140140
if (do) {
141141
const exists = try extras.doesFileExist(null, ".gitignore");
@@ -155,7 +155,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
155155
}
156156

157157
// ask about .gitattributes
158-
if (try u.does_folder_exist(".git")) {
158+
if (try extras.doesFolderExist(null, ".git")) {
159159
const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitattributes?", gpa);
160160
if (do) {
161161
const exists = try extras.doesFileExist(null, ".gitattributes");

src/common.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
136136
u.fail("fetch: git: version type '{s}' is invalid.", .{vtype});
137137
},
138138
};
139-
if (try u.does_folder_exist(pv)) {
139+
if (try extras.doesFolderExist(null, pv)) {
140140
if (vers.id == .branch) {
141141
if (options.update) {
142142
try d.type.update(options.alloc, pv, d.path);
@@ -158,7 +158,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
158158
try setTreeReadOnly(pvd, options.alloc);
159159
return pv;
160160
}
161-
if (!try u.does_folder_exist(p)) {
161+
if (!try extras.doesFolderExist(null, p)) {
162162
try d.type.pull(options.alloc, d.path, p);
163163
} else {
164164
if (options.update) {
@@ -168,7 +168,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
168168
return p;
169169
},
170170
.hg => {
171-
if (!try u.does_folder_exist(p)) {
171+
if (!try extras.doesFolderExist(null, p)) {
172172
try d.type.pull(options.alloc, d.path, p);
173173
} else {
174174
if (options.update) {
@@ -178,12 +178,12 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
178178
return p;
179179
},
180180
.http => {
181-
if (try u.does_folder_exist(pv)) {
181+
if (try extras.doesFolderExist(null, pv)) {
182182
return pv;
183183
}
184184
const file_name = try u.last(try u.split(options.alloc, d.path, "/"));
185185
if (d.version.len > 0) {
186-
if (try u.does_folder_exist(pv)) {
186+
if (try extras.doesFolderExist(null, pv)) {
187187
return pv;
188188
}
189189
const file_path = try std.fs.path.join(options.alloc, &.{ pv, file_name });
@@ -199,7 +199,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
199199
u.fail("{s} does not match hash {s}", .{ d.path, d.version });
200200
return p;
201201
}
202-
if (try u.does_folder_exist(p)) {
202+
if (try extras.doesFolderExist(null, p)) {
203203
try std.fs.cwd().deleteTree(p);
204204
}
205205
const file_path = try std.fs.path.resolve(options.alloc, &.{ p, file_name });

src/util/funcs.zig

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ pub fn split(alloc: std.mem.Allocator, in: string, delim: string) ![]string {
4646
return list.toOwnedSlice();
4747
}
4848

49-
pub fn does_folder_exist(fpath: string) !bool {
50-
const file = std.fs.cwd().openFile(fpath, .{}) catch |e| switch (e) {
51-
error.FileNotFound => return false,
52-
error.IsDir => return true,
53-
else => |ee| return ee,
54-
};
55-
defer file.close();
56-
const s = try file.stat();
57-
if (s.kind != .directory) {
58-
return false;
59-
}
60-
return true;
61-
}
62-
6349
pub fn trim_suffix(in: string, suffix: string) string {
6450
if (std.mem.endsWith(u8, in, suffix)) {
6551
return in[0 .. in.len - suffix.len];

0 commit comments

Comments
 (0)