Skip to content

Commit

Permalink
Remove some dependencies in favor of rust std
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 21, 2024
1 parent 10d4a8d commit 8b080d9
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 66 deletions.
34 changes: 1 addition & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ authors = ["Lucien Greathouse <me@lpghatguy.com>"]

[dependencies]
anyhow = "1.0"
atty = "0.2"
clap = { version = "4.5", features = ["derive"] }
dialoguer = "0.11"
dirs = "5.0"
env_logger = "0.11"
fs-err = "2.8"
itertools = "0.12"
log = "0.4"
once_cell = "1.9"
reqwest = { version = "0.12", default-features = false, features = [
"blocking",
"rustls-tls",
Expand Down
9 changes: 5 additions & 4 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs;
use std::io;

use anyhow::{bail, format_err, Context};
Expand Down Expand Up @@ -25,7 +26,7 @@ impl AuthManifest {
/// Create an empty global auth manifest if there isn't one already.
pub fn init(home: &Home) -> anyhow::Result<()> {
let base_dir = home.path();
fs_err::create_dir_all(base_dir)?;
fs::create_dir_all(base_dir)?;

let manifest_path = base_dir.join(MANIFEST_FILE_NAME);
write_if_not_exists(&manifest_path, DEFAULT_MANIFEST.trim())?;
Expand All @@ -37,7 +38,7 @@ impl AuthManifest {
pub fn load(home: &Home) -> anyhow::Result<Option<AuthManifest>> {
let file_path = home.path().join(MANIFEST_FILE_NAME);

let contents = match fs_err::read_to_string(&file_path) {
let contents = match fs::read_to_string(&file_path) {
Ok(contents) => contents,
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Expand All @@ -56,11 +57,11 @@ impl AuthManifest {

fn _add_token(home: &Home, token_type: &str, token: &str) -> anyhow::Result<()> {
let manifest_path = home.path().join(MANIFEST_FILE_NAME);
let content = fs_err::read_to_string(&manifest_path)?;
let content = fs::read_to_string(&manifest_path)?;
let mut document: DocumentMut = content.parse()?;
document[token_type] = toml_edit::value(token.to_string());

fs_err::write(&manifest_path, document.to_string())?;
fs::write(&manifest_path, document.to_string())?;

log::info!("A {token_type} token has been added globally.");

Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![allow(unused)]

use std::fs::OpenOptions;
use std::io::{self, BufWriter, Write};
use std::path::Path;

use anyhow::bail;
use fs_err::OpenOptions;

pub fn write_only_new(path: &Path, contents: &str) -> anyhow::Result<()> {
let mut file = match OpenOptions::new().create_new(true).write(true).open(path) {
Expand Down
9 changes: 5 additions & 4 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -35,7 +36,7 @@ impl Manifest {
/// Create an empty global Aftman manifest if there isn't one already.
pub fn init_global(home: &Home) -> anyhow::Result<()> {
let base_dir = home.path();
fs_err::create_dir_all(base_dir)?;
fs::create_dir_all(base_dir)?;

let manifest_path = base_dir.join(MANIFEST_FILE_NAME);
write_if_not_exists(&manifest_path, DEFAULT_MANIFEST.trim())?;
Expand Down Expand Up @@ -82,7 +83,7 @@ impl Manifest {
pub fn load_from_dir(path: &Path) -> anyhow::Result<Option<Manifest>> {
let file_path = path.join(MANIFEST_FILE_NAME);

let contents = match fs_err::read_to_string(&file_path) {
let contents = match fs::read_to_string(&file_path) {
Ok(contents) => contents,
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Expand Down Expand Up @@ -142,11 +143,11 @@ impl Manifest {
}

fn add_tool(manifest_path: &Path, alias: &ToolAlias, id: &ToolId) -> anyhow::Result<()> {
let content = fs_err::read_to_string(manifest_path)?;
let content = fs::read_to_string(manifest_path)?;
let mut document: DocumentMut = content.parse()?;
document["tools"][alias.as_ref()] = toml_edit::value(id.to_string());

fs_err::write(manifest_path, document.to_string())?;
fs::write(manifest_path, document.to_string())?;

log::info!(
"Tool {alias} = {id} has been added to {}",
Expand Down
5 changes: 2 additions & 3 deletions src/system_path/unix.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::env;
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::Path;

use fs_err::OpenOptions;

use crate::config::write_if_not_exists;
use crate::dirs::home_dir;
use crate::home::Home;
Expand Down Expand Up @@ -48,7 +47,7 @@ pub fn add(home: &Home) -> anyhow::Result<bool> {
}

fn append_line_if_not_present(path: &Path, line: &str, create: bool) -> anyhow::Result<bool> {
let ends_with_newline = match fs_err::read_to_string(path) {
let ends_with_newline = match fs::read_to_string(path) {
// This file already has this line, skip it.
Ok(contents) if contents.contains(line) => return Ok(false),
Ok(contents) if contents.ends_with('\n') => true,
Expand Down
32 changes: 16 additions & 16 deletions src/tool_storage.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::borrow::Cow;
use std::cell::OnceCell;
use std::collections::BTreeSet;
use std::env::current_dir;
use std::env::{consts::EXE_SUFFIX, current_exe};
use std::fmt::Write;
use std::io::{self, BufWriter, Read};
use std::fs::{self, File};
use std::io::{self, BufWriter, IsTerminal, Read};
use std::io::{Seek, Write as _};
use std::path::{Path, PathBuf};

use anyhow::{bail, Context};
use fs_err::File;
use itertools::{Either, Itertools};
use once_cell::unsync::OnceCell;

use crate::auth::AuthManifest;
use crate::home::Home;
Expand All @@ -33,10 +33,10 @@ pub struct ToolStorage {
impl ToolStorage {
pub fn new(home: &Home) -> anyhow::Result<Self> {
let storage_dir = home.path().join("tool-storage");
fs_err::create_dir_all(&storage_dir)?;
fs::create_dir_all(&storage_dir)?;

let bin_dir = home.path().join("bin");
fs_err::create_dir_all(&bin_dir)?;
fs::create_dir_all(&bin_dir)?;

let auth = AuthManifest::load(home)?;

Expand Down Expand Up @@ -99,14 +99,14 @@ impl ToolStorage {
log::debug!("Copying own executable into temp dir");
let source_dir = tempfile::tempdir()?;
let source_path = source_dir.path().join(self_name);
fs_err::copy(&self_path, &source_path)?;
fs::copy(&self_path, &source_path)?;
let self_path = source_path;

let junk_dir = tempfile::tempdir()?;
let aftman_name = format!("aftman{EXE_SUFFIX}");
let mut found_aftman = false;

for entry in fs_err::read_dir(&self.bin_dir)? {
for entry in fs::read_dir(&self.bin_dir)? {
let entry = entry?;
let path = entry.path();
let name = path.file_name().unwrap().to_str().unwrap();
Expand All @@ -119,15 +119,15 @@ impl ToolStorage {

// Copy the executable into a temp directory so that we can replace
// it even if it's currently running.
fs_err::rename(&path, junk_dir.path().join(name))?;
fs_err::copy(&self_path, path)?;
fs::rename(&path, junk_dir.path().join(name))?;
fs::copy(&self_path, path)?;
}

// If we didn't find and update Aftman already, install it.
if !found_aftman {
log::info!("Installing Aftman...");
let aftman_path = self.bin_dir.join(aftman_name);
fs_err::copy(&self_path, aftman_path)?;
fs::copy(&self_path, aftman_path)?;
}

log::info!("Updated Aftman binaries successfully!");
Expand Down Expand Up @@ -347,7 +347,7 @@ impl ToolStorage {
if mode == TrustMode::Check {
// If the terminal isn't interactive, tell the user that they
// need to open an interactive terminal to trust this tool.
if atty::isnt(atty::Stream::Stderr) {
if !io::stderr().is_terminal() {
bail!(
"Tool {name} has never been installed. \
Run `aftman add {name}` in your terminal to install it and trust this tool.",
Expand Down Expand Up @@ -390,7 +390,7 @@ impl ToolStorage {
fn install_executable(&self, id: &ToolId, mut contents: impl Read) -> anyhow::Result<()> {
let output_path = self.exe_path(id);

fs_err::create_dir_all(output_path.parent().unwrap())?;
fs::create_dir_all(output_path.parent().unwrap())?;

let mut output = BufWriter::new(File::create(&output_path)?);
io::copy(&mut contents, &mut output)?;
Expand All @@ -412,7 +412,7 @@ impl ToolStorage {
let output_path = self.exe_path(id);
let expected_name = format!("{}{EXE_SUFFIX}", id.name().name());

fs_err::create_dir_all(output_path.parent().unwrap())?;
fs::create_dir_all(output_path.parent().unwrap())?;

// If there is an executable with an exact name match, install that one.
let mut zip = zip::ZipArchive::new(artifact)?;
Expand Down Expand Up @@ -448,7 +448,7 @@ impl ToolStorage {
let link_name = format!("{}{}", alias.as_ref(), EXE_SUFFIX);
let link_path = self.bin_dir.join(link_name);

fs_err::copy(self_path, link_path).context("Failed to create Aftman alias")?;
fs::copy(self_path, link_path).context("Failed to create Aftman alias")?;
Ok(())
}

Expand All @@ -469,7 +469,7 @@ pub struct InstalledToolsCache {

impl InstalledToolsCache {
pub fn read(path: &Path) -> anyhow::Result<Self> {
let contents = match fs_err::read_to_string(path) {
let contents = match fs::read_to_string(path) {
Ok(v) => v,
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Expand Down Expand Up @@ -497,7 +497,7 @@ impl InstalledToolsCache {
writeln!(&mut output, "{}", tool).unwrap();
}

fs_err::write(path, output)?;
fs::write(path, output)?;
Ok(())
}
}
5 changes: 3 additions & 2 deletions src/trust.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeSet;
use std::fmt::Write;
use std::fs;
use std::io;

use anyhow::bail;
Expand Down Expand Up @@ -28,7 +29,7 @@ impl TrustCache {
pub fn read(home: &Home) -> anyhow::Result<Self> {
let path = home.path().join("trusted.txt");

let contents = match fs_err::read_to_string(path) {
let contents = match fs::read_to_string(path) {
Ok(v) => v,
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Expand Down Expand Up @@ -57,7 +58,7 @@ impl TrustCache {
}

let path = home.path().join("trusted.txt");
fs_err::write(path, output)?;
fs::write(path, output)?;

return Ok(true);
}
Expand Down

0 comments on commit 8b080d9

Please sign in to comment.