website/blog

62 lines
1.3 KiB
Text
Raw Normal View History

2025-01-20 17:59:22 -05:00
#!/usr/bin/env raku
2025-01-21 03:24:01 -05:00
use v6.e.PREVIEW;
2025-01-21 01:31:33 -05:00
use DB;
2025-01-21 03:24:01 -05:00
my %*SUB-MAIN-OPTS =
:named-anywhere,
:bundling,
;
2025-01-21 01:31:33 -05:00
2025-01-21 21:41:28 -05:00
my IO::Path:D $blog-dir = $*PROGRAM.parent;
2025-01-21 03:24:01 -05:00
#= The directory this script is located in
#| Load the database from the provided file
sub load-db(IO::Path $file --> DB::PostDB:D) {
my $result = DB::PostDB.from-json: $file.slurp;
if $result ~~ DB::PostDB:D {
return $result;
} else {
die "Error parsing $file as databse: $result";
}
}
#| Initalize the database
multi MAIN(
"db",
"init",
#| The path of the database file
IO::Path :$file = $blog-dir.add("db.json"),
#| Overwrite an already existing database file
Bool :$force
) {
die "Database file already exists, use --force to overwrite: {$file.Str}"
if $file.e && !$force;
2025-01-21 22:08:10 -05:00
print "Blog Title: ";
my $title = get;
print "Tagline: ";
my $tagline = get;
my $meta = DB::BlogMeta.new: title => $title, tagline => $tagline;
my $db = DB::PostDB.new: meta => $meta;
2025-01-21 03:24:01 -05:00
if $force {
$file.spurt: $db.to-json, :create-only;
} else {
$file.spurt: $db.to-json;
}
}
#| Ensure that the database loads, erroring otherwise
multi MAIN(
"db",
"verify",
#| The path of the database file
IO::Path :$file = $blog-dir.add("db.json"),
) {
load-db $file;
say "Database OK";
}