-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10b2f9e
commit 650610b
Showing
5 changed files
with
97 additions
and
178 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "orion-ssg" | ||
version = "1.1.5" | ||
version = "1.1.6" | ||
edition = "2021" | ||
authors = ["Adrian Tombu <[email protected]>"] | ||
keywords = ["generator", "static", "markdown", "html", "blog"] | ||
|
@@ -13,12 +13,12 @@ include = ["/src", "LICENSE.md", "README.md"] | |
[dependencies] | ||
anyhow = "1" | ||
chrono = { version = "0.4", features = ["serde"] } | ||
clap = { version = "4.0", features = ["derive"] } | ||
console = "0.15" | ||
fs_extra = "1.3" | ||
glob = "0.3" | ||
gray_matter = "0.2" | ||
notify = "6" | ||
pico-args = "0.5" | ||
pulldown-cmark = "0.11" | ||
quick-xml = "0.35" | ||
rayon = "1.7" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,63 +8,85 @@ mod serve; | |
|
||
use crate::config::Config; | ||
use anyhow::Context; | ||
use clap::{Parser, Subcommand}; | ||
use console::style; | ||
|
||
/// A simple static blog generator | ||
#[derive(Debug, Parser)] | ||
#[clap( | ||
author = "Adrian Tombu <[email protected]>", | ||
version, | ||
about = "A simple static blog generator", | ||
long_about = "Write your post in Markdown and build them into a static HTML website" | ||
)] | ||
struct Cli { | ||
#[clap(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum Commands { | ||
/// Create a new empty post | ||
#[clap(arg_required_else_help = true)] | ||
Post { | ||
/// The slug of the post | ||
slug: String, | ||
|
||
/// Set to true to create the new post as a draft (it won't be published) | ||
#[arg(short, long, default_value_t = false)] | ||
draft: bool, | ||
}, | ||
|
||
/// Builds the blog to html | ||
Build, | ||
|
||
/// Initialise a new Orion project | ||
#[clap(arg_required_else_help = true)] | ||
Init { | ||
/// Path of the new project | ||
path: String, | ||
}, | ||
|
||
/// Runs a local server to navigate the blog | ||
Serve, | ||
const HELP: &str = "\ | ||
A simple static blog generator | ||
Usage: orion-ssg <COMMAND> | ||
Commands: | ||
init --path my-blog Initialise a new Orion project | ||
post --slug my-amazing-title --draft Create a new empty post | ||
build Builds the blog to html | ||
serve Runs a local server to navigate the blog | ||
Options: | ||
-h, --help Print help | ||
-v, --version Print version"; | ||
|
||
#[derive(Debug)] | ||
struct AppArgs { | ||
/// The slug of the post | ||
slug: Option<String>, | ||
|
||
/// Set to true to create the new post as a draft (it won't be published) | ||
draft: Option<String>, | ||
|
||
/// Path of the new project | ||
path: Option<String>, | ||
command: String, | ||
} | ||
|
||
fn main() { | ||
let args = Cli::parse(); | ||
match parse_args() { | ||
Ok(args) => { | ||
match args.command.as_str() { | ||
"post" => post::run( | ||
&args.slug.expect("--slug is required"), | ||
args.draft.unwrap_or_default() == "--draft", | ||
) | ||
.context("Failed to create a new post"), | ||
|
||
"build" => build::run().context("Failed to build the blog"), | ||
|
||
"init" => init::run(&args.path.expect("--path is required")) | ||
.context("Failed to initialize a new project"), | ||
|
||
"serve" => serve::run().context("Failed to serve the blog locally"), | ||
|
||
let res = match args.command { | ||
Commands::Post { slug, draft } => { | ||
post::run(&slug, draft).context("Failed to create a new post") | ||
_ => { | ||
print!("{HELP}"); | ||
std::process::exit(0); | ||
} | ||
} | ||
.expect("TODO: panic message"); | ||
} | ||
Commands::Build => build::run().context("Failed to build the blog"), | ||
Commands::Init { path } => init::run(&path).context("Failed to initialize a new project"), | ||
Commands::Serve => serve::run().context("Failed to serve the blog locally"), | ||
}; | ||
Err(e) => { | ||
eprintln!("Error: {e}."); | ||
std::process::exit(1); | ||
} | ||
} | ||
} | ||
|
||
fn parse_args() -> Result<AppArgs, pico_args::Error> { | ||
let mut pargs = pico_args::Arguments::from_env(); | ||
|
||
if let Err(err) = res { | ||
eprintln!("{:?}", style(err).red()); | ||
std::process::exit(1); | ||
if pargs.clone().finish().is_empty() || pargs.contains(["-h", "--help"]) { | ||
print!("{HELP}"); | ||
std::process::exit(0); | ||
} | ||
|
||
if pargs.contains(["-v", "--version"]) { | ||
print!("orion-ssg v{}", env!("CARGO_PKG_VERSION")); | ||
std::process::exit(0); | ||
} | ||
|
||
let args = AppArgs { | ||
slug: pargs.opt_value_from_str("--slug")?, | ||
draft: pargs.opt_value_from_str("--draft")?, | ||
path: pargs.opt_value_from_str("--path")?, | ||
command: pargs.free_from_str()?, | ||
}; | ||
|
||
Ok(args) | ||
} |