123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- use anyhow::{bail, Result};
- use cache::relative_to_absolute;
- use clap::{Parser, Subcommand};
- use std::env;
- use std::fs::File;
- use std::path::PathBuf;
- use std::io::{BufRead, BufReader}; // , Write, stdout};
- use std::process::Command;
- mod cache;
- // 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")]
- struct Cli {
- /// Cache directory path
- #[arg(default_value = "cache")]
- cache: PathBuf,
- #[command(subcommand)]
- command: Option<Commands>,
- }
- #[derive(Subcommand)]
- enum Commands {
- /// Update go
- Update {},
- /// Display information
- Info {},
- }
- /// Query `go version`
- #[must_use]
- fn find_go_version() -> Result<String> {
- let output = Command::new("go").arg("version").output()?;
- if output.status.success() {
- // Ok! We have something!
- return Ok(String::from_utf8_lossy(output.stdout.as_slice()).to_string());
- }
- bail!("Failed to query go version.");
- }
- /// Locate the go binary
- ///
- /// This is redundant, it should be located via GO_PATH.
- #[allow(dead_code)]
- #[must_use]
- fn find_go() -> Result<String> {
- let output = Command::new("which").arg("go").output()?;
- if output.status.success() {
- return Ok(String::from_utf8_lossy(output.stdout.as_slice()).to_string());
- }
- bail!("Failed to locate go binary.");
- }
- const GO_URL: &str = "https://go.dev/dl/";
- const GO_FILE: &str = "go-dl.html";
- // 2 MB download of html...
- // static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
- /*
- CSS Selector elements of interest: (a href="" part).
- href are relative...
- div[class="downloadWrapper"] > a[class="download downloadBox"]
- <a class="download downloadBox" href="/dl/go1.24.0.windows-amd64.msi">
- <a class="download downloadBox" href="/dl/go1.24.0.linux-amd64.tar.gz">
- table[class="downloadtable"] > tr[class=" "] > td[class="filename"] > a[class="download"]
- Or possibly,
- table[class="downloadtable"] > tr[class=" "]
- or tr[class="highlight "] ?
- and grab the section of td's. class=filename has a href, last has SHA256.
- <a class="download" href="/dl/go1.24.0.src.tar.gz">go1.24.0.src.tar.gz</a>
- <a class="download" href="/dl/go1.24.0.linux-amd64.tar.gz">go1.24.0.linux-amd64.tar.gz</a>
- <a class="download" href="/dl/go1.24.0.linux-arm64.tar.gz">go1.24.0.linux-arm64.tar.gz</a>
- <a class="download" href="/dl/go1.24.0.windows-amd64.zip">go1.24.0.windows-amd64.zip</a>
- <a class="download" href="/dl/go1.24.0.windows-amd64.msi">go1.24.0.windows-amd64.msi</a>
- */
- /*
- fn download_and_save(url: &str, filename: &str) -> Result<()> {
- let client = reqwest::blocking::Client::builder()
- .user_agent(APP_USER_AGENT)
- .build()?;
- print!("Downloading: {url} ");
- let _ = stdout().flush();
- let mut resp = client.get(url).send().context("Failed get")?;
- if resp.status().is_success() {
- let mut file =
- File::create(filename).with_context(|| format!("Creating file {filename} failed."))?;
- resp.copy_to(&mut file)?;
- } else {
- bail!("Status Code: {:?}", resp.status());
- }
- println!("OK");
- Ok(())
- }
- */
- // URL: https://go.dev/dl/go1.24.1.linux-amd64.tar.gz
- #[must_use]
- /// Get go version from download URL.
- fn version_from_url(url: &str, arch: &str) -> Option<String> {
- if let Some(parts) = url.split_once(arch) {
- if let Some(part) = parts.0.rsplit_once("/go") {
- let part = part.1.trim_matches('.');
- return Some(part.to_string());
- }
- }
- None
- }
- #[must_use]
- /// Get go version from `go version` output.
- fn version_from_go(text: &str) -> Option<String> {
- let parts : Vec<&str> = text.split(' ').collect();
- if parts.len() == 4 {
- return Some(parts[2].to_string().replace("go", ""))
- }
- None
- }
- /// 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;
- if !href.is_empty() {
- 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> {
- let reader = BufReader::new(fp);
- for line in reader.lines() {
- if let Ok(line) = line {
- if line.contains("a class=\"download\"") {
- if line.contains(arch) {
- // Return just the href part.
- return just_href(&line);
- }
- }
- }
- }
- bail!("Unable to locate architecture download link");
- }
- /// find_link for given arch (architecture)
- ///
- /// Look for <a class="download" href=""
- #[must_use]
- 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() {
- if let Ok(line) = line {
- if line.contains("a class=\"download\"") {
- if line.contains(arch) {
- // Return just the href part.
- return just_href(&line);
- }
- }
- }
- }
- bail!("Unable to locate architecture download link");
- }
- fn main() -> Result<()> {
- let cli = Cli::parse();
- // Get go environment
- let go_path = env::var("GOPATH").unwrap_or(String::new());
- let go_root = env::var("GOROOT").unwrap_or(String::new());
- let go_version: String;
- if let Ok(version) = find_go_version() {
- go_version = version.as_str().trim().to_string();
- } else {
- panic!("I wasn't able to locate go. I need `go version` to know what arch to dl.");
- }
- let version = version_from_go(&go_version).unwrap();
- // Since I have GO_PATH, I really don't need to do `where go`...
- // $GOROOT/bin/go
- let go_where: String;
- if let Ok(location) = find_go() {
- go_where = location.as_str().trim().to_string();
- } else {
- panic!("I wasn't able to locate the go binary.");
- }
- // Get arch (from `go version` output)
- let parts = go_version.split(" ");
- 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);
- println!("where: {}", go_where);
- println!("arch: {}", arch);
- */
- let cache = cache::Cache::new(cli.cache, None)?;
- // println!("Result: {:?}", get_go_downloads().await );
- // Get go version and path
- match &cli.command {
- Some(Commands::Update {}) => {
- let status = cache.fetch(GO_URL);
- // Check to see if file already exists AND
- // Check version against go's version.
- // Since the go.dev site doesn't allow caching or knowing when it changed...
- match status {
- cache::Status::Fetched(fp) => {
- let link = find_arch_link(&arch, &fp);
- if let Ok(relative) = link {
- let abs = relative_to_absolute(GO_URL, &relative).unwrap();
- println!("URL: {}", abs);
- let latest_version = version_from_url(&abs, &arch);
- if let Some(latest) = latest_version {
- println!("Version: {} [have {}]", latest, version);
- if version != latest {
- let latest_status = cache.fetch(&abs);
- println!("Latest: {:?}", latest_status);
- }
- } else {
- println!("Finding version failed for string: [{}]", abs);
- }
- }
- },
- cache::Status::Cached(fp) => {
- println!("(from cache)"); // I wish I could see this.
- let link = find_arch_link(&arch, &fp);
- if let Ok(relative) = link {
- let abs = relative_to_absolute(GO_URL, &relative).unwrap();
- println!("URL: {}", abs);
- }
- }
- _ => {
- println!("Status = {:?}", status);
- }
- }
- }
- Some(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"]);
- }
- }
- Ok(())
- }
|