Skip to content

Commit 907ced9

Browse files
Merge pull request #21 from anna-singleton/dev (v0.3.0)
merge dev into main for release v0.3.0
2 parents fdd13b0 + 9f8d779 commit 907ced9

File tree

7 files changed

+169
-84
lines changed

7 files changed

+169
-84
lines changed

.github/workflows/build-dev.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: rust unstable build & release (linux)
2+
3+
on:
4+
push:
5+
branches: [ "dev" ]
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
10+
jobs:
11+
build:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: build executable
18+
run: cargo build --verbose --release
19+
20+
- name: upload & add to releases
21+
uses: "marvinpinto/action-automatic-releases@latest"
22+
with:
23+
repo_token: "${{ secrets.GITHUB_TOKEN }}"
24+
automatic_release_tag: "dev-latest"
25+
prerelease: true
26+
title: "Latest Linux Build (UNSTABLE)"
27+
files: |
28+
target/release/runfast
29+

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "runfast"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
edition = "2021"
55
authors = ["Anna Singleton<[email protected]>"]
66
repository = "https://github.com/anna-singleton/runfast"

src/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ pub(crate) struct Cli {
88
pub(crate) force_choose_new: bool,
99
#[arg(short, long="runners-path", help="Load specific toml config")]
1010
pub(crate) runners_path: Option<String>,
11+
#[arg(short='c', long="clean-cache", help="Remove cached directories that no longer exist")]
12+
pub(crate) clean_cache: bool,
13+
#[arg(long="reset-cache", help="Remove ALL directories in the cache")]
14+
pub(crate) reset_cache: bool,
1115
}

src/main.rs

Lines changed: 29 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,15 @@
11
extern crate skim;
2-
use std::path::PathBuf;
3-
use std::collections::HashMap;
42

53
use clap::Parser;
6-
use directories::BaseDirs;
74
use skim::prelude::*;
8-
use serde::{Serialize, Deserialize};
95

106
mod cli;
117
mod runner;
8+
mod runnercache;
129

1310
use cli::Cli;
1411
use runner::Runner;
15-
16-
#[derive(Serialize, Deserialize, Debug)]
17-
struct RunnerCache {
18-
runners: HashMap<PathBuf, Runner>,
19-
}
20-
21-
impl RunnerCache {
22-
/// Returns the cache if its a valid cache, and the executing user has
23-
/// access to the cache
24-
fn load() -> Option<RunnerCache> {
25-
let cache_path = BaseDirs::new()
26-
.unwrap()
27-
.cache_dir()
28-
.join("runfast-cache.toml");
29-
30-
if !cache_path.exists() {
31-
return Some(RunnerCache { runners: HashMap::new() })
32-
}
33-
34-
let cache_string = std::fs::read_to_string(cache_path).unwrap();
35-
36-
match toml::from_str::<RunnerCache>(&cache_string) {
37-
Ok(cache) => Some(cache),
38-
Err(e) => {
39-
println!("Could Not Parse Cache with Error: {}\n\
40-
Continuing without cache use.", e);
41-
None
42-
// return none to signify intentionally not generating a blank
43-
// cache, otherwise we may overwrite an existing one that has
44-
// parse errors
45-
},
46-
}
47-
}
48-
49-
/// Returns a Some(Runner) if the path exists in the cache, or None if it
50-
/// does not
51-
fn try_get_runner(&self) -> Option<Runner> {
52-
let cdir = std::env::current_dir().unwrap();
53-
self.runners.get(&cdir).map(|rnr| rnr.to_owned())
54-
}
55-
56-
/// Adds a runner to the cache, serialises it, then writes it to disk.
57-
///
58-
/// In the case the current filepath is already in the cache, overwrite it
59-
/// with the new value of the runner
60-
///
61-
/// # Arguments:
62-
///
63-
/// * [runner](Runner) - A borrowed runner to be added to the cache.
64-
///
65-
fn add_runner(&mut self, runner: &Runner) {
66-
let current_path = std::env::current_dir().unwrap();
67-
if self.runners.contains_key(&current_path) {
68-
self.runners.remove(&current_path);
69-
}
70-
self.runners.insert(current_path, runner.clone());
71-
72-
let new_cache = match toml::to_string(&self) {
73-
Ok(nc) => nc,
74-
Err(e) => {
75-
eprintln!("Could not serialise new cache data to toml, error: {}", e);
76-
return;
77-
},
78-
};
79-
80-
let cache_path = BaseDirs::new()
81-
.unwrap()
82-
.cache_dir()
83-
.join("runfast-cache.toml");
84-
85-
match std::fs::write(cache_path, new_cache) {
86-
Ok(_) => (),
87-
Err(e) => eprintln!("Could not write toml to disk, error: {}", e),
88-
};
89-
}
90-
}
12+
use runnercache::RunnerCache;
9113

9214
fn select_new_runner(runners_path: Option<String>) -> Option<Runner> {
9315
let runners = runner::load_runners(&runners_path);
@@ -136,11 +58,38 @@ fn select_new_runner(runners_path: Option<String>) -> Option<Runner> {
13658
chosen_runner
13759
}
13860

61+
13962
fn main() {
14063
let cli = Cli::parse();
14164

14265
let mut cache = RunnerCache::load();
14366

67+
// there is probably a way to do this with Clap, might need to switch
68+
// to builders
69+
if cli.clean_cache && cli.reset_cache {
70+
eprintln!("You cannot clean and reset the cache at the same time!");
71+
return
72+
}
73+
74+
if cli.clean_cache {
75+
match cache {
76+
Some(mut cache) => {
77+
match cache.clean_cache() {
78+
Ok(x) => println!("Cache Cleaned, Removed {} Entries.", x),
79+
Err(e) => eprintln!("Couldn't clean cache, {}", e),
80+
}
81+
},
82+
None => eprintln!("Cache is corrupted, cannot clean it."),
83+
};
84+
return;
85+
} else if cli.reset_cache {
86+
match RunnerCache::reset_cache() {
87+
Ok(_) => println!("Cache emptied."),
88+
Err(e) => eprintln!("Could not empty cache. Error: {}", e),
89+
}
90+
return;
91+
}
92+
14493
let chosen = if cli.force_choose_new {
14594
let runner = select_new_runner(cli.runners_path);
14695
match cache {

src/runner.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,8 @@ pub fn load_runners(path: &Option<String>) -> Vec<Runner> {
170170
None => confdir.join("runfast/runners.toml"),
171171
};
172172

173-
println!("path: {:?}", userconf_path);
174173
let mut user_configs: Option<Config> = None;
175174
if userconf_path.exists() {
176-
println!("userconf exists!");
177175
let user_confstring = read_to_string(userconf_path).unwrap();
178176
match toml::from_str::<Config>(&user_confstring) {
179177
Ok(conf) => user_configs = Some(conf),

src/runnercache.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use std::path::PathBuf;
2+
use std::collections::HashMap;
3+
use directories::BaseDirs;
4+
use serde::{Serialize, Deserialize};
5+
6+
use crate::runner::Runner;
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
pub struct RunnerCache {
10+
runners: HashMap<PathBuf, Runner>,
11+
}
12+
13+
impl RunnerCache {
14+
/// Returns the cache if its a valid cache, and the executing user has
15+
/// access to the cache
16+
pub fn load() -> Option<RunnerCache> {
17+
let cache_path = BaseDirs::new()
18+
.unwrap()
19+
.cache_dir()
20+
.join("runfast-cache.toml");
21+
22+
if !cache_path.exists() {
23+
return Some(RunnerCache { runners: HashMap::new() })
24+
}
25+
26+
let cache_string = std::fs::read_to_string(cache_path).unwrap();
27+
28+
match toml::from_str::<RunnerCache>(&cache_string) {
29+
Ok(cache) => Some(cache),
30+
Err(e) => {
31+
println!("Could Not Parse Cache with Error: {}\n\
32+
Continuing without cache use.", e);
33+
None
34+
// return none to signify intentionally not generating a blank
35+
// cache, otherwise we may overwrite an existing one that has
36+
// parse errors
37+
},
38+
}
39+
}
40+
41+
/// Returns a Some(Runner) if the path exists in the cache, or None if it
42+
/// does not
43+
pub fn try_get_runner(&self) -> Option<Runner> {
44+
let cdir = std::env::current_dir().unwrap();
45+
self.runners.get(&cdir).map(|rnr| rnr.to_owned())
46+
}
47+
48+
/// Adds a runner to the cache, serialises it, then writes it to disk.
49+
///
50+
/// In the case the current filepath is already in the cache, overwrite it
51+
/// with the new value of the runner
52+
///
53+
/// # Arguments:
54+
///
55+
/// * [runner](Runner) - A borrowed runner to be added to the cache.
56+
///
57+
pub fn add_runner(&mut self, runner: &Runner) {
58+
let current_path = std::env::current_dir().unwrap();
59+
if self.runners.contains_key(&current_path) {
60+
self.runners.remove(&current_path);
61+
}
62+
self.runners.insert(current_path, runner.clone());
63+
64+
65+
self.write_cache();
66+
}
67+
68+
fn write_cache(&self) -> Result<(),String> {
69+
let new_cache = match toml::to_string(&self) {
70+
Ok(nc) => nc,
71+
Err(e) => {
72+
eprintln!("Could not serialise new cache data to toml, error: {}", e);
73+
e.to_string()
74+
},
75+
};
76+
77+
let cache_path = BaseDirs::new()
78+
.unwrap()
79+
.cache_dir()
80+
.join("runfast-cache.toml");
81+
82+
match std::fs::write(cache_path, new_cache) {
83+
Ok(_) => Ok(()),
84+
Err(e) => {
85+
eprintln!("Could not write toml to disk, error: {}", e);
86+
Err(e.to_string())
87+
},
88+
}
89+
}
90+
91+
pub fn clean_cache(&mut self) -> Result<u32, String> {
92+
let before = self.runners.len();
93+
self.runners.retain(|path, _| path.exists());
94+
let after = self.runners.len();
95+
96+
self.write_cache()?;
97+
98+
Ok((before - after) as u32)
99+
}
100+
101+
pub fn reset_cache() -> Result<(), String> {
102+
let empty = RunnerCache{ runners: HashMap::new() };
103+
empty.write_cache()
104+
}
105+
}

0 commit comments

Comments
 (0)