Skip to content

Commit

Permalink
Added self-update command
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Nov 26, 2018
1 parent 399d253 commit f186ed8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 19 deletions.
93 changes: 74 additions & 19 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -225,26 +225,30 @@ wasmer_install() {
magenta2="${reset}\033[34m"
magenta3="${reset}\033[34;2m"

printf "${reset}Installing Wasmer!$reset\n"
printf "
${magenta1} ${magenta2} ${magenta3}###${reset}
${magenta1} ${magenta2} ${magenta3}#####${reset}
${magenta1} ${magenta2}### ${magenta3}######${reset}
${magenta1} ${magenta2}###### ${magenta3}#############${reset}
${magenta1}# ${magenta2}####### ${magenta3}##############${reset}
${magenta1}##### ${magenta2}#############${magenta3}#########${reset}
${magenta1}######${magenta2}###############${magenta3}#######${reset}
${magenta1}############${magenta2}#########${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3} ###${reset}
${magenta1}##############${magenta2}#######
${magenta1}###########${magenta2} ###
${magenta1}########${magenta2}
${magenta1}####${reset}
if which wasmer >/dev/null; then
printf "${reset}Updating wasmer$reset\n"
else
printf "${reset}Installing Wasmer!$reset\n"
printf "
${magenta1} ${magenta2} ${magenta3}###${reset}
${magenta1} ${magenta2} ${magenta3}#####${reset}
${magenta1} ${magenta2}### ${magenta3}######${reset}
${magenta1} ${magenta2}###### ${magenta3}#############${reset}
${magenta1}# ${magenta2}####### ${magenta3}##############${reset}
${magenta1}##### ${magenta2}#############${magenta3}#########${reset}
${magenta1}######${magenta2}###############${magenta3}#######${reset}
${magenta1}############${magenta2}#########${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3}#######${reset}
${magenta1}##############${magenta2}#######${magenta3} ###${reset}
${magenta1}##############${magenta2}#######
${magenta1}###########${magenta2} ###
${magenta1}########${magenta2}
${magenta1}####${reset}
"
fi
# if [ -d "$HOME/.wasmer" ]; then
# if which wasmer; then
# local latest_url
Expand Down Expand Up @@ -290,9 +294,41 @@ wasmer_install() {


wasmer_reset() {
unset -f wasmer_install wasmer_reset wasmer_download_json wasmer_link wasmer_detect_profile wasmer_download_file wasmer_download wasmer_verify_or_quit
unset -f wasmer_install wasmer_compareversions wasmer_reset wasmer_download_json wasmer_link wasmer_detect_profile wasmer_download_file wasmer_download wasmer_verify_or_quit
}

# Example taken from
# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
wasmer_compareversions () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}

wasmer_download() {
# identify platform based on uname output
Expand Down Expand Up @@ -322,6 +358,25 @@ wasmer_download() {
printf "\033[1A$cyan> Getting wasmer releases... ✓$reset\n"
fi

if which wasmer >/dev/null; then
WASMER_VERSION=$(wasmer --version | sed 's/[a-z[:blank:]]//g')
wasmer_compareversions "$WASMER_VERSION" "$WASMER_RELEASE_TAG"
case $? in
# WASMER_VERSION = WASMER_RELEASE_TAG
0)
printf "You are already on the latest wasmer: ${WASMER_RELEASE_TAG}\n";
exit 0
;;
# WASMER_VERSION > WASMER_RELEASE_TAG
1)
printf "You are already on a more recent version than the published one: ${WASMER_RELEASE_TAG}.\nExiting\n";
exit 0
;;
# WASMER_VERSION < WASMER_RELEASE_TAG (we continue)
2)
;;
esac
fi
# fetch the real release data to make sure it exists before we attempt a download
wasmer_download_json RELEASE_DATA "$RELEASES_URL/tag/$WASMER_RELEASE_TAG"

Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod macros;
mod recovery;
pub mod apis;
pub mod common;
mod update;
pub mod sighandler;
#[cfg(test)]
mod spectests;
Expand All @@ -41,6 +42,10 @@ enum CLIOptions {
/// Run a WebAssembly file. Formats accepted: wasm, wast
#[structopt(name = "run")]
Run(Run),

/// Update wasmer to the latest version
#[structopt(name = "self-update")]
SelfUpdate,
}

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -117,5 +122,6 @@ fn main() {
let options = CLIOptions::from_args();
match options {
CLIOptions::Run(options) => run(options),
CLIOptions::SelfUpdate => update::self_update(),
}
}
17 changes: 17 additions & 0 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! When wasmer self-update is executed, this is what gets executed
use std::process::{Command, Stdio};
use std::io;

pub fn self_update() {
println!("Fetching latest installer");
let cmd = Command::new("curl").arg("https://get.wasmer.io").arg("-sSfL")
.stdout(Stdio::piped()).spawn().unwrap();

let mut the_process = Command::new("sh")
.stdin(cmd.stdout.unwrap())
.stdout(Stdio::inherit())
.spawn()
.ok().expect("Failed to execute.");

the_process.wait();
}

0 comments on commit f186ed8

Please sign in to comment.