Skip to content

Commit

Permalink
Replace clap with pico-args
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantombu committed Jul 2, 2024
1 parent 10b2f9e commit 650610b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 178 deletions.
123 changes: 8 additions & 115 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
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"]
Expand All @@ -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"
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ Orion
A static site generator written in Rust to create a simple blog from Markdown files.

### CLI Actions
* `orion init [PATH]` : create a directory to start a new Orion blog (with a few fake posts)
* `orion post [SLUG]` : create a new Markdown post

* `orion init --path my-blog` : create a directory to start a new Orion blog (with a few fake posts)
* `orion post --slug my-amazing-title --draft` : create a new Markdown post
* `orion build` : export the Markdown to html
* `orion serve` : build, start a local server to test your build, and watch for changes on `/posts`, `/static` and `/themes`
* `orion serve` : build, start a local server to test your build, and watch for changes on `/posts`, `/static`
and `/themes`
* `orion --version` : display the current version of Orion

You can use the `--help` or `-h` flag for each action to know more
* `orion --help` : print the help

### Directory structure

```
.
├── posts
Expand All @@ -39,9 +41,11 @@ The build directory (the files that will be deployed into production)
All the assets you want to use (images for your posts, for example)

`/themes`
It contains the different themes you can use, each folder representing them with its own assets: html layout, css, images, ...
It contains the different themes you can use, each folder representing them with its own assets: html layout, css,
images, ...

### Front matter

You can use the following values in your posts. They will be located at the top of the file in between `---`.

```
Expand All @@ -55,6 +59,6 @@ image: https://www.publicdomainpictures.net/pictures/220000/velka/orion-nebula.j
The actual markdown content
```

### Credits
### Credits

The favicon was made by Denis Moskowitz from the [Noun Project](https://thenounproject.com/term/orion/868269/).
6 changes: 3 additions & 3 deletions src/build/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ pub struct Posts {
impl Posts {
pub fn new(posts: Vec<Post>) -> Self {
let mut categories = HashSet::new();
posts.iter().for_each(|p| {
for p in &posts {
p.categories.iter().for_each(|c| {
categories.insert(c.to_string());
})
});
});
}

Self { categories, posts }
}
Expand Down
124 changes: 73 additions & 51 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 650610b

Please sign in to comment.