From 1b496211a6fed1c76217b06cc14ee89ac89991b3 Mon Sep 17 00:00:00 2001 From: Mutzi Date: Wed, 25 Jan 2023 13:25:12 +0100 Subject: [PATCH] Added self update function --- .gitlab-ci.yml | 38 ++--- Cargo.lock | 391 +++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 16 +- src/main.rs | 24 ++- src/update.rs | 50 +++++++ 5 files changed, 455 insertions(+), 64 deletions(-) create mode 100644 src/update.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 095de30..b45c3bc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,32 +3,20 @@ stages: - release variables: - PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/insatller/" + PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/installer/" -build_glibc: - stage: build - image: rust:bullseye - rules: - - if: $CI_COMMIT_BRANCH == "installer" - script: - - cargo build --release - - cp target/release/dotfiles_installer ./installer-amd64-glibc - artifacts: - paths: - - installer-amd64-glibc - -build_muslc: +build: stage: build image: rust:alpine rules: - if: $CI_COMMIT_BRANCH == "installer" script: - - apk add pkgconf musl-dev openssl-dev - - cargo build --release - - cp target/release/dotfiles_installer ./installer-amd64-muslc + - apk add pkgconf musl-dev + - RUSTFLAGS='-C target-feature=+crt-static -C link-self-contained=yes -C link-arg=-s' cargo build --release --target x86_64-unknown-linux-musl + - cp target/x86_64-unknown-linux-musl/release/dotfiles_installer ./installer-amd64 artifacts: paths: - - installer-amd64-muslc + - installer-amd64 upload_assets: @@ -37,13 +25,10 @@ upload_assets: rules: - if: $CI_COMMIT_BRANCH == "installer" needs: - - job: build_glibc - artifacts: true - - job: build_muslc + - job: build artifacts: true script: - - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file installer-amd64-glibc "${PACKAGE_REGISTRY_URL}/dev-$CI_COMMIT_SHORT_SHA/installer-amd64-glibc"' - - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file installer-amd64-muslc "${PACKAGE_REGISTRY_URL}/dev-$CI_COMMIT_SHORT_SHA/installer-amd64-muslc"' + - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file installer-amd64 "${PACKAGE_REGISTRY_URL}/dev-$CI_COMMIT_SHORT_SHA/installer-amd64"' create_release: stage: release @@ -59,10 +44,7 @@ create_release: description: 'Release dev-$CI_COMMIT_SHORT_SHA' assets: links: - - name: 'installer-amd64-glibc' - url: '${PACKAGE_REGISTRY_URL}/dev-$CI_COMMIT_SHORT_SHA/installer-amd64-glibc' - link_type: package - - name: 'installer-amd64-muslc' - url: '${PACKAGE_REGISTRY_URL}/dev-$CI_COMMIT_SHORT_SHA/installer-amd64-muslc' + - name: 'installer-amd64' + url: '${PACKAGE_REGISTRY_URL}/dev-$CI_COMMIT_SHORT_SHA/installer-amd64' link_type: package diff --git a/Cargo.lock b/Cargo.lock index ba94ac3..8f5581e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,52 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "attohttpc" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b85f766c20e6ae766956f7a2fcc4e0931e79a7e1f48b29132b5d647021114914" +dependencies = [ + "http", + "log", + "rustls", + "serde", + "serde_json", + "url", + "webpki", + "webpki-roots", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bumpalo" +version = "3.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" + +[[package]] +name = "bytes" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c" + +[[package]] +name = "cc" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" + [[package]] name = "cfg-if" version = "1.0.0" @@ -43,11 +83,11 @@ dependencies = [ name = "dotfiles_installer" version = "0.1.0" dependencies = [ + "attohttpc", "dialoguer", "fs_extra", "indicatif", "serde", - "strum", "toml", ] @@ -66,6 +106,21 @@ dependencies = [ "instant", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +dependencies = [ + "percent-encoding", +] + [[package]] name = "fs_extra" version = "1.2.0" @@ -73,10 +128,41 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" [[package]] -name = "heck" -version = "0.4.0" +name = "hashbrown" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "http" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "idna" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] [[package]] name = "indicatif" @@ -99,6 +185,21 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "itoa" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" + +[[package]] +name = "js-sys" +version = "0.3.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -111,12 +212,48 @@ version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "nom8" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae01545c9c7fc4486ab7debaf2aad7003ac19431791868fb2e8066df97fad2f8" +dependencies = [ + "memchr", +] + [[package]] name = "number_prefix" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "percent-encoding" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" + [[package]] name = "portable-atomic" version = "0.3.19" @@ -160,10 +297,47 @@ dependencies = [ ] [[package]] -name = "rustversion" -version = "1.0.11" +name = "ring" +version = "0.16.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin", + "untrusted", + "web-sys", + "winapi", +] + +[[package]] +name = "rustls" +version = "0.20.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f" +dependencies = [ + "log", + "ring", + "sct", + "webpki", +] + +[[package]] +name = "ryu" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" + +[[package]] +name = "sct" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" +dependencies = [ + "ring", + "untrusted", +] [[package]] name = "serde" @@ -185,6 +359,26 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_spanned" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c68e921cef53841b8925c2abadd27c9b891d9613bdc43d6b823062866df38e8" +dependencies = [ + "serde", +] + [[package]] name = "shell-words" version = "1.1.0" @@ -192,26 +386,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" [[package]] -name = "strum" -version = "0.24.1" +name = "spin" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" -dependencies = [ - "strum_macros", -] - -[[package]] -name = "strum_macros" -version = "0.24.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "rustversion", - "syn", -] +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "syn" @@ -239,26 +417,181 @@ dependencies = [ ] [[package]] -name = "toml" -version = "0.5.11" +name = "tinyvec" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + +[[package]] +name = "toml" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb9d890e4dc9298b70f740f615f2e05b9db37dce531f6b24fb77ac993f9f217" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5" dependencies = [ "serde", ] +[[package]] +name = "toml_edit" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "729bfd096e40da9c001f778f5cdecbd2957929a24e10e5883d9392220a751581" +dependencies = [ + "indexmap", + "nom8", + "serde", + "serde_spanned", + "toml_datetime", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58" + [[package]] name = "unicode-ident" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-width" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + +[[package]] +name = "url" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" + +[[package]] +name = "web-sys" +version = "0.3.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "webpki-roots" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" +dependencies = [ + "webpki", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index ed54619..7b9c4e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,17 @@ edition = "2021" [dependencies] dialoguer = "0.10.3" indicatif = "0.17.3" -strum = { version = "0.24.1", features = ["derive"] } -serde = { version = "1.0.152", features = ["derive"] } -toml = "0.5.10" +toml = "0.6.0" fs_extra = "1.2.0" + +[dependencies.serde] +version = "1.0.152" +features = ["derive"] + +[dependencies.attohttpc] +version = "0.24.0" +default_features = false +features = [ + "json", + "tls-rustls-webpki-roots" +] diff --git a/src/main.rs b/src/main.rs index 40afaa6..f9421b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,9 @@ mod repository; mod config; mod operations; mod utils; +mod update; -use strum::{EnumIter, IntoEnumIterator}; - -#[derive(Debug, Clone, EnumIter)] +#[derive(Debug, Clone)] enum MainMenu { Install, Collect, @@ -30,6 +29,13 @@ impl ToString for MainMenu { } fn main() { + if let Some(version) = option_env!("CI_COMMIT_SHA") { + println!("Starting installer version {}", version); + update::check_for_updates(version); + } else { + println!("Starting installer version unknown"); + } + if !repository::open_repo() { println!("Failed to open/clone repo!"); return; @@ -50,7 +56,17 @@ fn main() { }; 'main_loop: loop { - let res = prompt::select(Some("What do you want to do?"), MainMenu::iter().collect()); + let res = prompt::select( + Some("What do you want to do?"), + vec![ + MainMenu::Install, + MainMenu::Collect, + MainMenu::Pull, + MainMenu::Diff, + MainMenu::Upload, + MainMenu::Quit + ] + ); match res { MainMenu::Install => operations::install(&mods), MainMenu::Collect => operations::collect(&mods), diff --git a/src/update.rs b/src/update.rs new file mode 100644 index 0000000..c851e4a --- /dev/null +++ b/src/update.rs @@ -0,0 +1,50 @@ +use std::env::current_exe; +use std::fs; +use std::fs::File; +use std::os::unix::process::CommandExt; +use std::path::Path; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +struct Commit { + id: String +} + +#[derive(Debug, Deserialize)] +struct Link { + direct_asset_url: String +} + +#[derive(Debug, Deserialize)] +struct Assets { + links: Vec +} + +#[derive(Debug, Deserialize)] +struct ReleaseEntry { + commit: Commit, + assets: Assets +} + +pub fn check_for_updates(version: &str) { + print!("Checking for updates... "); + let resp: Vec = attohttpc::get("https://gitlab.mattv.de/api/v4/projects/43/releases").send().unwrap().json().unwrap(); + let newest = resp.first().unwrap(); + if newest.commit.id != version { + println!("New version exists"); + let exe = current_exe().unwrap(); + let temp = Path::new("temp"); + + print!("Downloading... "); + attohttpc::get(newest.assets.links.first().unwrap().direct_asset_url.clone()).send().unwrap().write_to(File::create(temp).unwrap()).unwrap(); + println!("Done"); + + fs::set_permissions(&temp, exe.metadata().unwrap().permissions()).unwrap(); + + fs::rename(&temp, &exe).unwrap(); + + std::process::Command::new(exe).exec(); + std::process::exit(1); + } + println!("No new version"); +} \ No newline at end of file