浏览代码

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

I now expire files > 7 days old.
Steve Thielemann 5 月之前
父节点
当前提交
b8170cb60a
共有 2 个文件被更改,包括 17 次插入8 次删除
  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 {
                                     if let Err(e) = r {
                                         println!("RemoveFile {:?}: {}", filepath, e);
                                         println!("RemoveFile {:?}: {}", filepath, e);
                                     }
                                     }
-                                    // Also delete .content !
-                                    // Which is trickier to find now...
+                                    // Also delete content file.
                                     Self::remove_from_filename(&mut filepath);
                                     Self::remove_from_filename(&mut filepath);
-                                    // filepath.set_extension("content");
                                     let r = remove_file(&filepath);
                                     let r = remove_file(&filepath);
                                     if let Err(e) = r {
                                     if let Err(e) = r {
                                         println!("RemoveFile {:?}: {}", filepath, e);
                                         println!("RemoveFile {:?}: {}", filepath, e);

+ 16 - 5
src/main.rs

@@ -3,10 +3,12 @@ use cache::relative_to_absolute;
 use clap::{Parser, Subcommand};
 use clap::{Parser, Subcommand};
 use std::collections::HashMap;
 use std::collections::HashMap;
 // use std::env;
 // use std::env;
+use std::env;
 use std::fs::{File, create_dir_all, read_dir, remove_dir_all, set_permissions};
 use std::fs::{File, create_dir_all, read_dir, remove_dir_all, set_permissions};
 use std::io::{BufRead, BufReader};
 use std::io::{BufRead, BufReader};
 use std::path::PathBuf;
 use std::path::PathBuf;
 use std::process::Command;
 use std::process::Command;
+use std::time::Duration;
 
 
 mod cache;
 mod cache;
 
 
@@ -15,16 +17,17 @@ mod cache;
     about = "Go Updater",
     about = "Go Updater",
     long_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.
 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)."
 This can't update a package manager installed version of go (permissions)."
 )]
 )]
 struct Cli {
 struct Cli {
+    /*
     /// Cache directory path
     /// Cache directory path
     #[arg(short, long, default_value = "cache")]
     #[arg(short, long, default_value = "cache")]
     cache: PathBuf,
     cache: PathBuf,
-
+    */
     #[command(subcommand)]
     #[command(subcommand)]
     command: Option<Commands>,
     command: Option<Commands>,
 }
 }
@@ -257,6 +260,10 @@ fn recursive_remove_readonly(path: &str) -> Result<bool> {
 fn main() -> Result<()> {
 fn main() -> Result<()> {
     let cli = Cli::parse();
     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
     GOROOT, defaults to $HOME/go
     This seems to be managed by go now.  So running ```go env``` would
     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}");
     let go_os_arch = format!("{go_os}-{go_arch}");
 
 
     // Initialize the cache.
     // 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 {
     match &cli.command {
         Some(Commands::Update {}) => {
         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 new_go_env = get_go_env().context("Calling `go env` with new go.")?;
                         let mut new_go_version = hashget(&new_go_env, "GOVERSION");
                         let mut new_go_version = hashget(&new_go_env, "GOVERSION");
                         if new_go_version.starts_with("go") {
                         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);
                         println!("Updated {} to {}", go_version, new_go_version);
 
 
-
                         /*
                         /*
                         Clear GOROOT.
                         Clear GOROOT.
                         mkdir_all(GOROOT).
                         mkdir_all(GOROOT).