Skip to content

Commit

Permalink
[chshersh#55] fetch exe_name from api if ommited
Browse files Browse the repository at this point in the history
Resolves [chshersh#55]
  • Loading branch information
mmohammadi9812 committed Sep 9, 2022
1 parent ab7cf4c commit 81e6964
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/sync/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::schema::ConfigAsset;
use crate::model::asset_name::AssetName;
use crate::model::tool::{Tool, ToolError, ToolInfo, ToolInfoTag};
use crate::sync::db::lookup_tool;
use serde::Deserialize;

pub fn configure_tool(tool_name: &str, config_asset: &ConfigAsset) -> Tool {
match lookup_tool(tool_name) {
Expand All @@ -27,11 +28,31 @@ pub fn configure_tool(tool_name: &str, config_asset: &ConfigAsset) -> Tool {
}
}

#[derive(Deserialize)]
pub struct RepoInformation {
pub name: String,
// There are other fields too, but we don't need them now
// They may be added later if needed at all
}

fn get_exe_name(owner: String, repo: String) -> Result<String, ureq::Error> {
let url = format!(
"https://api.github.com/repos/{owner}/{repo}",
owner = owner,
repo = repo
);
let repo: RepoInformation = ureq::get(&url).call()?.into_json()?;
Ok(repo.name)
}

/// Configure 'ToolInfo' completely from 'ConfigAsset'
fn full_configure(config_asset: &ConfigAsset) -> Option<ToolInfo> {
let owner = config_asset.owner.clone()?;
let repo = config_asset.repo.clone()?;
let exe_name = config_asset.exe_name.clone()?;
let exe_name = match config_asset.exe_name.clone() {
Some(exe_name) => exe_name,
None => get_exe_name(owner.clone(), repo.clone()).unwrap(),
};
let tag = config_asset
.tag
.clone()
Expand Down Expand Up @@ -219,6 +240,38 @@ mod tests {
);
}

#[test]
fn get_exe_name() {
let tool_name = "abcdef";

let config_asset = ConfigAsset {
owner: Some(String::from("chshersh")),
repo: Some(String::from("tool-sync")),
exe_name: None,
asset_name: AssetName {
linux: Some(String::from("my-linux")),
macos: Some(String::from("my-macos")),
windows: Some(String::from("yours-windows")),
},
tag: Some(String::from("1.0.0")),
};

assert_eq!(
configure_tool(tool_name, &config_asset),
Tool::Known(ToolInfo {
owner: "chshersh".to_string(),
repo: "tool-sync".to_string(),
exe_name: "tool-sync".to_string(),
asset_name: AssetName {
linux: Some("my-linux".to_string()),
macos: Some("my-macos".to_string()),
windows: Some("yours-windows".to_string()),
},
tag: ToolInfoTag::Specific("1.0.0".to_string()),
})
);
}

#[test]
fn partial_override() {
let tool_name = "ripgrep";
Expand Down

0 comments on commit 81e6964

Please sign in to comment.