Skip to content

Commit 1faf95c

Browse files
committed
cli: init with template
1 parent 47abb5d commit 1faf95c

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

devenv/src/cli.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ impl GlobalOptions {
182182
#[derive(Subcommand, Clone)]
183183
pub enum Commands {
184184
#[command(about = "Scaffold devenv.yaml, devenv.nix, .gitignore and .envrc.")]
185-
Init { target: Option<PathBuf> },
185+
Init {
186+
target: Option<PathBuf>,
187+
#[arg(long, help = "Use template")]
188+
template: Option<String>,
189+
},
186190

187191
#[command(about = "Generate devenv.yaml and devenv.nix using AI")]
188192
Generate {

devenv/src/cnix.rs

+9
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ impl Nix {
246246
Ok(())
247247
}
248248

249+
pub async fn init(&self, path: &Path, template: &str) -> Result<()> {
250+
let mut cmd =
251+
self.prepare_command("nix", &["flake", "init", "-t", template], &self.options)?;
252+
cmd.current_dir(path);
253+
self.run_nix_command(cmd, &self.options).await?;
254+
255+
Ok(())
256+
}
257+
249258
pub async fn metadata(&self) -> Result<String> {
250259
let options = Options {
251260
cache_output: true,

devenv/src/devenv.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl Devenv {
145145
self.devenv_dotfile.join("processes.pid")
146146
}
147147

148-
pub fn init(&self, target: &Option<PathBuf>) -> Result<()> {
148+
pub async fn init(&self, target: &Option<PathBuf>, template: &Option<String>) -> Result<()> {
149149
let target = target
150150
.clone()
151151
.unwrap_or_else(|| fs::canonicalize(".").expect("Failed to get current directory"));
@@ -155,6 +155,11 @@ impl Devenv {
155155
std::fs::create_dir_all(&target).expect("Failed to create target directory");
156156
}
157157

158+
if let Some(template) = template {
159+
self.nix.init(&target, template).await?;
160+
return Ok(());
161+
}
162+
158163
for filename in REQUIRED_FILES {
159164
info!("Creating {}", filename);
160165

devenv/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ async fn main() -> Result<()> {
133133
};
134134
Ok(())
135135
}
136-
Commands::Init { target } => devenv.init(&target),
136+
Commands::Init { target, template } => devenv.init(&target, &template).await,
137137
Commands::Generate { .. } => match which::which("devenv-generate") {
138138
Ok(devenv_generate) => {
139139
let error = Command::new(devenv_generate)

0 commit comments

Comments
 (0)