Skip to content

Commit

Permalink
git: refactoring the lookup config file
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Feb 26, 2024
1 parent e14d7d5 commit 90fd9e4
Showing 1 changed file with 58 additions and 38 deletions.
96 changes: 58 additions & 38 deletions coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ fn is_hidden(entry: &DirEntry) -> bool {
.unwrap_or(false)
}

struct IndexingInfo {
config: Conf,
lang: PluginLang,
exec_path: String,
}

impl Github {
/// Create a new instance of the Repository
/// with a name and a url
Expand All @@ -66,6 +72,48 @@ impl Github {
}
}

// check if the plugin has the custom configuration to read.
async fn lookup_config_file(
&self,
root_path: &str,
) -> Result<Option<IndexingInfo>, CoffeeError> {
for file in ["coffee.yaml", "coffee.yml"] {
#[allow(unused_assignments)]
let mut plugin_lang = PluginLang::Unknown;
let conf_path = format!("{}/{}", root_path, file);
if let Ok(mut conf_file) = File::open(conf_path).await {
let mut conf_str = String::new();
conf_file.read_to_string(&mut conf_str).await?;
log::debug!("found plugin configuration: {}", conf_str);

let conf_file = serde_yaml::from_str::<Conf>(&conf_str)
.map_err(|err| error!("Coffee manifest malformed: {err}"))?;
let conf_lang = conf_file.plugin.lang.to_owned();
match conf_lang.as_str() {
"pypip" => plugin_lang = PluginLang::PyPip,
"pypoetry" => plugin_lang = PluginLang::PyPoetry,
"go" => plugin_lang = PluginLang::Go,
"rs" | "rust" => plugin_lang = PluginLang::Rust,
"dart" => plugin_lang = PluginLang::Dart,
"js" => plugin_lang = PluginLang::JavaScript,
"ts" => plugin_lang = PluginLang::TypeScript,
"java" | "kotlin" | "scala" => plugin_lang = PluginLang::JVM,
_ => {
return Err(error!("language {conf_lang} not supported"));
}
};

let exec_path = format!("{root_path}/{}", conf_file.plugin.main);
return Ok(Some(IndexingInfo {
config: conf_file,
lang: plugin_lang,
exec_path,
}));
}
}
Ok(None)
}

/// Index the repository to store information
/// related to the plugins
pub async fn index_repository(&mut self) -> Result<(), CoffeeError> {
Expand All @@ -85,46 +133,18 @@ impl Github {
.to_os_string()
.to_string_lossy()
.to_string();
let mut conf = None;
let mut exec_path = None;
let mut plugin_name = None;
let mut plugin_lang = PluginLang::Unknown;

// check if the plugin has the custom configuration to read.
let mut conf = None;
for file in ["coffee.yaml", "coffee.yml"] {
let conf_path = format!("{}/{}", root_path, file);
if let Ok(mut conf_file) = File::open(conf_path).await {
let mut conf_str = String::new();
conf_file.read_to_string(&mut conf_str).await?;
debug!("found plugin configuration: {}", conf_str);

let conf_file = serde_yaml::from_str::<Conf>(&conf_str)
.map_err(|err| error!("Coffee manifest malformed: {err}"))?;
plugin_name = Some(conf_file.plugin.name.to_string());
let conf_lang = conf_file.plugin.lang.to_owned();
match conf_lang.as_str() {
"pypip" => plugin_lang = PluginLang::PyPip,
"pypoetry" => plugin_lang = PluginLang::PyPoetry,
"go" => plugin_lang = PluginLang::Go,
"rs" | "rust" => plugin_lang = PluginLang::Rust,
"dart" => plugin_lang = PluginLang::Dart,
"js" => plugin_lang = PluginLang::JavaScript,
"ts" => plugin_lang = PluginLang::TypeScript,
"java" | "kotlin" | "scala" => plugin_lang = PluginLang::JVM,
_ => {
return Err(error!("language {conf_lang} not supported"));
}
};

exec_path = Some(format!("{root_path}/{}", conf_file.plugin.main));
conf = Some(conf_file);
break;
}
}

// check if there was a coffee configuration file
if conf.is_none() {
debug!("conf file not found, so we try to guess the language");
let mut plugin_name = None;
if let Some(index_info) = self.lookup_config_file(&root_path).await? {
exec_path = Some(index_info.exec_path);
plugin_lang = index_info.lang;
plugin_name = Some(index_info.config.plugin.name.to_owned());
conf = Some(index_info.config);
} else {
// check if there was a coffee configuration file
log::debug!("conf file not found, so we try to guess the language");
// try to understand the language from the file
let files = WalkDir::new(plugin_path.path()).max_depth(1);
for file in files {
Expand Down

0 comments on commit 90fd9e4

Please sign in to comment.