Skip to content

Commit

Permalink
Adds a generate command for a default .tools.toml file (chshersh#62)
Browse files Browse the repository at this point in the history
Resolves chshersh#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"
```
  • Loading branch information
MitchellBerend authored Sep 8, 2022
1 parent eeed4bb commit ab7cf4c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,27 @@ 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):

```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.

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
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cli;
pub mod schema;
pub mod template;
pub mod toml;
3 changes: 3 additions & 0 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
38 changes: 38 additions & 0 deletions src/config/template.rs
Original file line number Diff line number Diff line change
@@ -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"
"##;
30 changes: 19 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@ 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;

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(),
}
}

Expand All @@ -46,3 +50,7 @@ fn resolve_config_path(config_path: Option<PathBuf>) -> PathBuf {
},
}
}

fn generate_config() {
println!("{}", template::CONFIG_TEMPLATE);
}

0 comments on commit ab7cf4c

Please sign in to comment.