From ab7cf4c21642f84f515d376eb7d19ed55e5db85c Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen <45570310+MitchellBerend@users.noreply.github.com> Date: Thu, 8 Sep 2022 13:26:22 +0200 Subject: [PATCH] Adds a generate command for a default .tools.toml file (#62) Resolves #52. The generate command also uses the --config flag to set the location of the generated file. Would it be a good idea to have some output when actually generating the config? Right now there is no actual response unless there is some file system error. ```bash $ cargo run -- --config test-tools.toml generate Compiling tool-sync v0.1.0 (/home/mitchell/rust/tool-sync) Finished dev [unoptimized + debuginfo] target(s) in 2.14s Running `target/debug/tool --config test-tools.toml generate`` $ cat test-tools.toml # This file was automatically generated by tool-sync # #store_directory = "$HOME/.tools.toml" # # [bat] # owner = "sharkdp" # repo = "bat" # exe_name = "bat" # tag = "latest" # [exa] # owner = "ogham" # repo = "exa" # exe_name = "exa" # tag = "latest" # [fd] # owner = "sharkdp" # repo = "fd" # exe_name = "fd" # tag = "latest" # [ripgrep] # owner = "BurntSushi" # repo = "ripgrep" # exe_name = "rg" # tag = "latest" ``` --- README.md | 9 +++++++-- src/config.rs | 1 + src/config/cli.rs | 3 +++ src/config/template.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 30 +++++++++++++++++++----------- 5 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 src/config/template.rs diff --git a/README.md b/README.md index 1690131..6d8c849 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,10 @@ store_directory = "~/.local/bin" [ripgrep] ``` -By default `tool-sync` reads configuration from `~/.tool.toml` but you can put -the content in any place and specify the path via the `--config` flag. +By default `tool-sync` reads configuration from `$HOME/.tool.toml` you can run `tool +default-config` to print a default configuration example to std out. You can +redirect this out put to a file like so `tool default-config > +$HOME/.tools.toml`. You can also quickly copy the above configuration to the default path by running the following command (Unix-only): @@ -109,6 +111,8 @@ the following command (Unix-only): ```shell curl https://raw.githubusercontent.com/chshersh/tool-sync/main/example-tool-sync-config.toml > ~/.tool.toml ``` +A default config can be also be generated by running `tool --config=path/to/config generate`. +This will generate an example file at `path/to/config`. The above example config lists some tools natively supported by `tool-sync` and therefore they don't require extra configuration. @@ -116,6 +120,7 @@ therefore they don't require extra configuration. To specify a tool not supported by `tool-sync`, add a TOML table entry and list all the required fields like in the example below: + ```toml [tokei] owner = "XAMPPRocky" # GitHub username diff --git a/src/config.rs b/src/config.rs index a2a7aa0..880c95b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,4 @@ pub mod cli; pub mod schema; +pub mod template; pub mod toml; diff --git a/src/config/cli.rs b/src/config/cli.rs index 069d49d..85333e2 100644 --- a/src/config/cli.rs +++ b/src/config/cli.rs @@ -17,4 +17,7 @@ pub struct Cli { pub enum Command { /// Sync all tools specified in configuration file Sync, + + /// Generate a default .tools.toml file and prints it to std out + DefaultConfig, } diff --git a/src/config/template.rs b/src/config/template.rs new file mode 100644 index 0000000..c966172 --- /dev/null +++ b/src/config/template.rs @@ -0,0 +1,38 @@ +/// This file only holds the template that is used to generate a default .tools.toml. + +pub const CONFIG_TEMPLATE: &str = r##"# # tool-sync default configuration file +# https://github.com/chshersh/tool-sync +# This file was automatically generated by tool-sync +##################################################### +# +# +# store_directory = "$HOME/.local/bin" +# +# tool-sync provides native support for some of the tools without the need to configure them +# Uncomment the tools you want to have them +# +# [bat] +# [difftastic] +# [fd] +# [ripgrep] +# +# To add configuration for other tools these are the config options: +# [ripgrep] +# owner = "BurntSushi" +# repo = "ripgrep" +# exe_name = "rg" +# +# # Uncomment to download a specific version or tag. +# # Without this tag latest will be used +# # tag = "13.0.0" +# +# +# Asset name to download on linux OSes +# asset_name.linux = "x86_64-unknown-linux-musl" +# +# uncomment if you want to install on macOS as well +# asset_name.macos = "apple-darwin" +# +# uncomment if you want to install on Windows as well +# asset_name.windows = "x86_64-pc-windows-msvc" +"##; diff --git a/src/lib.rs b/src/lib.rs index a628346..88d0544 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ use clap::Parser; use std::path::PathBuf; use crate::config::cli::{Cli, Command}; +use crate::config::template; use crate::config::toml; use crate::sync::sync; @@ -14,19 +15,22 @@ const DEFAULT_CONFIG_PATH: &str = ".tool.toml"; pub fn run() { let cli = Cli::parse(); - let config_path = resolve_config_path(cli.config); + let config_path = resolve_config_path(cli.config.clone()); - match toml::parse_file(&config_path) { - Err(e) => { - err::abort_with(&format!( - "Error parsing configuration at path {}: {}", - config_path.display(), - e.display() - )); - } - Ok(tool) => match cli.command { - Command::Sync => sync(tool), + match cli.command { + Command::Sync => match toml::parse_file(&config_path) { + Err(e) => { + err::abort_with(&format!( + "Error parsing configuration at path {}: {}", + config_path.display(), + e.display() + )); + } + Ok(tool) => { + sync(tool); + } }, + Command::DefaultConfig => generate_config(), } } @@ -46,3 +50,7 @@ fn resolve_config_path(config_path: Option) -> PathBuf { }, } } + +fn generate_config() { + println!("{}", template::CONFIG_TEMPLATE); +}