Skip to content

Commit feabe4b

Browse files
committed
Allow passing a custom dir to init
1 parent 28adfac commit feabe4b

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/commands/init.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
use anyhow::{anyhow, Result};
2-
use std::{collections::HashMap, env::current_dir, fs};
2+
use std::{collections::HashMap, fs, path::PathBuf};
33

44
use crate::{
55
manifest::{self, Manifest},
66
modrinth::{Client, VersionType},
77
};
88

9-
pub async fn init(client: &Client, version: Option<String>, name: Option<String>) -> Result<()> {
10-
let current_dir = current_dir().expect("Failed to get current directory");
11-
9+
pub async fn init(
10+
client: &Client,
11+
path: PathBuf,
12+
version: Option<String>,
13+
name: Option<String>,
14+
) -> Result<()> {
1215
let name = if let Some(name) = name {
1316
name
1417
} else {
1518
// TODO: some degree of error handling I guess
16-
current_dir
17-
.file_name()
19+
path.file_name()
1820
.and_then(|name| name.to_str())
1921
.unwrap_or("pack")
2022
.to_string()
@@ -52,7 +54,9 @@ pub async fn init(client: &Client, version: Option<String>, name: Option<String>
5254

5355
fs::write("podzol.toml", &toml_edit::ser::to_string_pretty(&manifest)?)?;
5456

55-
git2::Repository::init(current_dir)?;
57+
if !fs::exists(path.join(".git"))? {
58+
git2::Repository::init(path)?;
59+
}
5660

5761
Ok(())
5862
}

src/main.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt::Display, str::FromStr};
1+
use std::{env::current_dir, fmt::Display, path::PathBuf, str::FromStr};
22

33
use anyhow::Result;
44
use clap::{Parser, Subcommand};
@@ -23,7 +23,9 @@ struct Args {
2323
enum Commands {
2424
/// Create a new podzol project in the specified directory
2525
Init {
26-
/// The name of this modpack (default to the directory name)
26+
/// Path to the directory to init (defaults to current directory)
27+
path: Option<PathBuf>,
28+
/// The name of this modpack (defaults to the directory name)
2729
#[arg(short, long)]
2830
name: Option<String>,
2931
/// The minecraft version (defaults to latest)
@@ -104,8 +106,19 @@ async fn main() -> Result<()> {
104106
Commands::Export => {
105107
commands::export(&client).await?;
106108
}
107-
Commands::Init { version, name, .. } => {
108-
init(&client, version, name).await?;
109+
Commands::Init {
110+
path,
111+
version,
112+
name,
113+
..
114+
} => {
115+
init(
116+
&client,
117+
path.unwrap_or_else(|| current_dir().expect("Failed to fetch current dir")),
118+
version,
119+
name,
120+
)
121+
.await?;
109122
}
110123
Commands::Remove => todo!(),
111124
}

0 commit comments

Comments
 (0)