Skip to content

Commit 9784d84

Browse files
committed
Make config subcommand local-aware
1 parent 9558e9a commit 9784d84

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ install:
2222
cargo install --path .
2323

2424
e2e: install
25-
sh -eux -c 'for i in ./e2e/test_*.sh ; do sh -x $i ; done'
25+
sh -eux -c 'for i in ./e2e/test_*.sh ; do sh -x "$i" ; done'
2626

2727
test *args: e2e
2828
cargo test

e2e/test_config_local_list_get_set.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
gptcommit config list
5+
# assert is valid TOML
6+
7+
gptcommit config get openai.model
8+
# assert default = text-davinci-003
9+
gptcommit config set --local openai.model foo
10+
gptcommit config set openai.model bar
11+
gptcommit config get openai.model
12+
# assert is foo
13+
14+
gptcommit config delete openai.model
15+
gptcommit config get openai.model
16+
# assert still is foo
17+
gptcommit config delete --local openai.model
18+
gptcommit config get openai.model
19+
# assert is default

src/actions/config.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::{collections::VecDeque, fs};
1+
use std::{collections::VecDeque, fs, path::PathBuf};
22

33
use clap::{Args, Subcommand};
44
use toml::Value;
55

6-
use crate::settings::{get_user_config_path, Settings};
6+
use crate::settings::{get_local_config_path, get_user_config_path, Settings};
77
use anyhow::{bail, Result};
88

99
#[derive(Subcommand, Debug)]
@@ -17,9 +17,20 @@ pub(crate) enum ConfigAction {
1717
/// Read config value
1818
Get { key: String },
1919
/// Set config value
20-
Set { key: String, value: String },
20+
Set {
21+
key: String,
22+
value: String,
23+
/// if set, modifies the local config. Default behavior modifies global config
24+
#[clap(long)]
25+
local: bool,
26+
},
2127
/// Clear config value
22-
Delete { key: String },
28+
Delete {
29+
key: String,
30+
/// if set, modifies the local config. Default behavior modifies global config
31+
#[clap(long)]
32+
local: bool,
33+
},
2334
}
2435

2536
#[derive(Args, Debug)]
@@ -34,28 +45,36 @@ pub(crate) async fn main(settings: Settings, args: ConfigArgs) -> Result<()> {
3445
match args.action {
3546
ConfigAction::List { save } => list(settings, save).await,
3647
ConfigAction::Get { key } => get(settings, key).await,
37-
ConfigAction::Set { key, value } => set(settings, key, value).await,
38-
ConfigAction::Delete { key } => delete(settings, key).await,
48+
ConfigAction::Set { key, value, local } => set(settings, key, value, local).await,
49+
ConfigAction::Delete { key, local } => delete(settings, key, local).await,
3950
}
4051
}
4152

42-
async fn delete(_settings: Settings, full_key: String) -> Result<()> {
53+
async fn delete(_settings: Settings, full_key: String, local: bool) -> Result<()> {
4354
let settings = &Settings::from_clear(&full_key)?;
4455
let toml_string = toml::to_string_pretty(settings).unwrap();
45-
let user_config_path = get_user_config_path().expect("Could not find user config path");
46-
fs::write(&user_config_path, toml_string)?;
56+
let config_path: PathBuf = if local {
57+
get_local_config_path().expect("Could not find repo-local config path")
58+
} else {
59+
get_user_config_path().expect("Could not find user config path")
60+
};
61+
fs::write(&config_path, toml_string)?;
4762
println!("Cleared {full_key}");
48-
println!("Config saved to {}", user_config_path.display());
63+
println!("Config saved to {}", config_path.display());
4964
Ok(())
5065
}
5166

52-
async fn set(_settings: Settings, full_key: String, value: String) -> Result<()> {
67+
async fn set(_settings: Settings, full_key: String, value: String, local: bool) -> Result<()> {
5368
let settings = &Settings::from_set_override(&full_key, &value)?;
5469
let toml_string = toml::to_string_pretty(settings).unwrap();
55-
let user_config_path = get_user_config_path().expect("Could not find user config path");
56-
fs::write(&user_config_path, toml_string)?;
70+
let config_path: PathBuf = if local {
71+
get_local_config_path().expect("Could not find repo-local config path")
72+
} else {
73+
get_user_config_path().expect("Could not find user config path")
74+
};
75+
fs::write(&config_path, toml_string)?;
5776
println!("{full_key} = {value}");
58-
println!("Config saved to {}", user_config_path.display());
77+
println!("Config saved to {}", config_path.display());
5978
Ok(())
6079
}
6180

src/settings.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ impl Settings {
192192
}
193193
}
194194

195+
pub fn get_local_config_path() -> Option<PathBuf> {
196+
if let Ok(config_dir) = get_hooks_path() {
197+
if !config_dir.is_dir() {
198+
fs::create_dir_all(&config_dir).ok()?;
199+
}
200+
let config_path = config_dir.join("../gptcommit.toml");
201+
if !config_path.exists() {
202+
fs::write(&config_path, "").ok()?;
203+
}
204+
return Some(config_path);
205+
}
206+
207+
None
208+
}
209+
195210
pub fn get_user_config_path() -> Option<PathBuf> {
196211
if let Some(home_dir) = dirs::home_dir() {
197212
let config_dir = home_dir.join(".config").join(APP_NAME);

0 commit comments

Comments
 (0)