Skip to content

Commit f30b362

Browse files
committed
Basic .csmrc parsing (toml)
1 parent 5a67b8f commit f30b362

6 files changed

Lines changed: 125 additions & 5 deletions

File tree

Cargo.lock

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ edition = "2024"
77
clap = { version = "4.5.50", features = ["derive", "wrap_help"] }
88
env_logger = "0.11.8"
99
log = "0.4.28"
10+
serde = { version = "1.0.228", features = ["derive"] }
11+
toml = "0.9.8"

src/csmrc.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// Module for reading a user's ~/.csmrc, if it exists.
2+
use serde::Deserialize;
3+
use std::default::Default;
4+
use std::io::{Error, ErrorKind};
5+
use std::path::PathBuf;
6+
7+
#[derive(Debug, Default, Deserialize)]
8+
pub struct Config {
9+
/// Override the $MAMBA_ROOT_PREFIX when cshelling out to micromamba.
10+
#[serde(default)]
11+
#[allow(dead_code)]
12+
pub mamba_root_prefix: Option<String>,
13+
}
14+
15+
impl Config {
16+
/// Read the user's ~/.csmrc if it exists, merging with the Default instance for
17+
/// Config.
18+
pub fn from_csmrc() -> Result<Self, std::io::Error> {
19+
let home = match std::env::var("HOME") {
20+
Ok(home) => home,
21+
Err(_) => match std::env::var("USERPROFILE") {
22+
Ok(home) => home,
23+
Err(_) => return Ok(Config::default()),
24+
},
25+
};
26+
27+
let csmrc_path = PathBuf::from(home).join(".csmrc");
28+
let csmrc_data = std::fs::read_to_string(csmrc_path)?;
29+
toml::from_str(&csmrc_data).map_err(|e| Error::new(ErrorKind::InvalidData, e))
30+
}
31+
}

src/env.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::csmrc::Config;
2+
13
#[derive(Debug, clap::Subcommand)]
24
pub enum Subcommand {
35
/// Create an environment
@@ -27,6 +29,7 @@ pub struct CreateArgs {
2729
name: Option<String>,
2830
}
2931

30-
pub fn run(subcommand: Subcommand) {
32+
pub fn run(config: Config, subcommand: Subcommand) {
33+
println!("{:?}", config);
3134
println!("{:?}", subcommand);
3235
}

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod csmrc;
12
mod env;
23
mod robot;
34

@@ -29,7 +30,7 @@ enum Command {
2930
Robot(robot::Subcommand),
3031
}
3132

32-
fn main() {
33+
fn main() -> Result<(), std::io::Error> {
3334
let cli = Cli::parse();
3435

3536
// Set up logging
@@ -44,9 +45,10 @@ fn main() {
4445
env_logger_builder.init();
4546

4647
let _ = create_mambarc();
48+
let config = csmrc::Config::from_csmrc()?;
4749
match cli.command {
48-
Command::Env(sub) => env::run(sub),
49-
Command::Robot(sub) => robot::run(sub),
50+
Command::Env(sub) => Ok(env::run(config, sub)),
51+
Command::Robot(sub) => Ok(robot::run(config, sub)),
5052
}
5153
}
5254

src/robot.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::csmrc::Config;
2+
13
#[derive(Debug, clap::Subcommand)]
24
pub enum Subcommand {
35
/// Create a Robotmk robot
@@ -13,6 +15,7 @@ pub struct CreateArgs {
1315
path: String,
1416
}
1517

16-
pub fn run(subcommand: Subcommand) {
18+
pub fn run(config: Config, subcommand: Subcommand) {
19+
println!("{:?}", config);
1720
println!("{:?}", subcommand);
1821
}

0 commit comments

Comments
 (0)