94 lines
2.2 KiB
Raku
Executable file
94 lines
2.2 KiB
Raku
Executable file
#!/usr/bin/env raku
|
|
use v6.e.PREVIEW;
|
|
|
|
use DB;
|
|
use DB::MarkdownPost;
|
|
|
|
my %*SUB-MAIN-OPTS =
|
|
:named-anywhere,
|
|
:bundling,
|
|
;
|
|
|
|
my IO::Path:D $blog-dir = $*PROGRAM.parent;
|
|
#= 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";
|
|
}
|
|
}
|
|
|
|
#| Write the databse to the provided file
|
|
sub write-db(IO::Path $file, DB::PostDB $db) {
|
|
my $output = $db.to-json;
|
|
$file.spurt: $output;
|
|
}
|
|
|
|
#| Initalize the database
|
|
multi MAIN(
|
|
"db",
|
|
"init",
|
|
#| The path of the database file
|
|
IO::Path(Str) :$db-file = $blog-dir.add("db.json"),
|
|
#| Overwrite an already existing database file
|
|
Bool :$force
|
|
) {
|
|
die "Database file already exists, use --force to overwrite: {$db-file.Str}"
|
|
if $db-file.e && !$force;
|
|
|
|
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;
|
|
|
|
if $force {
|
|
$db-file.spurt: $db.to-json, :create-only;
|
|
} else {
|
|
$db-file.spurt: $db.to-json;
|
|
}
|
|
}
|
|
|
|
#| Ensure that the database loads, erroring otherwise
|
|
multi MAIN(
|
|
"db",
|
|
"verify",
|
|
#| The path of the database file
|
|
IO::Path(Str) :$db = $blog-dir.add("db.json"),
|
|
) {
|
|
load-db $db;
|
|
say "Database OK";
|
|
}
|
|
|
|
#| Create a new markdown post
|
|
|
|
multi MAIN(
|
|
"new",
|
|
"markdown",
|
|
#| The path to the markdown file
|
|
IO::Path(Str) $source,
|
|
#| The path of the database file
|
|
IO::Path(Str) :$db-file = $blog-dir.add("db.json"),
|
|
#| The date/time the post should be recorded as posted at
|
|
DateTime(Str) :$posted-at = DateTime.now,
|
|
#| Should the post be hidden from the archive?
|
|
Bool :$hidden = False,
|
|
) {
|
|
my $db = load-db $db-file;
|
|
my $id =
|
|
$db.insert-post:
|
|
MarkdownPost.new(
|
|
source => $source.absolute.IO,
|
|
posted-at => $posted-at,
|
|
hidden => $hidden,
|
|
);
|
|
write-db $db-file, $db;
|
|
say 'Post inserted with id ', $id;
|
|
}
|