Skip to content

Commit

Permalink
manplan-2 feature: could we allow for rules yaml to be driven by dot …
Browse files Browse the repository at this point in the history
…file?
  • Loading branch information
GJKrupa committed Oct 5, 2023
1 parent bb5b4ed commit 2da56ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "manplan"
version = "0.1.3"
version = "0.1.4"
authors = ["Gerard Krupa"]
edition = "2021"
description = "Tool for keeping sdkman candidates up-to-date"
Expand All @@ -17,6 +17,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
maplit = "1.0.2"
clap = { version = "4.4.6", features = ["derive"] }
expect-exit = "0.5.2"

[profile.release]
lto = "fat"
Expand Down
25 changes: 22 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::{env, fs};

use clap::Parser;
use expect_exit::Expected;

use crate::rules::{parse_rules, VersionMatch};
use crate::sdkman::{SdkMan, ToolManager};
Expand All @@ -14,7 +16,7 @@ pub mod sdkman;
struct Args {
/// Filename of the YAML config file
#[arg(short, long)]
file: String,
file: Option<String>,

/// Just print out the commands that would be executed
#[arg(short, long, default_value_t = false)]
Expand All @@ -25,14 +27,31 @@ struct Args {
no_uninstall: bool,
}

#[allow(clippy::needless_return)]
fn get_rules_file(args: &Args) -> Option<String> {
if let Some(file) = args.file.as_ref() {
return Some(file.clone());
} else if let Ok(home) = env::var("HOME") {
let dotfile = Path::new(home.as_str()).join(".sdk-rules.yaml");
if dotfile.is_file() {
return Some(dotfile.to_str().unwrap().to_string());
}
}
return None;
}

fn main() {
let args = Args::parse();

let sdkman = SdkMan {
dry_run: args.dry_run,
no_uninstall: args.no_uninstall,
};
let rules = parse_rules(fs::read_to_string(args.file).expect("Failed to read input file"));

let rules_file =
get_rules_file(&args).or_exit_("Must specify -f or have a ~/.sdk-rules.yaml file");
let rules = parse_rules(fs::read_to_string(rules_file).or_exit_("Failed to read rules file"));

for (name, candidate) in rules.expect("Rules file could not be parsed").candidates {
let installed: HashSet<_> = sdkman
.installed_versions(name.clone())
Expand Down

0 comments on commit 2da56ec

Please sign in to comment.