|
@@ -3,10 +3,12 @@ use cache::relative_to_absolute;
|
|
|
use clap::{Parser, Subcommand};
|
|
|
use std::collections::HashMap;
|
|
|
// use std::env;
|
|
|
+use std::env;
|
|
|
use std::fs::{File, create_dir_all, read_dir, remove_dir_all, set_permissions};
|
|
|
use std::io::{BufRead, BufReader};
|
|
|
use std::path::PathBuf;
|
|
|
use std::process::Command;
|
|
|
+use std::time::Duration;
|
|
|
|
|
|
mod cache;
|
|
|
|
|
@@ -15,16 +17,17 @@ mod cache;
|
|
|
about = "Go Updater",
|
|
|
long_about = "Go Updater
|
|
|
|
|
|
-This checks the https://go.dev/dl for newer versions of go.
|
|
|
+This checks https://go.dev/dl for newer versions of go.
|
|
|
It depends upon go being in the path, and optionally GOPATH being set.
|
|
|
|
|
|
This can't update a package manager installed version of go (permissions)."
|
|
|
)]
|
|
|
struct Cli {
|
|
|
+ /*
|
|
|
/// Cache directory path
|
|
|
#[arg(short, long, default_value = "cache")]
|
|
|
cache: PathBuf,
|
|
|
-
|
|
|
+ */
|
|
|
#[command(subcommand)]
|
|
|
command: Option<Commands>,
|
|
|
}
|
|
@@ -257,6 +260,10 @@ fn recursive_remove_readonly(path: &str) -> Result<bool> {
|
|
|
fn main() -> Result<()> {
|
|
|
let cli = Cli::parse();
|
|
|
|
|
|
+ // Construct cache_dir from HOME/~
|
|
|
+ let home = env::var("HOME")?;
|
|
|
+ let cache_dir = format!("{home}/.cache/go-up");
|
|
|
+
|
|
|
/*
|
|
|
GOROOT, defaults to $HOME/go
|
|
|
This seems to be managed by go now. So running ```go env``` would
|
|
@@ -289,7 +296,11 @@ fn main() -> Result<()> {
|
|
|
let go_os_arch = format!("{go_os}-{go_arch}");
|
|
|
|
|
|
// Initialize the cache.
|
|
|
- let cache = cache::Cache::new(cli.cache, None)?;
|
|
|
+ let cache = cache::Cache::new(PathBuf::from(cache_dir), None)?;
|
|
|
+ let ex = cache.expire(Duration::from_secs(7 * 60 * 60 * 24))?;
|
|
|
+ if ex {
|
|
|
+ println!("Expired files from cache.");
|
|
|
+ }
|
|
|
|
|
|
match &cli.command {
|
|
|
Some(Commands::Update {}) => {
|
|
@@ -345,12 +356,12 @@ fn main() -> Result<()> {
|
|
|
let new_go_env = get_go_env().context("Calling `go env` with new go.")?;
|
|
|
let mut new_go_version = hashget(&new_go_env, "GOVERSION");
|
|
|
if new_go_version.starts_with("go") {
|
|
|
- new_go_version.remove(0); new_go_version.remove(0);
|
|
|
+ new_go_version.remove(0);
|
|
|
+ new_go_version.remove(0);
|
|
|
}
|
|
|
|
|
|
println!("Updated {} to {}", go_version, new_go_version);
|
|
|
|
|
|
-
|
|
|
/*
|
|
|
Clear GOROOT.
|
|
|
mkdir_all(GOROOT).
|