Read versions from Cargo.toml ref #3 #11

Merged
cadey merged 4 commits from cargo-version into main 2020-10-25 19:21:31 +00:00
5 changed files with 68 additions and 11 deletions
Showing only changes of commit 26894c7fad - Show all commits

38
Cargo.lock generated
View File

@ -98,6 +98,17 @@ version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
[[package]]
name = "cargo_toml"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "513d17226888c7b8283ac02a1c1b0d8a9d4cbf6db65dfadb79f598f5d7966fe9"
dependencies = [
"serde",
"serde_derive",
"toml",
]
[[package]]
name = "cc"
version = "1.0.54"
@ -351,6 +362,7 @@ name = "gitea-release"
version = "0.5.2"
dependencies = [
"anyhow",
"cargo_toml",
"comrak",
"elfs",
"git2",
@ -365,6 +377,7 @@ dependencies = [
"structopt",
"tempfile",
"tokio",
"toml",
"url",
]
@ -881,9 +894,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.17"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1502d12e458c49a4c9cbff560d0fe0060c252bc29799ed94ca2ed4bb665a0101"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [
"unicode-xid",
]
@ -1100,18 +1113,18 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.111"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9124df5b40cbd380080b2cc6ab894c040a3070d995f5c9dc77e18c34a8ae37d"
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.111"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f2c3ac8e6ca1e9c80b8be1023940162bf81ae3cffbb1809474152f2ce1eb250"
checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
dependencies = [
"proc-macro2",
"quote",
@ -1209,9 +1222,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.29"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb37da98a55b1d08529362d9cbb863be17556873df2585904ab9d2bc951291d0"
checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac"
dependencies = [
"proc-macro2",
"quote",
@ -1353,6 +1366,15 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645"
dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.0"

View File

@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
anyhow = "1.0"
cargo_toml = "0.8.1"
comrak = "0.7"
git2 = "0.13"
http = "0.2"
@ -19,6 +20,7 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
structopt = { version = "0.3", default-features = false }
tokio = { version = "0.2", features = ["macros"] }
toml = "0.5.7"
url = "2"
gitea = { path = "./gitea" }

29
src/version/cargo.rs Normal file
View File

@ -0,0 +1,29 @@
use anyhow::Result;
use cargo_toml::Manifest;
use std::fs::{self, File};
use std::io::Read;
fn get_file_as_byte_vec(filename: &str) -> Option<Vec<u8>> {
let f = File::open(&filename);
if f.is_err() {
return None;
}
let mut f = f.unwrap();
let metadata = fs::metadata(&filename).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");
Some(buffer)
}
pub fn read() -> Result<Option<String>> {
let bytes = get_file_as_byte_vec("Cargo.toml");
match bytes {
Some(bytes) => {
let pkg: Manifest = toml::from_slice(&bytes)?;
Ok(Some(pkg.package.unwrap().version))
}
None => Ok(None)
}
}

View File

@ -1,9 +1,13 @@
use anyhow::Result;
use std::{fs, path::PathBuf};
mod cargo;
pub(crate) fn read_version(fname: PathBuf) -> Result<String> {
let version = fs::read_to_string(fname)?;
Ok(version.trim().into())
let version = cargo::read()
.unwrap()
.unwrap_or(fs::read_to_string(fname)?.trim().into());
Ok(version)
}
#[cfg(test)]

View File

@ -10,7 +10,7 @@ const TAG: &'static str = "0.1.0";
async fn cut() -> Result<()> {
let _ = pretty_env_logger::try_init();
let name = elfs::next();
let token = std::env::var("DOMO_GITEA_TOKEN")?;
let token = std::env::var("DOMO_GITEA_TOKEN").expect("wanted envvar DOMO_GITEA_TOKEN");
let cli = gitea::Client::new("https://tulpa.dev".into(), token.clone(), APP_USER_AGENT)?;
debug!("created gitea client");