Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow running init on different filenames #266

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub enum Commands {
Init {
#[arg(long, default_value_t = false)]
dry_run: bool,
/// Output to a specific file
#[arg(long)]
file: Option<PathBuf>,
},
/// Start the TUI (default)
#[default]
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ async fn main() -> Result<()> {
let args = cli::Args::parse();

// Handle the `init` command immediately after parsing args
if let Some(cli::Commands::Init { dry_run }) = args.command {
if let Err(error) = onboarding::run(dry_run).await {
if let Some(cli::Commands::Init { dry_run, file }) = args.command {
if let Err(error) = onboarding::run(file, dry_run).await {
eprintln!("{error:#}");
std::process::exit(1);
}
Expand Down
19 changes: 13 additions & 6 deletions src/onboarding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
//!
//! Currently all values are inserted into a tera context, which is then rendered into the
//! `kwaak.toml` template.
//!
//! In the future it would be much nicer if it builds an actual `Config` struct. Then this can also
//! be used for
use std::path::PathBuf;

use crate::templates::Templates;
use anyhow::{Context, Result};
use commands::command_questions;
Expand All @@ -20,21 +25,23 @@ mod llm;
mod project;
mod util;

pub async fn run(dry_run: bool) -> Result<()> {
pub async fn run(file: Option<PathBuf>, dry_run: bool) -> Result<()> {
let file = file.unwrap_or_else(|| PathBuf::from("kwaak.toml"));
if !dry_run {
if std::fs::metadata(".git").is_err() {
anyhow::bail!("Not a git repository, please run `git init` first");
}
if std::fs::metadata("kwaak.toml").is_ok() {
if std::fs::metadata(&file).is_ok() {
anyhow::bail!(
"kwaak.toml already exists in current directory, skipping initialization"
"{} already exists in current directory, skipping initialization",
file.display()
);
}
}

println!("Welcome to Kwaak! Let's get started by initializing a new configuration file.");
println!("\n");
println!("We have a few questions to ask you to get started, you can always change these later in the `kwaak.toml` file.");
println!("We have a few questions to ask you to get started, you can always change these later in the `{}` file.", file.display());

let mut context = tera::Context::new();
project_questions(&mut context);
Expand All @@ -55,8 +62,8 @@ pub async fn run(dry_run: bool) -> Result<()> {
if dry_run {
println!("\nDry run, would have written the following to kwaak.toml:\n\n{config}");
} else {
std::fs::write("kwaak.toml", &config)?;
println!("\nInitialized kwaak project in current directory, please review and customize the created `kwaak.toml` file.\n Kwaak also needs a `Dockerfile` to execute your code in, with `ripgrep` and `fd` installed. Refer to https://github.com/bosun-ai/kwaak for an up to date list.");
std::fs::write(&file, &config)?;
println!("\nInitialized kwaak project in current directory, please review and customize the created `{}` file.\n Kwaak also needs a `Dockerfile` to execute your code in, with `ripgrep` and `fd` installed. Refer to https://github.com/bosun-ai/kwaak for an up to date list.", file.display());
}

Ok(())
Expand Down