-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
145 additions
and
348 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
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,63 +1,38 @@ | ||
use anyhow::{bail, Result}; | ||
use clap::Parser; | ||
use error_stack::{Report, Result, ResultExt}; | ||
use simplelog::{debug, error}; | ||
use simplelog::debug; | ||
|
||
use crate::{ | ||
errors::{CommandExecutionError, VirtualEnvError}, | ||
virtualenv::{pyenv::Pyenv, rsenv::Rsenv, VirtualEnvironment}, | ||
}; | ||
use crate::virtualenv::{pyenv::Pyenv, rsenv::Rsenv, VirtualEnvironment}; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Command { | ||
#[clap(help = "string matching a Python version known to pyenv-rs")] | ||
virtualenv: String, | ||
} | ||
|
||
fn try_activate(v: VirtualEnvironment, venv: &String) -> Result<(), VirtualEnvError> { | ||
fn try_activate(v: VirtualEnvironment, venv: &String) -> Result<()> { | ||
if v.list().contains(venv) { | ||
if let Err(e) = v.activate(Some(venv)) { | ||
error!("{e}"); | ||
return Err(e); | ||
debug!("Unable to activate venv: {e:?}"); | ||
bail!(e); | ||
}; | ||
return Ok(()); | ||
} | ||
Err( | ||
Report::new(VirtualEnvError::NotVirtualEnv(venv.to_string())) | ||
.attach_printable("{venv} is not a virtual environment"), | ||
) | ||
bail!("{venv} is not a virtual environment") | ||
} | ||
|
||
impl Command { | ||
pub fn execute(&self) -> Result<(), CommandExecutionError> { | ||
let context = CommandExecutionError { | ||
command: "activate".into(), | ||
}; | ||
if let Err(e) = VirtualEnvironment::deactivate(true) { | ||
debug!("{e}"); | ||
}; | ||
if try_activate( | ||
VirtualEnvironment::new(&Rsenv).change_context(context.clone())?, | ||
&self.virtualenv, | ||
) | ||
.is_ok() | ||
{ | ||
pub fn execute(&self) -> Result<()> { | ||
VirtualEnvironment::deactivate(true)?; | ||
if try_activate(VirtualEnvironment::new(&Rsenv)?, &self.virtualenv).is_ok() { | ||
return Ok(()); | ||
} | ||
if try_activate( | ||
VirtualEnvironment::new(&Pyenv).change_context(context)?, | ||
&self.virtualenv, | ||
) | ||
.is_ok() | ||
{ | ||
if try_activate(VirtualEnvironment::new(&Pyenv)?, &self.virtualenv).is_ok() { | ||
return Ok(()); | ||
} | ||
error!( | ||
bail!( | ||
"Failed to find and activate virtual environment {}", | ||
self.virtualenv | ||
); | ||
Err(Report::new(CommandExecutionError { | ||
command: "activate".into(), | ||
}) | ||
.attach_printable(format!("Unable to activate virtualenv {}", self.virtualenv))) | ||
} | ||
} |
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
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
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,28 +1,21 @@ | ||
use crate::errors::CommandExecutionError; | ||
use crate::shell::SupportedShell; | ||
use anyhow::{Context, Result}; | ||
use clap::Parser; | ||
use error_stack::{Result, ResultExt}; | ||
use std::io; | ||
use std::io::Write; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Command {} | ||
|
||
impl Command { | ||
pub fn execute(&self) -> Result<(), CommandExecutionError> { | ||
let error_context = CommandExecutionError { | ||
command: "init".into(), | ||
}; | ||
let shell = SupportedShell::new() | ||
.change_context(error_context.clone()) | ||
.attach("Unable to detect current shell")?; | ||
pub fn execute(&self) -> Result<()> { | ||
let shell = SupportedShell::new().context("Unable to detect current shell")?; | ||
|
||
let hook = shell.get_hook(); | ||
|
||
io::stdout() | ||
.write_all(hook.as_bytes()) | ||
.change_context(error_context) | ||
.attach("Unable to write hook to STDOUT!")?; | ||
.context("Unable to write hook to STDOUT!")?; | ||
Ok(()) | ||
} | ||
} |
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
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,42 +1,33 @@ | ||
use crate::{ | ||
errors::{CommandExecutionError, VirtualEnvError}, | ||
virtualenv::{pyenv::Pyenv, rsenv::Rsenv, traits::VirtualEnvCompatible}, | ||
}; | ||
use crate::virtualenv::{pyenv::Pyenv, rsenv::Rsenv, traits::VirtualEnvCompatible}; | ||
use anyhow::{bail, Result}; | ||
use clap::Parser; | ||
use error_stack::{Report, Result}; | ||
use simplelog::error; | ||
use simplelog::debug; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct UseCommand { | ||
#[clap(help = "Virtual environment name")] | ||
venv: String, | ||
} | ||
|
||
fn try_save(f: &dyn VirtualEnvCompatible, venv: String) -> Result<(), VirtualEnvError> { | ||
fn try_save(f: &dyn VirtualEnvCompatible, venv: String) -> Result<()> { | ||
if f.list().contains(&venv) { | ||
f.save(&venv)?; | ||
return Ok(()); | ||
} | ||
Err( | ||
Report::new(VirtualEnvError::NotVirtualEnv(venv.clone())).attach_printable(format!( | ||
"{} not in the list of existing virtual environments", | ||
venv | ||
)), | ||
) | ||
bail!("{} not in the list of existing virtual environments", venv) | ||
} | ||
|
||
impl UseCommand { | ||
pub fn execute(&self) -> Result<(), CommandExecutionError> { | ||
pub fn execute(&self) -> Result<()> { | ||
match try_save(&Rsenv, self.venv.clone()) { | ||
Ok(()) => return Ok(()), | ||
Err(e) => error!("{e}"), | ||
Err(e) => debug!("Rsenv: {e}"), | ||
} | ||
match try_save(&Pyenv, self.venv.clone()) { | ||
Ok(()) => return Ok(()), | ||
Err(e) => error!("{e}"), | ||
Err(e) => debug!("Pyenv: {e}"), | ||
} | ||
|
||
error!("Virtual environment {} doesn't exist", &self.venv); | ||
Ok(()) | ||
bail!("Virtual environment {} doesn't exist", &self.venv); | ||
} | ||
} |
Oops, something went wrong.