Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix git pull issue and unwrap error #80

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions fluere-plugin/src/downloader.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
use crate::util::home_cache_path;
use std::process::Command;
use git2::{Cred, FetchOptions, RemoteCallbacks, Repository};

Check failure on line 2 in fluere-plugin/src/downloader.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `git2`

error[E0432]: unresolved import `git2` --> fluere-plugin/src/downloader.rs:2:5 | 2 | use git2::{Cred, FetchOptions, RemoteCallbacks, Repository}; | ^^^^ use of undeclared crate or module `git2`

pub fn download_plugin_from_github(repo_name: &str) -> Result<(), std::io::Error> {
let url = format!("https://github.com/{}.git", repo_name);
let path = home_cache_path();
let cd_cmd = format!("cd {}", path.display());
let path = home_cache_path().unwrap();
if !path.exists() {
std::fs::create_dir_all(path.clone())?;
}
if path.join(repo_name.split('/').last().unwrap()).exists() {
Command::new("bash")
.arg("-c")
.arg(cd_cmd + ";git fetch ;git pull")
.output()?;
let repo_path = path.join(repo_name.split('/').last().unwrap());
if repo_path.exists() {
let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|_url, username_from_url, _allowed_types| {
Cred::ssh_key(
username_from_url.unwrap(),
None,
std::path::Path::new(&format!("{}/.ssh/id_rsa", std::env::var("HOME").unwrap())),
None,
)
});
let mut fetch_options = FetchOptions::new();
fetch_options.remote_callbacks(callbacks);
let repo = Repository::open(&repo_path)?;
repo.find_remote("origin")?
.fetch(&["master"], Some(&mut fetch_options), None)?;
let fetch_head = repo.find_reference("FETCH_HEAD")?;
let annotated = repo.reference_to_annotated_commit(&fetch_head)?;
repo.merge(&[&annotated], None, Some(&mut fetch_options))?;
} else {
Command::new("bash")
.arg("-c")
.arg(cd_cmd + "; git clone " + &url)
.output()?;
Repository::clone(&url, &path)?;
}
Ok(())
}
9 changes: 6 additions & 3 deletions fluere-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl PluginManager {
None => {
match download_plugin_from_github(name) {
Ok(_) => {
let path = home_cache_path().join(name.split('/').last().unwrap());
let path = home_cache_path()?.join(name.split('/').last().unwrap());
match std::fs::read_to_string(path.join("init.lua")) {
Ok(code) => {
let lua_clone = self.lua.clone();
Expand All @@ -124,8 +124,11 @@ impl PluginManager {
let argument_table = lua.create_table()?;

// println!("extra argument details{:?}", plugin_config.extra_arguments);
for (key, value) in
plugin_config.extra_arguments.clone().unwrap().iter()
for (key, value) in plugin_config
.extra_arguments
.clone()
.expect("not enough argument")
.iter()
{
argument_table.set(key.as_str(), value.as_str())?;
}
Expand Down
29 changes: 18 additions & 11 deletions fluere-plugin/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@ use std::env;
use std::fs;
use std::path::{Path, PathBuf};

pub fn home_cache_path() -> PathBuf {
pub fn home_cache_path() -> Result<PathBuf, std::io::Error> {
// Check for the SUDO_USER environment variable
let sudo_user = env::var("SUDO_USER");

let path_base = match sudo_user {
Ok(user) => {
// If SUDO_USER is set, construct the path using the user's home directory
let user_home = format!("/home/{}", user);
Path::new(&user_home).join(".cache")
// on macOS just return the cache_dir()
if env::consts::OS == "macos" {
Ok(cache_dir().expect("Could not determine the home directory"))
} else {
// If SUDO_USER is set, construct the path using the user's home directory
let user_home = format!("/home/{}", user);
Ok(Path::new(&user_home).join(".cache"))
}
}
Err(_) => {
// If not running under sudo, just use the config_dir function as before
cache_dir().unwrap()
// If not running under sudo, just use the cache_dir function as before
cache_dir().ok_or(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Could not determine the cache directory",
))
}
};

let path_config = path_base.join("fluere");
if !path_config.exists() {
fs::create_dir_all(path_config.clone()).unwrap();
let path_cache = path_base?.join("fluere");
if !path_cache.exists() {
fs::create_dir_all(path_cache.clone())?;
}
path_config
Ok(path_cache)
}
2 changes: 1 addition & 1 deletion src/net/live_fluereflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ fn draw_ui(
flow_columns[5],
);
}
async fn listen_for_exit_keys() -> Result<(),std::io::Error> {
async fn listen_for_exit_keys() -> Result<(), std::io::Error> {
loop {
if event::poll(std::time::Duration::from_millis(100))? {
if let event::Event::Key(KeyEvent {
Expand Down
Loading