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

Use XDG_CACHE_HOME as download path #28

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ notifications:
script:
- cargo build --verbose --all
- cargo test --verbose --all
- cargo run -- --verbose && rm -r target/debug/bin
- cargo run -- --verbose
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sys-info = "0.5.8"
reqwest = "0.9.22"
tar = "0.4.26"
flate2 = "1.0.13"
xdg = "^2.1"

[dev-dependencies]
tempfile = "3.1.0"
Expand Down
40 changes: 5 additions & 35 deletions src/checker.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::architecture::Architecture;
use crate::error::{Error, Result};
use crate::error::Result;
use crate::ostype::OsType;
use std::{fs, io, path::Path, path::PathBuf};
use std::{fmt::Display, fs, io, path::Path};

#[cfg(test)]
mod tests {
use super::*;
use crate::error::Result;
use flate2::write::GzEncoder;
use flate2::Compression;
use std::env;
use std::env::consts;
use std::fs::File;
use tempfile::NamedTempFile;
Expand Down Expand Up @@ -49,33 +48,11 @@ mod tests {
assert_eq!(false, path_exists(&path));
}

#[test]
fn test_get_base_path() {
let pwd = env::current_dir().unwrap();
let pwd = pwd.display();

// this is a little hacky... it only works as long as debug assertions stay dissabled for
// release builds. but for now `cargo test` and `cargo test --release` work, when executed
// from the project root
#[cfg(debug_assertions)]
let profile = "debug";
#[cfg(not(debug_assertions))]
let profile = "release";

let expected = format!(
"{}{sep}target{sep}{}{sep}deps",
pwd,
profile,
sep = std::path::MAIN_SEPARATOR
);
let base_path = get_base_path(env::current_exe().unwrap()).unwrap();
assert_eq!(expected, base_path)
}

#[test]
fn test_download() -> Result<()> {
let base_url = generate_base_url("2.0.3");
let base_path = get_base_path(env::current_exe().unwrap()).unwrap();
let base_path = tempfile::tempdir().unwrap();
let base_path = base_path.path().display().to_string();
let architecture = consts::ARCH.parse::<Architecture>()?;
let os_type = consts::OS.parse::<OsType>()?;

Expand Down Expand Up @@ -140,7 +117,7 @@ pub fn generate_base_url(version: &str) -> String {
format!("{}/{}", base_url, version)
}

pub fn download(base_url: &str, base_path: &str, filename: &str) -> Result<()> {
pub fn download(base_url: &str, base_path: impl Display, filename: impl Display) -> Result<()> {
let filepath = format!("{}/{}.tar.gz", base_path, filename);
let url = format!("{}/{}.tar.gz", base_url, filename);
let mut resp = reqwest::get(&url)?;
Expand All @@ -158,10 +135,3 @@ pub fn unpack(tar_path: impl AsRef<Path>, base_path: impl AsRef<Path>) -> Result

Ok(())
}

pub fn get_base_path(path: PathBuf) -> Result<String> {
path.parent()
.map(Path::display)
.map(|path| path.to_string())
.ok_or(Error::InvalidBasePath)
}
10 changes: 8 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum Error {
Network(reqwest::Error),
Encoding(std::str::Utf8Error),
Output(fmt::Error),
InvalidBasePath,
Xdg(xdg::BaseDirectoriesError),
}

impl fmt::Display for Error {
Expand All @@ -25,11 +25,17 @@ impl fmt::Display for Error {
Network(err) => write!(fmt, "Error downloading the file ({})", err),
Encoding(err) => write!(fmt, "Encoding error ({})", err),
Output(err) => write!(fmt, "Output error ({})", err),
InvalidBasePath => write!(fmt, "Invalid Base Path"),
Xdg(err) => write!(fmt, "XdgError({})", err),
}
}
}

impl From<xdg::BaseDirectoriesError> for Error {
fn from(err: xdg::BaseDirectoriesError) -> Self {
Error::Xdg(err)
}
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::IO(err)
Expand Down
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod ostype;

use crate::{architecture::Architecture, error::Result, ostype::OsType};
use std::{
env,
env::consts,
io::{self, Write},
process,
Expand All @@ -14,18 +13,23 @@ use std::{
fn main() -> Result<()> {
let version = "2.0.3";

// create XDG profile
let xdg_dirs = xdg::BaseDirectories::with_profile("editorconfig-checker", version)?;
let architecture = consts::ARCH.parse::<Architecture>()?;
let os_type = consts::OS.parse::<OsType>()?;

let base_path = checker::get_base_path(env::current_exe()?)?;
// create XDG cache dir in case it does not exist
xdg_dirs.create_cache_directory("")?;
// use XDG cache home as base path
let base_path = xdg_dirs.get_cache_home();
let filename = checker::generate_filename(os_type, architecture);
let tar_path = format!("{}/{}.tar.gz", base_path, filename);
let binary_path = format!("{}/bin/{}", base_path, filename);
let tar_path = format!("{}/{}.tar.gz", base_path.display(), filename);
let binary_path = format!("{}/bin/{}", base_path.display(), filename);

if !checker::path_exists(&binary_path) {
let base_url: String = checker::generate_base_url(version);

checker::download(&base_url, &base_path, &filename)?;
checker::download(&base_url, base_path.display(), &filename)?;
checker::unpack(&tar_path, &base_path)?;
}

Expand Down