-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor commands into separate modules (#94)
Some refactoring to change a few things: * Move all commands into separate modules to make the top-level `match` in `lib.rs` concise * Change the text of the `default-config` * Refactor formatting of tool names into a separate function * Rename integration test files to specify whether they are used for the the `sync` or `install` command
- Loading branch information
Showing
13 changed files
with
172 additions
and
151 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,52 +1,46 @@ | ||
/// This file only holds the template that is used to generate a default .tools.toml. | ||
use std::fmt::Write; | ||
use crate::sync::db; | ||
|
||
use crate::sync::db::build_db; | ||
pub fn generate_default_config() { | ||
println!("{}", config_template()); | ||
} | ||
|
||
pub fn config_template() -> String { | ||
let mut tools: String = String::new(); | ||
for tool in build_db().keys().cloned().collect::<Vec<String>>() { | ||
if let Err(e) = writeln!(tools, "# [{}]", tool) { | ||
crate::err::abort_suggest_issue(&format!("{}", e)); | ||
}; | ||
} | ||
// adding another hash to fil a new line before the next block | ||
tools.push('#'); | ||
fn config_template() -> String { | ||
let tools = db::fmt_tool_names(|name| format!("# [{name}]")); | ||
|
||
format!( | ||
r###"# This config file was generated for version {version} | ||
# | ||
# # tool-sync default configuration file | ||
r###"# This configuration is automatically generated by tool-sync {version} | ||
# /~https://github.com/chshersh/tool-sync | ||
# This file was automatically generated by tool-sync | ||
##################################################### | ||
# | ||
####################################### | ||
# | ||
# Installation directory for all the tools: | ||
# 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 | ||
# tool-sync provides native support for some of the tools without the need to | ||
# configure them. Uncomment all the tools you want to install with a single | ||
# 'tool sync' command: | ||
# | ||
{tools} | ||
# 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" | ||
# You can configure the installation of any tool by specifying corresponding options: | ||
# | ||
# 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""###, | ||
# [ripgrep] # Name of the tool (new or one of the hardcoded to override default settings) | ||
# owner = "BurntSushi" # GitHub repository owner | ||
# repo = "ripgrep" # GitHub repository name | ||
# exe_name = "rg" # Executable name inside the asset | ||
# 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""###, | ||
version = env!("CARGO_PKG_VERSION"), | ||
) | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod client; | ||
pub mod err; |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::collections::BTreeMap; | ||
use std::path::PathBuf; | ||
|
||
use crate::config::schema::{Config, ConfigAsset}; | ||
use crate::config::toml; | ||
use crate::infra::err; | ||
use crate::sync; | ||
use crate::sync::db::{fmt_tool_names, lookup_tool}; | ||
|
||
/// Install a single tool | ||
pub fn install(config_path: PathBuf, name: String) { | ||
toml::with_parsed_file(config_path, |config| install_tool(config, name)) | ||
} | ||
|
||
/// Find if the tool is already mentioned in the config | ||
fn install_tool(mut config: Config, name: String) { | ||
if let Some(tool_info) = lookup_tool(&name) { | ||
let tool_btree: BTreeMap<String, ConfigAsset> = BTreeMap::from([(name, tool_info.into())]); | ||
config.tools = tool_btree; | ||
sync::sync_from_config_no_check(config); | ||
} else { | ||
let tools = fmt_tool_names(|name| format!(" * {name}")); | ||
|
||
let exit_message = format!( | ||
r#"Unknown tool: '{name}' | ||
Supported tools: | ||
{tools}"# | ||
); | ||
|
||
err::abort(&exit_message); | ||
} | ||
} |
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
Oops, something went wrong.