Skip to content

Commit

Permalink
More env management
Browse files Browse the repository at this point in the history
  • Loading branch information
x0f5c3 committed Oct 21, 2022
1 parent 2886d2b commit f0feae5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) struct Config {
impl Config {
fn from_file(path: PathBuf) -> Result<Self> {
let conf = fs::read_to_string(&path)?;
Ok(serde_json::from_str(&conf)?)
Ok(toml::from_str(&conf)?)
}
pub fn to_app(self) -> Result<App> {
App::new(self)
Expand Down Expand Up @@ -75,12 +75,20 @@ impl Config {
})
}
pub fn save(&self) -> Result<()> {
if let Some(l) = &self.list {
let mut file = fs::OpenOptions::new()
.truncate(true)
.create(true)
.write(true)
.open(&self.list_path)?;
file.write_all(toml::to_string_pretty(l)?.as_bytes())?;
}
let mut file = fs::OpenOptions::new()
.truncate(true)
.create(true)
.write(true)
.open(&self.config_path)?;
let to_write = serde_json::to_string_pretty(&self)?;
let to_write = toml::to_string_pretty(&self)?;
file.write_all(to_write.as_bytes())?;
file.sync_all()?;
Ok(())
Expand Down
8 changes: 7 additions & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ lazy_static! {
pub static ref DEFAULT_INSTALL: PathBuf = PROJECT_DIRS.data_local_dir().join("envs");
pub static ref CURRENT_INSTALL: Option<PathBuf> = which::which("go")
.context("Can't find go")
.and_then(|x| x.parent().context("Can't get parent")?.parent().context("Can't get parent").map(|x| x.to_path_buf())).ok();
.and_then(|x| {
x.parent()
.context("Can't get parent")?
.parent()
.context("Can't get parent")
.map(|x| x.to_path_buf())
}).ok();
pub static ref ENVS_DIR: PathBuf = PROJECT_DIRS.data_local_dir().join("envs");
pub static ref ARCH: String = {
match std::env::consts::ARCH {
Expand Down
41 changes: 21 additions & 20 deletions src/envs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::App;
use crate::consts::{env_setter, ENVS_DIR};
use crate::config::{App, Config};
use crate::consts::{env_setter, CONFIG_PATH, ENVS_DIR};
use anyhow::{Context, Result};
use rayon::prelude::*;
use semver::Version;
Expand All @@ -17,24 +17,21 @@ pub struct InstalledEnv {
impl InstalledEnv {
pub fn new(path: &Path) -> Result<InstalledEnv> {
let go_path = path.join("bin").join("go");
let potential_env_file = path.join(".go.env");
let version = if potential_env_file.exists() {
Version::parse(
&fs::read_to_string(potential_env_file).context("Can't read .go.env file")?,
)
.context("Can't parse version from .go.env file")?
} else {
Version::parse(
&duct::cmd!(&go_path, "version")
.read()
.context("Can't get go version")?
.split(" ")
.nth(2)
.context("Can't get go version")?
.replace("go", ""),
)
.context("Can't parse version from go version")?
};
let potential_env_file = path.join(".go_version.env");
if potential_env_file.exists() && potential_env_file.is_file() {
return toml::from_str(&fs::read_to_string(potential_env_file)?)
.context("Failed to parse env file");
}
let version = Version::parse(
&duct::cmd!(&go_path, "version")
.read()
.context("Can't get go version")?
.split(" ")
.nth(2)
.context("Can't get go version")?
.replace("go", ""),
)
.context("Can't parse version from go version")?;
Ok(InstalledEnv {
version,
path: path.to_path_buf(),
Expand Down Expand Up @@ -69,7 +66,10 @@ impl EnvManager {
}
})
.collect();
let config = Config::new(env_dir.clone(), CONFIG_PATH.clone())?;
let app = config.to_app()?;
let ret = EnvManager {
app,
env_dir,
current,
available,
Expand All @@ -88,6 +88,7 @@ impl EnvManager {
self.env_dir.join(".go.env"),
env_setter(self.current.path.join("bin").display()),
)?;
self.app.config.save()?;
Ok(())
}
}
Expand Down

0 comments on commit f0feae5

Please sign in to comment.