Просмотр исходного кода

bin/tau: remove temp file when done

Dastan-glitch 4 лет назад
Родитель
Сommit
93a2fc7722
3 измененных файлов с 7 добавлено и 3 удалено
  1. 1 0
      Cargo.lock
  2. 1 0
      bin/tau/tau-cli/Cargo.toml
  3. 5 3
      bin/tau/tau-cli/src/util.rs

+ 1 - 0
Cargo.lock

@@ -3928,6 +3928,7 @@ dependencies = [
  "log",
  "log",
  "num_cpus",
  "num_cpus",
  "prettytable-rs",
  "prettytable-rs",
+ "rand",
  "serde",
  "serde",
  "serde_json",
  "serde_json",
  "simplelog",
  "simplelog",

+ 1 - 0
bin/tau/tau-cli/Cargo.toml

@@ -22,6 +22,7 @@ simplelog = "0.12.0"
 url = "2.2.2"
 url = "2.2.2"
 chrono = "0.4.19"
 chrono = "0.4.19"
 prettytable-rs = "0.8.0"
 prettytable-rs = "0.8.0"
+rand = "0.8.5"
 
 
 # Encoding and parsing
 # Encoding and parsing
 serde_json = "1.0.79"
 serde_json = "1.0.79"

+ 5 - 3
bin/tau/tau-cli/src/util.rs

@@ -11,6 +11,7 @@ use chrono::{Datelike, Local, NaiveDate, NaiveDateTime};
 use clap::Subcommand;
 use clap::Subcommand;
 use log::error;
 use log::error;
 use prettytable::{cell, format, row, Cell, Row, Table};
 use prettytable::{cell, format, row, Cell, Row, Table};
+use rand::distributions::{Alphanumeric, DistString};
 use serde::{Deserialize, Serialize};
 use serde::{Deserialize, Serialize};
 use serde_json::Value;
 use serde_json::Value;
 
 
@@ -171,8 +172,8 @@ pub fn set_title() -> Result<String> {
 pub fn desc_in_editor() -> Result<Option<String>> {
 pub fn desc_in_editor() -> Result<Option<String>> {
     // Create a temporary file with some comments inside
     // Create a temporary file with some comments inside
     let mut file_path = temp_dir();
     let mut file_path = temp_dir();
-    file_path.push("temp_file");
-    File::create(&file_path)?;
+    let file_name = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
+    file_path.push(file_name);
     fs::write(
     fs::write(
         &file_path,
         &file_path,
         "\n# Write task description above this line\n# These lines will be removed\n",
         "\n# Write task description above this line\n# These lines will be removed\n",
@@ -190,7 +191,8 @@ pub fn desc_in_editor() -> Result<Option<String>> {
 
 
     // Whatever has been written in temp file, will be read here
     // Whatever has been written in temp file, will be read here
     let mut lines = String::new();
     let mut lines = String::new();
-    File::open(file_path)?.read_to_string(&mut lines)?;
+    File::open(&file_path)?.read_to_string(&mut lines)?;
+    fs::remove_file(file_path)?;
 
 
     // Store only non-comment lines
     // Store only non-comment lines
     let mut description = String::new();
     let mut description = String::new();