123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- use anyhow::{bail, Result};
- use clap::{Parser, Subcommand};
- use std::env;
- use std::fs::File;
- use std::io::{BufRead, BufReader, Write};
- use std::process::Command;
- pub mod built_info {
- include!(concat!(env!("OUT_DIR"), "/built.rs"));
- }
- fn reqwests_version() -> Option<String> {
- for (name, version) in built_info::DEPENDENCIES {
- if name == "reqwest" {
- return Some(version.to_string());
- }
- }
- None
- }
- #[derive(Parser)]
- #[command(about = "Go updater")]
- struct Cli {
- #[command(subcommand)]
- command: Option<Commands>,
- }
- #[derive(Subcommand)]
- enum Commands {
-
- Update {},
- Info {},
- }
- fn find_go_version() -> Result<String> {
- let output = Command::new("go").arg("version").output()?;
- if output.status.success() {
-
- return Ok(String::from_utf8_lossy(output.stdout.as_slice()).to_string());
- }
- bail!("Failed to query go version.");
- }
- 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";
- static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
- fn get_go_downloads() -> Result<()> {
-
-
- let client = reqwest::blocking::Client::builder()
- .user_agent(APP_USER_AGENT)
- .build()?;
- print!("Downloading: {GO_URL} ");
- let mut resp = client.get(GO_URL).send()?;
- if resp.status().is_success() {
-
-
-
- let mut file = File::create(GO_FILE)?;
- resp.copy_to(&mut file)?;
-
- } else {
- panic!("Failed: {:?}", resp.status());
- }
- println!("OK");
- Ok(())
- }
- use std::error::Error;
- 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 Ok(line);
- }
- }
- }
- }
- Err(Box::from("Unable to locate architecture download link"))
- }
- fn main() -> Result<()> {
- let cli = Cli::parse();
-
- 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 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.");
- }
-
- let parts = go_version.split(" ");
- let mut arch = parts.last().unwrap().to_string();
- arch = arch.replace("/", "-");
-
- println!("Reqwest version: {:?}", reqwests_version());
- println!("path {} / root {}", go_path, go_root);
- println!("version: {}", go_version);
- println!("where: {}", go_where);
- println!("arch: {}", arch);
-
-
- match &cli.command {
- Some(Commands::Update {}) => {
- get_go_downloads()?;
- let link = find_link(&arch);
- println!("{:?}", link);
- }
- Some(Commands::Info {}) => {}
- None => {
-
- let _show_help: Cli = Cli::parse_from(["--help"]);
- }
- }
- Ok(())
- }
|