Skip to content

Commit

Permalink
fix: Updated write_config_to_toml function to return a Result to hand…
Browse files Browse the repository at this point in the history
…le potential errors in file writing operations
  • Loading branch information
yzzting committed May 12, 2024
1 parent 2c595d6 commit a77cdc0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Error;
use std::path::Path;
use toml;

Expand Down Expand Up @@ -102,6 +103,7 @@ pub fn generate_config_toml() -> String {
toml::to_string(&config).expect("Could not serialize config")
}

pub fn write_config_to_toml(config_toml: &str, path: &Path) {
pub fn write_config_to_toml(config_toml: &str, path: &Path) -> Result<(), Error> {
fs::write(path, config_toml).expect("Could not write to config file");
Ok(())
}
13 changes: 10 additions & 3 deletions src/install.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use std::fs::File;
use std::fs::{self, File};
use std::io::{self, Error, ErrorKind, Result, Write};
use std::path::Path;

Expand All @@ -12,11 +12,18 @@ pub fn install_commit_msg_hook() -> Result<()> {
return Err(Error::new(ErrorKind::NotFound, "Not a git repository"));
}

let config_toml = Path::new("config.toml");
let home_dir = env::var("HOME").expect("Error getting home directory");
let config_dir = Path::new(&home_dir).join(".config/commit_crafter");
let config_dif_clone = config_dir.clone();
fs::create_dir_all(config_dif_clone).expect("Error creating config directory");
let config_toml = config_dir.join("config.toml");
if !config_toml.exists() {
println!("Error: config.toml not found. Start generating default files.");
let config_str = generate_config_toml();
write_config_to_toml(&config_str, config_toml)
if let Err(e) = write_config_to_toml(&config_str, &config_toml) {
eprintln!("Failed to write default config.toml: {}", e);
return Err(e);
}
}

let current_exe_path = env::current_exe()
Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate commit_crafter;
use std::env;

use commit_crafter::{config, git_integration, install, llm, uninstall};

Expand Down Expand Up @@ -56,15 +57,22 @@ fn main() {
eprintln!("Error: No changes to commit");
std::process::exit(1);
}
llm::openai::openai_request(&output, "config.toml").unwrap();
let config_dir = get_config_dir();
llm::openai::openai_request(&output, &config_dir).unwrap();
}
Err(e) => eprintln!("Error: {}", e),
},
_ => (),
}
}

fn get_config_dir() -> String {
let home_dir = env::var("HOME").expect("Error getting home directory");
format!("{}/.config/commit_crafter/config.toml", home_dir)
}

fn handle_config_subcommand(sub_matches: &clap::ArgMatches) {
let config_dir = get_config_dir();
match sub_matches.subcommand() {
Some(("set", matches)) => {
let key = matches
Expand All @@ -73,13 +81,13 @@ fn handle_config_subcommand(sub_matches: &clap::ArgMatches) {
let value = matches
.get_one::<String>("VALUE")
.expect("Required VALUE missing");
config::set_config_key(key, value, "config.toml").expect("Failed to set configuration");
config::set_config_key(key, value, &config_dir).expect("Failed to set configuration");
}
Some(("get", matches)) => {
let key = matches
.get_one::<String>("KEY")
.expect("Required KEY missing");
let value = config::get_config_key(&[key.as_str()], "config.toml")
let value = config::get_config_key(&[key.as_str()], &config_dir)
.expect("Failed to get configuration")
.join("\n");
println!("{}", value);
Expand All @@ -91,8 +99,8 @@ fn handle_config_subcommand(sub_matches: &clap::ArgMatches) {
"openai_model",
"user_language",
];
let values = config::get_config_key(&keys, "config.toml")
.expect("Failed to list configurations");
let values =
config::get_config_key(&keys, &config_dir).expect("Failed to list configurations");
for (key, value) in keys.iter().zip(values.iter()) {
println!("{}: {}", key, value);
}
Expand Down
16 changes: 12 additions & 4 deletions tests/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ fn test_write_config_toml() {
let temp_dir = tempdir().unwrap();
let file_path = temp_dir.path().join("config.toml");
let config_str = config::generate_config_toml();
config::write_config_to_toml(&config_str, &file_path);
if let Err(e) = config::write_config_to_toml(&config_str, &file_path) {
eprintln!("Failed to write config.toml: {}", e);
}

let write_content = fs::read_to_string(&file_path).unwrap();
assert_eq!(config_str, write_content);
Expand All @@ -28,7 +30,9 @@ fn test_set_config_key() {
let file_path = temp_dir.path().join("config.toml");

let config_str = config::generate_config_toml();
config::write_config_to_toml(&config_str, &file_path);
if let Err(e) = config::write_config_to_toml(&config_str, &file_path) {
eprintln!("Failed to write config.toml: {}", e);
}

config::set_config_key("openai_api_key", "test_key", &file_path).unwrap();

Expand All @@ -44,7 +48,9 @@ fn test_get_config_key() {
let file_path = temp_dir.path().join("config.toml");

let config_str = config::generate_config_toml();
config::write_config_to_toml(&config_str, &file_path);
if let Err(e) = config::write_config_to_toml(&config_str, &file_path) {
eprintln!("Failed to write config.toml: {}", e);
}

let key = "openai_api_key";
let value = config::get_config_key(&[key], &file_path).unwrap();
Expand All @@ -59,7 +65,9 @@ fn test_list_config_keys() {
let file_path = temp_dir.path().join("config.toml");

let config_str = config::generate_config_toml();
config::write_config_to_toml(&config_str, &file_path);
if let Err(e) = config::write_config_to_toml(&config_str, &file_path) {
eprintln!("Failed to write config.toml: {}", e);
}

let keys = [
"openai_api_key",
Expand Down
9 changes: 7 additions & 2 deletions tests/test_llm_openai.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use commit_crafter::{config, llm};
use std::env;
use tempfile::tempdir;

#[test]
fn test_openai_request_by_no_config() {
let temp_dir = tempdir().unwrap();
let file_path = temp_dir.path().join("config.toml");
let config_str = config::generate_config_toml();
config::write_config_to_toml(&config_str, &file_path);
if let Err(e) = config::write_config_to_toml(&config_str, &file_path) {
eprintln!("Failed to write config.toml: {}", e);
}

// run openai_request
let result = llm::openai::openai_request("diff_content", file_path.to_str().unwrap());
Expand All @@ -22,8 +25,10 @@ fn test_openai_request_by_no_config() {

#[test]
fn test_openai_request() {
let home_dir = env::var("HOME").expect("Error getting home directory");
let config_dir = format!("{}/.config/commit_crafter/config.toml", home_dir);
// run openai_request
let result = llm::openai::openai_request("diff_content", "config.toml");
let result = llm::openai::openai_request("diff_content", &config_dir);

match result {
Ok(_) => assert!(true),
Expand Down

0 comments on commit a77cdc0

Please sign in to comment.