From a921c853ea95572e140eeec9db58e97e8f6087f1 Mon Sep 17 00:00:00 2001 From: Andreas Bigger Date: Wed, 14 Dec 2022 08:20:45 -0700 Subject: [PATCH 1/2] :memo: prevent resource leaking with temp file destruction --- Cargo.toml | 1 + src/main.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b0ffcd4..cf11304 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ clap = { version = "4.0.29", features = ["derive"] } reqwest = { version = "0.11.13", default-features = false, features = ["json", "blocking", "native-tls-crate", "default-tls", "hyper-tls", "__tls"] } bat = { version = "0.22.1", default-features = false, features = ["regex-onig"] } colored = "2.0.0" +tempfile = "3.3.0" diff --git a/src/main.rs b/src/main.rs index a252d08..65edfe6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,8 @@ use question::{Answer, Question}; use reqwest::blocking::Client; use serde_json::json; use spinners::{Spinner, Spinners}; -use std::{env, fs, io::Write, process::Command}; +use std::{env, io::Write, process::Command}; +use tempfile::tempfile; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -40,7 +41,7 @@ fn main() { "model": "text-davinci-003", "prompt": format!("{}:\n```bash\n#!/bin/bash\n", cli.prompt), })) - .header("Authorization", format!("Bearer {}", api_key)) + .header("Authorization", format!("Bearer {api_key}")) .send() .unwrap() .error_for_status() @@ -71,7 +72,14 @@ fn main() { .print() .unwrap(); - let mut file = fs::File::create(".tmp.sh").unwrap(); + // Creates a temporary file with destructor that will delete it when this variable goes out of scope + let mut file = tempfile().unwrap_or_else(|_| { + spinner.stop_and_persist( + "✖".red().to_string().as_str(), + "Failed to create a temporary file.".red().to_string(), + ); + std::process::exit(1); + }); file.write_all(text.as_bytes()).unwrap(); let mut should_run = true; @@ -121,6 +129,4 @@ fn main() { println!("{}", String::from_utf8_lossy(&output.stdout)); } - - fs::remove_file(".tmp.sh").unwrap(); } From 75dd09f3af6e3c9cb8f513a86d370d4a2380a1ae Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Wed, 14 Dec 2022 19:21:15 +0100 Subject: [PATCH 2/2] make sure we're calling the correct file --- Cargo.toml | 1 + src/main.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf11304..fba8fb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ reqwest = { version = "0.11.13", default-features = false, features = ["json", " bat = { version = "0.22.1", default-features = false, features = ["regex-onig"] } colored = "2.0.0" tempfile = "3.3.0" +filepath = "0.1.2" diff --git a/src/main.rs b/src/main.rs index 65edfe6..99b94c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,16 @@ use bat::PrettyPrinter; use clap::Parser; use colored::*; +use filepath::FilePath; use question::{Answer, Question}; use reqwest::blocking::Client; use serde_json::json; use spinners::{Spinner, Spinners}; -use std::{env, io::Write, process::Command}; +use std::{ + env, + io::{Read, Write}, + process::Command, +}; use tempfile::tempfile; #[derive(Parser, Debug)] @@ -72,7 +77,6 @@ fn main() { .print() .unwrap(); - // Creates a temporary file with destructor that will delete it when this variable goes out of scope let mut file = tempfile().unwrap_or_else(|_| { spinner.stop_and_persist( "✖".red().to_string().as_str(), @@ -101,9 +105,8 @@ fn main() { if should_run { spinner = Spinner::new(Spinners::BouncingBar, "Executing...".into()); - // run command and print output and error let output = Command::new("bash") - .arg(".tmp.sh") + .arg(file.path().expect("Couldn't get path of temporary file.")) .output() .unwrap_or_else(|_| { spinner.stop_and_persist(