Automatically create all folder if env var provided
If ZIPPONDB_PATH is found, it will try to create and ur the directory. If ZIPPONDB_SCHEMA is found, it will try to init the schema using it.
This commit is contained in:
parent
08869e5e3d
commit
62e4f53477
@ -1082,6 +1082,18 @@ pub const FileEngine = struct {
|
||||
return count - 1;
|
||||
}
|
||||
|
||||
pub fn isSchemaFileInDir(self: *FileEngine) bool {
|
||||
const path = std.fmt.allocPrint(
|
||||
self.allocator,
|
||||
"{s}/schema.zipponschema",
|
||||
.{self.path_to_ZipponDB_dir},
|
||||
) catch return false;
|
||||
defer self.allocator.free(path);
|
||||
|
||||
_ = std.fs.cwd().openDir(path, .{ .iterate = true }) catch return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn writeSchemaFile(self: *FileEngine) FileEngineError!void {
|
||||
var zippon_dir = std.fs.cwd().openDir(self.path_to_ZipponDB_dir, .{}) catch return FileEngineError.MemoryError;
|
||||
defer zippon_dir.close();
|
||||
|
29
src/main.zig
29
src/main.zig
@ -39,11 +39,15 @@ pub fn main() !void {
|
||||
defer file_engine.deinit();
|
||||
|
||||
if (path_env_variable) |path| {
|
||||
std.debug.print("Found envirionment variable ZIPPONDB_PATH: {s}.\n", .{path});
|
||||
var to_init = true;
|
||||
_ = std.fs.cwd().openDir(path, .{}) catch {
|
||||
std.debug.print("Error opening ZipponDB path using environment variable, please select the database using 'db use' or create a new one with 'db new'\n", .{});
|
||||
file_engine = FileEngine.init(allocator, "");
|
||||
to_init = false;
|
||||
std.debug.print("{s} directory not found, creating it\n", .{path});
|
||||
std.fs.cwd().makeDir(path) catch {
|
||||
std.debug.print("{s} couldnt be make. Please use 'db new' or 'db use'.\n", .{path});
|
||||
file_engine = FileEngine.init(allocator, "");
|
||||
to_init = false;
|
||||
};
|
||||
};
|
||||
if (to_init) {
|
||||
file_engine = FileEngine.init(allocator, path);
|
||||
@ -51,11 +55,28 @@ pub fn main() !void {
|
||||
//file_engine.createLog("main");
|
||||
file_engine.log("main", .Info, "Found envirionment variable ZIPPONDB_PATH: {s}", .{path});
|
||||
}
|
||||
|
||||
// Check if the db have a schema
|
||||
if (!file_engine.isSchemaFileInDir()) {
|
||||
std.debug.print("Database don't have any schema. Checking if ZIPPONDB_SCHEMA env variable exist.\n", .{});
|
||||
const schema_env_variable = utils.getEnvVariables(allocator, "ZIPPONDB_SCHEMA");
|
||||
if (schema_env_variable) |schema| {
|
||||
std.debug.print("Found envirionment variable ZIPPONDB_SCHEMA {s}.\n", .{schema});
|
||||
file_engine.initDataFolder(schema) catch {
|
||||
std.debug.print("Couldn't use {s} as schema.\n", .{schema});
|
||||
};
|
||||
} else {
|
||||
std.debug.print("No envirionment variable ZIPPONDB_SCHEMA found.\n", .{});
|
||||
}
|
||||
} else {
|
||||
std.debug.print("Database have a schema.\n", .{});
|
||||
}
|
||||
} else {
|
||||
std.debug.print("No envirionment variable ZIPPONDB_PATH found.\n", .{});
|
||||
file_engine = FileEngine.init(allocator, "");
|
||||
}
|
||||
|
||||
const line_buf = try allocator.alloc(u8, BUFFER_SIZE); // TODO: Remove the size limitation
|
||||
const line_buf = try allocator.alloc(u8, BUFFER_SIZE);
|
||||
defer allocator.free(line_buf);
|
||||
|
||||
while (true) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user