|
@@ -3,25 +3,37 @@ use cache::relative_to_absolute;
|
|
|
use clap::{Parser, Subcommand};
|
|
|
use std::env;
|
|
|
use std::fs::File;
|
|
|
+use std::io::{BufRead, BufReader};
|
|
|
use std::path::PathBuf;
|
|
|
-use std::io::{BufRead, BufReader}; // , Write, stdout};
|
|
|
use std::process::Command;
|
|
|
|
|
|
mod cache;
|
|
|
|
|
|
+// I'm not sure if reqwest::blocking's write_to is working right or not.
|
|
|
+// It seems like it might be storing the entire file in memory...
|
|
|
+// Which I don't want. I might have to go back to tokio and chunk.
|
|
|
+
|
|
|
// see reqwest/web-o/src/cache.rs for example cache
|
|
|
// It restores reqwest::header::HeaderMap
|
|
|
// (which allows duplicates... and ignores case on keys)
|
|
|
|
|
|
#[derive(Parser)]
|
|
|
-#[command(about = "Go updater")]
|
|
|
+#[command(
|
|
|
+ about = "Go Updater",
|
|
|
+ long_about = "Go Updater
|
|
|
+
|
|
|
+This checks the https://go.dev/dl for newer versions of go.
|
|
|
+It depends upon the GOPATH and GOROOT environment variables being set.
|
|
|
+
|
|
|
+This can't update a package manager installed version of go (permissions)."
|
|
|
+)]
|
|
|
struct Cli {
|
|
|
/// Cache directory path
|
|
|
- #[arg(default_value = "cache")]
|
|
|
+ #[arg(short, long, default_value = "cache")]
|
|
|
cache: PathBuf,
|
|
|
|
|
|
#[command(subcommand)]
|
|
|
- command: Option<Commands>,
|
|
|
+ command: Commands, // command: Option<Commands>,
|
|
|
}
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
@@ -44,7 +56,7 @@ fn find_go_version() -> Result<String> {
|
|
|
}
|
|
|
|
|
|
/// Locate the go binary
|
|
|
-///
|
|
|
+///
|
|
|
/// This is redundant, it should be located via GO_PATH.
|
|
|
#[allow(dead_code)]
|
|
|
#[must_use]
|
|
@@ -88,7 +100,7 @@ and grab the section of td's. class=filename has a href, last has SHA256.
|
|
|
|
|
|
*/
|
|
|
|
|
|
-/*
|
|
|
+/*
|
|
|
fn download_and_save(url: &str, filename: &str) -> Result<()> {
|
|
|
let client = reqwest::blocking::Client::builder()
|
|
|
.user_agent(APP_USER_AGENT)
|
|
@@ -127,9 +139,9 @@ fn version_from_url(url: &str, arch: &str) -> Option<String> {
|
|
|
#[must_use]
|
|
|
/// Get go version from `go version` output.
|
|
|
fn version_from_go(text: &str) -> Option<String> {
|
|
|
- let parts : Vec<&str> = text.split(' ').collect();
|
|
|
+ let parts: Vec<&str> = text.split(' ').collect();
|
|
|
if parts.len() == 4 {
|
|
|
- return Some(parts[2].to_string().replace("go", ""))
|
|
|
+ return Some(parts[2].to_string().replace("go", ""));
|
|
|
}
|
|
|
None
|
|
|
}
|
|
@@ -137,19 +149,19 @@ fn version_from_go(text: &str) -> Option<String> {
|
|
|
/// Return just the href="<return this part>".
|
|
|
#[must_use]
|
|
|
fn just_href(link: &str) -> Result<String> {
|
|
|
- let parts = link.split_once("href=\"").unwrap_or(("","")).1;
|
|
|
- let href = parts.split_once("\"").unwrap_or(("", "")).0;
|
|
|
+ let parts = link.split_once("href=\"").unwrap_or(("", "")).1;
|
|
|
+ let href = parts.split_once("\"").unwrap_or(("", "")).0;
|
|
|
if !href.is_empty() {
|
|
|
- return Ok(href.to_string());
|
|
|
+ return Ok(href.to_string());
|
|
|
}
|
|
|
bail!("Unable to locate href");
|
|
|
}
|
|
|
|
|
|
/// Find a href link for given arch (architecture)
|
|
|
-///
|
|
|
+///
|
|
|
/// Look for <a class="download" href="
|
|
|
#[must_use]
|
|
|
-fn find_arch_link(arch: &str, fp:&File) -> Result<String> {
|
|
|
+fn find_arch_link(arch: &str, fp: &File) -> Result<String> {
|
|
|
let reader = BufReader::new(fp);
|
|
|
for line in reader.lines() {
|
|
|
if let Ok(line) = line {
|
|
@@ -165,10 +177,12 @@ fn find_arch_link(arch: &str, fp:&File) -> Result<String> {
|
|
|
}
|
|
|
|
|
|
/// find_link for given arch (architecture)
|
|
|
-///
|
|
|
+///
|
|
|
/// Look for <a class="download" href=""
|
|
|
#[must_use]
|
|
|
-fn find_link(arch: &str) -> Result<String> { // , Box<dyn Error>> {
|
|
|
+#[deprecated]
|
|
|
+fn find_link(arch: &str) -> Result<String> {
|
|
|
+ // , Box<dyn Error>> {
|
|
|
let fp = File::open(GO_FILE)?;
|
|
|
let reader = BufReader::new(fp);
|
|
|
for line in reader.lines() {
|
|
@@ -215,7 +229,7 @@ fn main() -> Result<()> {
|
|
|
let mut arch = parts.last().unwrap().to_string();
|
|
|
arch = arch.replace("/", "-");
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
println!("GO_PATH {}", go_path);
|
|
|
println!("GO_ROOT {}", go_root);
|
|
|
println!("version: {}", go_version);
|
|
@@ -229,7 +243,7 @@ fn main() -> Result<()> {
|
|
|
// Get go version and path
|
|
|
|
|
|
match &cli.command {
|
|
|
- Some(Commands::Update {}) => {
|
|
|
+ Commands::Update {} => {
|
|
|
let status = cache.fetch(GO_URL);
|
|
|
|
|
|
// Check to see if file already exists AND
|
|
@@ -247,6 +261,7 @@ fn main() -> Result<()> {
|
|
|
if let Some(latest) = latest_version {
|
|
|
println!("Version: {} [have {}]", latest, version);
|
|
|
if version != latest {
|
|
|
+ println!("Downloading newer version...");
|
|
|
let latest_status = cache.fetch(&abs);
|
|
|
println!("Latest: {:?}", latest_status);
|
|
|
}
|
|
@@ -254,7 +269,7 @@ fn main() -> Result<()> {
|
|
|
println!("Finding version failed for string: [{}]", abs);
|
|
|
}
|
|
|
}
|
|
|
- },
|
|
|
+ }
|
|
|
cache::Status::Cached(fp) => {
|
|
|
println!("(from cache)"); // I wish I could see this.
|
|
|
|
|
@@ -269,18 +284,23 @@ fn main() -> Result<()> {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- Some(Commands::Info {}) => {
|
|
|
+ Commands::Info {} => {
|
|
|
println!("GO_PATH {}", go_path);
|
|
|
println!("GO_ROOT {}", go_root);
|
|
|
println!("go ver: {}", go_version);
|
|
|
println!("version: {}", version);
|
|
|
println!("where: {}", go_where);
|
|
|
println!("arch: {}", arch);
|
|
|
- }
|
|
|
- None => {
|
|
|
- // Display help.
|
|
|
- let _show_help: Cli = Cli::parse_from(["--help"]);
|
|
|
- }
|
|
|
+ } /*
|
|
|
+ None => {
|
|
|
+ // Display help.
|
|
|
+ println!("No option selected. Oops.");
|
|
|
+ cli
|
|
|
+ // Why isn't this working?
|
|
|
+ let _show_help: Cli = Cli::parse_from(["--help"]); // WHAT?
|
|
|
+ println!("... DERP?");
|
|
|
+ }
|
|
|
+ */
|
|
|
}
|
|
|
Ok(())
|
|
|
}
|