Skip to content

Commit 1329780

Browse files
committed
fix: make various config/db paths consistent across different parts of the application.
1 parent e383ffd commit 1329780

12 files changed

Lines changed: 66 additions & 62 deletions

File tree

‎crates/forge_app/src/service/api.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Live {
6262
let user_prompt = Arc::new(Service::user_prompt_service());
6363

6464
// Create an owned String that will live for 'static
65-
let sqlite = Arc::new(Service::db_pool_service(&env.db_path)?);
65+
let sqlite = Arc::new(Service::db_pool_service(&env.db_path())?);
6666

6767
let conversation_repo = Arc::new(Service::conversation_repo(sqlite.clone()));
6868

‎crates/forge_app/src/service/env.rs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,29 @@ impl Live {
5353

5454
Ok(Environment {
5555
os: std::env::consts::OS.to_string(),
56-
cwd: cwd.display().to_string(),
56+
cwd,
5757
shell: if cfg!(windows) {
5858
std::env::var("COMSPEC")?
5959
} else {
6060
std::env::var("SHELL").unwrap_or("/bin/sh".to_string())
6161
},
62-
home: dirs::home_dir().map(|a| a.display().to_string()),
6362
files,
6463
api_key,
6564
large_model_id,
6665
small_model_id,
67-
db_path: db_path().await?,
66+
base_path: base_path().await?,
67+
home: dirs::home_dir(),
6868
})
6969
}
7070
}
7171

72-
async fn db_path() -> Result<String> {
73-
let db_path = dirs::home_dir()
74-
.ok_or(anyhow::anyhow!("Unable to get home dir."))?
75-
.join(".forge");
72+
async fn base_path() -> Result<PathBuf> {
73+
let db_path = dirs::config_dir()
74+
.map(|a| a.join("forge"))
75+
.unwrap_or(PathBuf::from(".").join(".forge"));
76+
7677
tokio::fs::create_dir_all(&db_path).await?;
77-
Ok(db_path.display().to_string())
78+
Ok(db_path)
7879
}
7980

8081
#[async_trait::async_trait]

‎crates/forge_app/src/service/suggestion.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ pub trait SuggestionService: Send + Sync {
1818
}
1919

2020
struct Live {
21-
path: String,
21+
path: PathBuf,
2222
}
2323

2424
impl Live {
25-
pub fn new(path: impl Into<String>) -> Self {
25+
pub fn new(path: impl Into<PathBuf>) -> Self {
2626
Self { path: path.into() }
2727
}
2828
}
2929

3030
#[async_trait::async_trait]
3131
impl SuggestionService for Live {
3232
async fn suggestions(&self) -> Result<Vec<File>> {
33-
let cwd = PathBuf::from(self.path.clone()); // Use the current working directory
33+
let cwd = self.path.clone(); // Use the current working directory
3434
let walker = Walker::max_all().cwd(cwd);
3535

3636
let files = walker.get().await?;
@@ -42,7 +42,7 @@ impl SuggestionService for Live {
4242
}
4343

4444
impl Service {
45-
pub fn completion_service(path: impl Into<String>) -> impl SuggestionService {
45+
pub fn completion_service(path: impl Into<PathBuf>) -> impl SuggestionService {
4646
Live::new(path)
4747
}
4848
}

‎crates/forge_app/src/service/system_prompt.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ impl PromptService for Live {
8383
#[cfg(test)]
8484
mod tests {
8585

86+
use std::path::PathBuf;
87+
8688
use forge_domain::{ModelId, Parameters};
8789
use insta::assert_snapshot;
8890

@@ -92,14 +94,14 @@ mod tests {
9294
fn test_env() -> Environment {
9395
Environment {
9496
os: "linux".to_string(),
95-
cwd: "/home/user/project".to_string(),
97+
cwd: PathBuf::from("/home/user/project"),
9698
shell: "/bin/bash".to_string(),
97-
home: Some("/home/user".to_string()),
9899
files: vec!["file1.txt".to_string(), "file2.txt".to_string()],
99100
api_key: "test".to_string(),
100101
large_model_id: "open-ai/gpt-4o".to_string(),
101102
small_model_id: "open-ai/gpt-4o-mini".to_string(),
102-
db_path: "/home/user/.forge/globalConfig".to_string(),
103+
base_path: PathBuf::from("/home/user/.forge/globalConfig"),
104+
home: Some(PathBuf::from("/home/user")),
103105
}
104106
}
105107

‎crates/forge_app/src/sqlite/driver.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use anyhow::{Context, Result};
24
use diesel::prelude::*;
35
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
@@ -20,8 +22,9 @@ pub(crate) struct Driver {
2022
}
2123

2224
impl Driver {
23-
pub fn new(db_path: &str) -> Result<Self> {
24-
let db_path = format!("{}/{}", db_path, DB_NAME);
25+
pub fn new(db_path: &PathBuf) -> Result<Self> {
26+
let db_path = db_path.join(DB_NAME);
27+
let db_path = db_path.to_string_lossy();
2528

2629
// Run migrations first
2730
let mut conn = SqliteConnection::establish(&db_path)
@@ -79,7 +82,7 @@ pub(crate) mod tests {
7982
impl TestDriver {
8083
pub fn new() -> Result<Self> {
8184
let temp_dir = TempDir::new().unwrap();
82-
let db_path = temp_dir.path().to_str().unwrap().to_string();
85+
let db_path = temp_dir.path().to_path_buf();
8386

8487
Ok(Self { driver: Driver::new(&db_path)?, _temp_dir: temp_dir })
8588
}

‎crates/forge_app/src/sqlite/mod.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod conn;
22
pub(crate) mod driver;
33

4+
use std::path::PathBuf;
5+
46
use anyhow::Result;
57
use diesel::r2d2::{ConnectionManager, PooledConnection};
68
use diesel::sqlite::SqliteConnection;
@@ -21,7 +23,7 @@ pub trait Sqlite: Send + Sync {
2123

2224
impl Service {
2325
/// Create a new SQLite service
24-
pub fn db_pool_service(db_path: &str) -> Result<impl Sqlite + 'static> {
26+
pub fn db_pool_service(db_path: &PathBuf) -> Result<impl Sqlite + 'static> {
2527
Driver::new(db_path)
2628
}
2729
}

‎crates/forge_domain/src/environment.rs‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use async_trait::async_trait;
24
use derive_setters::Setters;
35
use serde::Serialize;
@@ -10,11 +12,11 @@ pub struct Environment {
1012
/// The operating system of the environment.
1113
pub os: String,
1214
/// The current working directory.
13-
pub cwd: String,
15+
pub cwd: PathBuf,
16+
/// The home directory.
17+
pub home: Option<PathBuf>,
1418
/// The shell being used.
1519
pub shell: String,
16-
/// The home directory, if available.
17-
pub home: Option<String>,
1820
/// A list of files in the current working directory.
1921
pub files: Vec<String>,
2022
/// The Forge API key.
@@ -23,8 +25,23 @@ pub struct Environment {
2325
pub large_model_id: String,
2426
/// The small model ID.
2527
pub small_model_id: String,
26-
/// Config dir for Forge.
27-
pub db_path: String,
28+
29+
/// The base path relative to which everything else stored.
30+
pub base_path: PathBuf,
31+
}
32+
33+
impl Environment {
34+
pub fn db_path(&self) -> PathBuf {
35+
self.base_path.clone()
36+
}
37+
38+
pub fn log_path(&self) -> PathBuf {
39+
self.base_path.join("logs")
40+
}
41+
42+
pub fn history_path(&self) -> PathBuf {
43+
self.base_path.clone()
44+
}
2845
}
2946

3047
/// Repository for accessing system environment information

‎crates/forge_main/src/editor.rs‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::path::PathBuf;
2-
1+
use forge_domain::Environment;
32
use nu_ansi_term::{Color, Style};
43
use reedline::{
54
default_emacs_keybindings, ColumnarMenu, DefaultHinter, EditCommand, Emacs, FileBackedHistory,
@@ -69,15 +68,9 @@ impl ForgeEditor {
6968
keybindings
7069
}
7170

72-
pub fn start(cwd: PathBuf) -> Self {
71+
pub fn start(env: Environment) -> Self {
7372
// Store file history in system config directory
74-
let history_file = dirs::config_dir()
75-
.map(|mut path| {
76-
path.push("forge");
77-
path.push(".forge_history");
78-
path
79-
})
80-
.unwrap_or_else(|| PathBuf::from(".forge_history"));
73+
let history_file = env.history_path();
8174

8275
let history = Box::new(
8376
FileBackedHistory::with_file(HISTORY_CAPACITY, history_file).unwrap_or_default(),
@@ -101,7 +94,7 @@ impl ForgeEditor {
10194
let edit_mode = Box::new(Emacs::new(Self::init()));
10295

10396
let editor = Reedline::create()
104-
.with_completer(Box::new(FileCompleter::new(cwd)))
97+
.with_completer(Box::new(FileCompleter::new(env.cwd)))
10598
.with_history(history)
10699
.with_hinter(Box::new(
107100
DefaultHinter::default().with_style(Style::new().fg(Color::DarkGray)),

‎crates/forge_main/src/info.rs‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,9 @@ pub fn display_info(env: &Environment, usage: &Usage) -> io::Result<()> {
1111
CONSOLE.writeln(format!(
1212
"{} {}",
1313
"Working Directory:".bold().bright_yellow(),
14-
env.cwd
14+
env.cwd.display()
1515
))?;
1616
CONSOLE.writeln(format!("{} {}", "Shell:".bold().bright_yellow(), env.shell))?;
17-
if let Some(home) = &env.home {
18-
CONSOLE.writeln(format!(
19-
"{} {}",
20-
"Home Directory:".bold().bright_yellow(),
21-
home
22-
))?;
23-
}
2417
CONSOLE.writeln(format!(
2518
"{} {}",
2619
"File Count:".bold().bright_yellow(),

‎crates/forge_main/src/input.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22

33
use async_trait::async_trait;
4-
use forge_domain::{Command, Usage, UserInput};
4+
use forge_domain::{Command, Environment, Usage, UserInput};
55
use tokio::fs;
66

77
use crate::console::CONSOLE;
@@ -12,13 +12,13 @@ use crate::status::StatusDisplay;
1212
/// Console implementation for handling user input via command line.
1313
#[derive(Debug, Default)]
1414
pub struct Console {
15-
cwd: PathBuf,
15+
env: Environment,
1616
}
1717

1818
impl Console {
1919
/// Creates a new instance of `Console`.
20-
pub fn new(cwd: PathBuf) -> Self {
21-
Self { cwd }
20+
pub fn new(env: Environment) -> Self {
21+
Self { env }
2222
}
2323
}
2424

@@ -35,7 +35,7 @@ impl UserInput for Console {
3535

3636
async fn prompt(&self, input: Option<Self::PromptInput>) -> anyhow::Result<Command> {
3737
CONSOLE.writeln("")?;
38-
let mut engine = ForgeEditor::start(self.cwd.clone());
38+
let mut engine = ForgeEditor::start(self.env.clone());
3939
let prompt: ForgePrompt = input.map(Into::into).unwrap_or_default();
4040

4141
loop {

0 commit comments

Comments
 (0)