mod/direnv: add support of shell and args

This commit is contained in:
Dmitriy Pleshevskiy 2024-03-19 12:35:20 +03:00
parent 0a74fec499
commit 747b5c642b
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
4 changed files with 53 additions and 24 deletions

View file

@ -1,3 +1,6 @@
[direnv]
nix = "flake"
[gitignore]
direnv = true
rust = true

View file

@ -1,24 +1,34 @@
use crate::module::direnv::DirenvModuleArgs;
use crate::module::direnv::{DirenvModuleArgs, DirenvNixFunction};
#[derive(Debug, Clone, clap::ValueEnum)]
pub enum DirenvCliNixValue {
Flake,
Shell,
}
impl From<DirenvCliNixValue> for DirenvNixFunction {
fn from(value: DirenvCliNixValue) -> Self {
match value {
DirenvCliNixValue::Flake => DirenvNixFunction::Flake,
DirenvCliNixValue::Shell => DirenvNixFunction::Shell,
}
}
}
#[derive(Debug, Clone, clap::Args)]
pub struct DirenvModuleCliArgs {
#[clap(long)]
pub nix: Option<DirenvCliNixValue>,
#[clap(long)]
pub nix_args: Option<String>,
#[clap(long)]
pub nodejs: bool,
}
impl From<DirenvModuleCliArgs> for DirenvModuleArgs {
fn from(args: DirenvModuleCliArgs) -> Self {
DirenvModuleArgs {
nix_shell: matches!(args.nix, Some(DirenvCliNixValue::Shell)),
nix_flake: matches!(args.nix, Some(DirenvCliNixValue::Flake)),
nix: args.nix.map(From::from),
nix_args: args.nix_args,
nodejs: args.nodejs,
}
}

View file

@ -1,4 +1,4 @@
use crate::module::direnv::DirenvModuleArgs;
use crate::module::direnv::{DirenvModuleArgs, DirenvNixFunction};
#[derive(Default, serde::Deserialize, serde::Serialize)]
pub struct DirenvModuleConfig {
@ -9,14 +9,18 @@ pub struct DirenvModuleConfig {
impl From<DirenvModuleConfig> for DirenvModuleArgs {
fn from(cfg: DirenvModuleConfig) -> Self {
DirenvModuleArgs {
nix_shell: cfg
nix: cfg
.nix
.map(|v| v.to_lowercase() == "shell")
.unwrap_or_default(),
nix_flake: cfg
.nix
.map(|v| v.to_lowercase() == "flake")
.unwrap_or_default(),
.clone()
.and_then(|v| match v.split_whitespace().next() {
Some("flake") => Some(DirenvNixFunction::Flake),
Some("shell") => Some(DirenvNixFunction::Shell),
_ => None,
}),
nix_args: cfg.nix.and_then(|v| match v.split_once(" ") {
Some((_, args)) => Some(String::from(args)),
None => None,
}),
nodejs: cfg.nodejs.unwrap_or_default(),
}
}

View file

@ -2,29 +2,41 @@ use std::collections::HashMap;
use super::Module;
const NIX_SHELL_PART: &str = "\
# nix
use nix";
const NIX_FLAKE_PART: &str = "\
# nix
use flake";
const NODEJS_PART: &str = "\
# nodejs
layout node";
pub enum DirenvNixFunction {
Flake,
Shell,
}
impl std::fmt::Display for DirenvNixFunction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Flake => f.write_str("flake"),
Self::Shell => f.write_str("nix"),
}
}
}
pub struct DirenvModuleArgs {
pub nix_shell: bool,
pub nix_flake: bool,
pub nix: Option<DirenvNixFunction>,
pub nix_args: Option<String>,
pub nodejs: bool,
}
fn make_gitignore_content(args: DirenvModuleArgs) -> String {
[
args.nix_shell.then_some(NIX_SHELL_PART),
args.nix_flake.then_some(NIX_FLAKE_PART),
args.nodejs.then_some(NODEJS_PART),
args.nix.map(|function| {
format!(
"# nix\nuse {} {}",
function,
args.nix_args.unwrap_or_default()
)
.trim_end()
.to_owned()
}),
args.nodejs.then_some(NODEJS_PART.to_string()),
]
.iter()
.flatten()