Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): add a docs command to easily access the user, dev and config documentation #1276

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ indicatif = "0.17"
itertools = "0.13"
merge = "0.1"
once_cell = "1.19"
open = "5.1.2"
self_update = { version = "0.41", default-features = false, optional = true, features = ["rustls", "archive-tar", "compression-flate2"] }
toml = "0.8"

Expand Down
8 changes: 8 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ use crate::{commands::EntryPoint, config::RusticConfig};
/// Application state
pub static RUSTIC_APP: AppCell<RusticApp> = AppCell::new();

// Constants
pub mod constants {
pub const RUSTIC_DOCS_URL: &str = "https://rustic.cli.rs/docs";
pub const RUSTIC_DEV_DOCS_URL: &str = "https://rustic.cli.rs/dev-docs";
pub const RUSTIC_CONFIG_DOCS_URL: &str =
"https://github.com/rustic-rs/rustic/blob/main/config/README.md";
}

/// Rustic Application
#[derive(Debug)]
pub struct RusticApp {
Expand Down
13 changes: 9 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) mod completions;
pub(crate) mod config;
pub(crate) mod copy;
pub(crate) mod diff;
pub(crate) mod docs;
pub(crate) mod dump;
pub(crate) mod find;
pub(crate) mod forget;
Expand Down Expand Up @@ -38,10 +39,11 @@ use crate::commands::webdav::WebDavCmd;
use crate::{
commands::{
backup::BackupCmd, cat::CatCmd, check::CheckCmd, completions::CompletionsCmd,
config::ConfigCmd, copy::CopyCmd, diff::DiffCmd, dump::DumpCmd, forget::ForgetCmd,
init::InitCmd, key::KeyCmd, list::ListCmd, ls::LsCmd, merge::MergeCmd, prune::PruneCmd,
repair::RepairCmd, repoinfo::RepoInfoCmd, restore::RestoreCmd, self_update::SelfUpdateCmd,
show_config::ShowConfigCmd, snapshots::SnapshotCmd, tag::TagCmd,
config::ConfigCmd, copy::CopyCmd, diff::DiffCmd, docs::DocsCmd, dump::DumpCmd,
forget::ForgetCmd, init::InitCmd, key::KeyCmd, list::ListCmd, ls::LsCmd, merge::MergeCmd,
prune::PruneCmd, repair::RepairCmd, repoinfo::RepoInfoCmd, restore::RestoreCmd,
self_update::SelfUpdateCmd, show_config::ShowConfigCmd, snapshots::SnapshotCmd,
tag::TagCmd,
},
config::{progress_options::ProgressOptions, AllRepositoryOptions, RusticConfig},
{Application, RUSTIC_APP},
Expand Down Expand Up @@ -95,6 +97,9 @@ enum RusticCmd {
/// Note that the exclude options only apply for comparison with a local path
Diff(DiffCmd),

/// Open the documentation
Docs(DocsCmd),

/// dump the contents of a file in a snapshot to stdout
Dump(DumpCmd),

Expand Down
61 changes: 61 additions & 0 deletions src/commands/docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! `docs` subcommand

use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use anyhow::Result;
use clap::Subcommand;

use crate::{
application::constants::{RUSTIC_CONFIG_DOCS_URL, RUSTIC_DEV_DOCS_URL, RUSTIC_DOCS_URL},
RUSTIC_APP,
};

#[derive(Command, Debug, Clone, Copy, Default, Subcommand, Runnable)]
enum DocsTypeSubcommand {
#[default]
/// Show the user documentation
User,
/// Show the development documentation
Dev,
/// Show the configuration documentation
Config,
}

/// Opens the documentation in the default browser.
#[derive(Clone, Command, Default, Debug, clap::Parser)]
pub struct DocsCmd {
#[clap(subcommand)]
cmd: Option<DocsTypeSubcommand>,
}

impl Runnable for DocsCmd {
fn run(&self) {
if let Err(err) = self.inner_run() {
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}

impl DocsCmd {
fn inner_run(&self) -> Result<()> {
let user_string = match self.cmd {
// Default to user docs if no subcommand is provided
Some(DocsTypeSubcommand::User) | None => {
open::that(RUSTIC_DOCS_URL)?;
format!("Opening the user documentation at {RUSTIC_DOCS_URL}")
}
Some(DocsTypeSubcommand::Dev) => {
open::that(RUSTIC_DEV_DOCS_URL)?;
format!("Opening the development documentation at {RUSTIC_DEV_DOCS_URL}")
}
Some(DocsTypeSubcommand::Config) => {
open::that(RUSTIC_CONFIG_DOCS_URL)?;
format!("Opening the configuration documentation at {RUSTIC_CONFIG_DOCS_URL}")
}
};

println!("{user_string}");

Ok(())
}
}