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: Add diag command to print storage location and other diagnostics #6

Merged
merged 2 commits into from
Jul 6, 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
11 changes: 9 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use clap::{crate_version, Parser};
pub struct Opts {
#[clap(subcommand)]
pub command: Option<Command>,
#[clap(short = 'o', long = "out", possible_values = OUTPUT_TYPES_STR, default_value = OutputType::Plain.to_str())]
/// Output result as plain text or as evalable command for one of the shells
#[clap(
short = 'o', long = "out", possible_values = OUTPUT_TYPES_STR, default_value = OutputType::Plain.to_str()
)]
/// Output result as plain text or as eval-able command for one of the shells
pub out_type: OutputType,
}

Expand All @@ -20,6 +22,8 @@ pub enum Command {
Browse(BrowseCmd),
/// Output a command string to integrate shellmark into the shell
Plug(PlugCmd),
/// Print storage location and other diagnostics
Diag(DiagCmd),
}

#[derive(Parser)]
Expand All @@ -45,3 +49,6 @@ pub struct PlugCmd {
/// Name of the shell alias
pub name: String,
}

#[derive(Parser)]
pub struct DiagCmd {}
34 changes: 34 additions & 0 deletions src/diag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::bookmarks;
use crate::shell::{Output, OutputType};
use crate::storage;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::path::PathBuf;

pub struct Diag {
pub data_dir: PathBuf,
pub bookmark_count: usize,
}

impl Display for Diag {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Data directory: {}", self.data_dir.display())?;
writeln!(f, "Bookmark count: {}", self.bookmark_count)
}
}

impl Output for Diag {
fn to_output(&self, _out_type: OutputType) -> Option<String> {
Some(format!("{self}"))
}
}

pub async fn diag_cmd() -> Result<Diag, Box<dyn Error>> {
let data_dir = storage::get_or_create_data_dir().await?;
let bookmarks = bookmarks::read_bookmarks().await?;
let bookmark_count = bookmarks.len();
Ok(Diag {
data_dir,
bookmark_count,
})
}
4 changes: 2 additions & 2 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<C: Clone + 'static> ModeMap<C> {
let binding = Binding::new(combo, act, desc);
self.map
.entry(mode.into())
.or_insert_with(Vec::new)
.or_default()
.push(Box::new(binding));
}

Expand All @@ -106,7 +106,7 @@ impl<C: Clone + 'static> ModeMap<C> {

self.map
.entry(mode.into())
.or_insert_with(Vec::new)
.or_default()
.push(Box::new(binding));
}

Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod add;
mod bookmarks;
mod browse;
mod cli;
mod diag;
mod keys;
mod plug;
mod search;
Expand All @@ -21,6 +22,8 @@ use tracing_subscriber::EnvFilter;

use crate::add::add_cmd;
use crate::browse::browse_cmd;
use crate::cli::Command;
use crate::diag::diag_cmd;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -38,6 +41,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Some(cli::Command::Browse(_)) => browse_cmd().await?.to_output(opts.out_type),
Some(cli::Command::Plug(plug_cmd_opts)) => plug_cmd(plug_cmd_opts).to_output(opts.out_type),
None => browse_cmd().await?.to_output(opts.out_type),
Some(Command::Diag(_)) => diag_cmd().await?.to_output(opts.out_type),
};

if let Some(output) = output {
Expand Down
8 changes: 3 additions & 5 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ impl<A: Output> Output for Option<A> {
pub const OUTPUT_TYPES_STR: &[&str] = &["plain", "posix", "fish", "powershell"];

#[derive(Parser)]
#[derive(Default)]
pub enum OutputType {
#[default]
Plain,
Posix,
Fish,
PowerShell,
}

impl Default for OutputType {
fn default() -> Self {
OutputType::Plain
}
}


impl OutputType {
pub const fn to_str(&self) -> &'static str {
Expand Down
17 changes: 11 additions & 6 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,24 @@ pub fn simplify_path(path: &Path) -> &Path {
dunce::simplified(path)
}

fn data_dir() -> PathBuf {
PROJECT_DIRS.data_local_dir().to_path_buf()
}
pub async fn get_or_create_data_dir() -> Result<PathBuf> {
let proj_dirs = &PROJECT_DIRS;
let data_local_dir = proj_dirs.data_local_dir();
let data_local_dir = data_dir();

if let Err(code) = fs::metadata(data_local_dir).await.map_err(|err| err.kind()) {
if let Err(code) = fs::metadata(&data_local_dir)
.await
.map_err(|err| err.kind())
{
match code {
std::io::ErrorKind::NotFound => {
info!(
"Creating a data folder for shellmark at: {}",
friendly_path(data_local_dir)
friendly_path(&data_local_dir)
);

fs::create_dir_all(data_local_dir).await.context(
fs::create_dir_all(&data_local_dir).await.context(
"Couldn't create a data folder for shellmark. Please, check the access rights.",
)?;

Expand All @@ -67,5 +72,5 @@ pub async fn get_or_create_data_dir() -> Result<PathBuf> {
}
}

Ok(data_local_dir.to_path_buf())
Ok(data_local_dir)
}
Loading