Skip to content

Commit

Permalink
added non-working refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 committed Jan 5, 2023
1 parent 2abe8e9 commit 9c84b97
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ mod env;
mod communication;
mod gpt;

use std::process::exit;
use std::{process::exit, fs::File,
io::{
Read,
Write
}
};

use structopt::StructOpt;

Expand All @@ -33,6 +38,9 @@ enum Cli {
/// Show the list of commands
#[structopt(short, long)]
file: String,
/// Show the list of commands
#[structopt(short, long)]
out: Option<String>,
},
}

Expand Down Expand Up @@ -71,8 +79,38 @@ async fn main() {
};
println!("{}", answer);
},
Cli::Refactor { help, file } => {
unimplemented!()
Cli::Refactor { help, file, out } => {
if help || file == "" {
println!("Usage: --question=\"YOUR_QUESTION\"");
exit(0);
}
let code = read_file(file);

let answer = match chat_gpt.ask(format!("refactor the following code \n\n: {}", code)).await {
Ok(out) => out,
Err(err) => {
println!("could not query openai: {}", err.to_string());
exit(0);
}
};
if let Some(outfile) = out {
write_file(outfile, answer);
exit(0);
}
println!("{}", answer);
}
}
}

fn read_file(file: String) -> String {
let mut file = File::open(file).expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Unable to read the file");
contents
}

fn write_file(file: String, content: String) {
let mut f = File::create(file)
.expect("Error encountered while creating file!");
f.write_all(content.as_bytes()).expect("Error while writing output to file");
}

0 comments on commit 9c84b97

Please sign in to comment.