Browse Source

Use ~/.cache/go-up for cache directory.

I now expire files > 7 days old.
Steve Thielemann 6 days ago
parent
commit
b8170cb60a
2 changed files with 17 additions and 8 deletions
  1. 1 3
      src/cache.rs
  2. 16 5
      src/main.rs

+ 1 - 3
src/cache.rs

@@ -309,10 +309,8 @@ impl Cache {
                                     if let Err(e) = r {
                                         println!("RemoveFile {:?}: {}", filepath, e);
                                     }
-                                    // Also delete .content !
-                                    // Which is trickier to find now...
+                                    // Also delete content file.
                                     Self::remove_from_filename(&mut filepath);
-                                    // filepath.set_extension("content");
                                     let r = remove_file(&filepath);
                                     if let Err(e) = r {
                                         println!("RemoveFile {:?}: {}", filepath, e);

+ 16 - 5
src/main.rs

@@ -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).