Skip to content
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
25 changes: 25 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/rust
{
"name": "Rust",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/rust:0-1-bullseye",
// Use 'mounts' to make the cargo cache persistent in a Docker Volume.
// "mounts": [
// {
// "source": "devcontainer-cargo-cache-${devcontainerId}",
// "target": "/usr/local/cargo",
// "type": "volume"
// }
// ]
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "cargo install just",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
25 changes: 13 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/main.rs"

[dependencies]
anyhow = "1.0.69"
async-std = "1.12.0"
async-trait = "0.1.64"
clap = { version = "4.1.6", features = ["derive"] }
colored = "2.0.0"
Expand All @@ -37,6 +38,7 @@ tera = { version = "1.17.1", default-features = false }
tiktoken-rs = "0.1.2"
tokio = { version = "1.25.0", features = ["full"] }
toml = "0.7.2"
toml_edit = "0.19.4"
which = "4.4.0"

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Configs are applied in the following order:
- The settings as read from the repo clone at `$GIT_ROOT/.git/gptcommit.toml`.
- Environment variables starting with `GPTCOMMIT__*`.

See all the config options available with `gptcommit config keys`.

### Set your OpenAI API key

Persist your OpenAI key
Expand Down Expand Up @@ -139,7 +141,6 @@ All of these awesome projects are built using `gptcommit`.

If you encounter any bugs or have any suggestions for improvements, please open an issue on the repository.


## License

This project is licensed under the [MIT License](./LICENSE).
1 change: 1 addition & 0 deletions e2e/test_config_list_get_set.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -eu

gptcommit config list
# assert is valid TOML
gptcommit config keys

gptcommit config get openai.model
# assert default = text-davinci-003
Expand Down
71 changes: 55 additions & 16 deletions e2e/test_config_local_list_get_set.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
#!/bin/sh
set -eu

gptcommit config list
# assert is valid TOML

gptcommit config get openai.model
# assert default = text-davinci-003
gptcommit config set --local openai.model foo
gptcommit config set openai.model bar
gptcommit config get openai.model
# assert is foo

gptcommit config delete openai.model
gptcommit config get openai.model
# assert still is foo
gptcommit config delete --local openai.model
gptcommit config get openai.model
# assert is default
export TEMPDIR=$(mktemp -d)
(
cd "${TEMPDIR}"
git init

gptcommit config list
# assert is valid TOML

gptcommit config get openai.model
# assert default = text-davinci-003
gptcommit config set --local openai.model foo
gptcommit config set openai.model bar
gptcommit config get openai.model
# assert is foo

gptcommit config delete openai.model
gptcommit config get openai.model
# assert still is foo
gptcommit config delete --local openai.model
gptcommit config get openai.model
# assert is default
)
rm -rf "${TEMPDIR}"


export TEMPDIR=$(mktemp -d)
(
cd "${TEMPDIR}"

gptcommit config list
# assert is valid TOML

gptcommit config get openai.model
# assert default = text-davinci-003
set +e
gptcommit config set --local openai.model foo
# TODO assert output
test $? -ne 0 || exit $?
set -e
gptcommit config set openai.model bar
gptcommit config get openai.model
# assert is foo

gptcommit config delete openai.model
gptcommit config get openai.model
# assert still is foo
set +e
gptcommit config delete --local openai.model
# TODO assert output
test $? -ne 0 || exit $?
set -e
gptcommit config get openai.model
# assert is default
)
rm -rf "${TEMPDIR}"
43 changes: 32 additions & 11 deletions src/actions/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ use std::{collections::VecDeque, fs, path::PathBuf};
use clap::{Args, Subcommand};
use toml::Value;

use crate::settings::{get_local_config_path, get_user_config_path, Settings};
use crate::{
settings::{get_local_config_path, get_user_config_path, Settings},
toml::DeepKeysCollector,
};
use anyhow::{bail, Result};

#[derive(Subcommand, Debug)]
pub(crate) enum ConfigAction {
/// List all config keys
Keys,
/// List all config values
List {
/// if set, will save the config to the user's config file
Expand Down Expand Up @@ -43,21 +48,41 @@ pub(crate) async fn main(settings: Settings, args: ConfigArgs) -> Result<()> {
debug!("Config subcommand - Settings = {:?}", settings);

match args.action {
ConfigAction::Keys => keys(settings).await,
ConfigAction::List { save } => list(settings, save).await,
ConfigAction::Get { key } => get(settings, key).await,
ConfigAction::Set { key, value, local } => set(settings, key, value, local).await,
ConfigAction::Delete { key, local } => delete(settings, key, local).await,
}
}

fn get_config_path(local: bool) -> Result<PathBuf> {
if local {
if let Some(config_path) = get_local_config_path() {
Ok(config_path)
} else {
bail!("No repo-local config found. Please run `git init` to create a repo first");
}
} else if let Some(config_path) = get_user_config_path() {
Ok(config_path)
} else {
bail!("No user config found.");
}
}

async fn keys(settings: Settings) -> Result<()> {
let toml_string = toml::to_string_pretty(&settings).unwrap();
let keys = DeepKeysCollector::get_keys(toml_string);
for key in keys {
println!("{key}");
}
Ok(())
}

async fn delete(_settings: Settings, full_key: String, local: bool) -> Result<()> {
let settings = &Settings::from_clear(&full_key)?;
let toml_string = toml::to_string_pretty(settings).unwrap();
let config_path: PathBuf = if local {
get_local_config_path().expect("Could not find repo-local config path")
} else {
get_user_config_path().expect("Could not find user config path")
};
let config_path = get_config_path(local)?;
fs::write(&config_path, toml_string)?;
println!("Cleared {full_key}");
println!("Config saved to {}", config_path.display());
Expand All @@ -67,11 +92,7 @@ async fn delete(_settings: Settings, full_key: String, local: bool) -> Result<()
async fn set(_settings: Settings, full_key: String, value: String, local: bool) -> Result<()> {
let settings = &Settings::from_set_override(&full_key, &value)?;
let toml_string = toml::to_string_pretty(settings).unwrap();
let config_path: PathBuf = if local {
get_local_config_path().expect("Could not find repo-local config path")
} else {
get_user_config_path().expect("Could not find user config path")
};
let config_path = get_config_path(local)?;
fs::write(&config_path, toml_string)?;
println!("{full_key} = {value}");
println!("Config saved to {}", config_path.display());
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod cmd;
mod git;
mod prompt;
mod summarize;
mod toml;
mod util;

use anyhow::Result;
Expand Down
Loading