aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: a25a5f75d10ae77c1cf63ba77e11341a7a4dd6d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use serde::{Deserialize, Serialize};
//use serde_json::Result;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
    env::{temp_dir, var},
    fs::File,
    io::Read,
    process::Command,
};
use std::fs;

#[derive(Serialize, Deserialize, Debug)]
struct BugReport {
    timestamp: String,
    status: String,
    title: String,
    description: Vec<String>,
    tags: Option<String>,
    version: String
}

fn new_bug() {
    let editor = var("EDITOR").unwrap();
    let mut file_path = temp_dir(); // TODO: figgure out how to get .git dir in a save manner
    file_path.push("NEW_BUG_REPORT");
    File::create(&file_path).expect("Could not create file");

    Command::new(editor)
        .arg(&file_path)
        .status()
        .expect("Something went wrong");

    let mut new_bug_report = String::new();
    let _ = File::open(&file_path)
        .expect("Could not open file")
        .read_to_string(&mut new_bug_report);
    let _ = fs::remove_file(&file_path);
    let mut header = "";
    let mut description: Vec::<String> = Vec::new();
    for (i, line) in new_bug_report.lines().enumerate() {
        match i {
            0 => header = line,
            1 => (), 
            _ => description.push(line.to_string()),
        }
    }
    let report = BugReport {
        timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs().to_string(),
        title: header.to_owned(),
        description: description.to_owned(),
        status: "new".to_owned(),
        tags: None,
        version: "v1".to_owned(),
    };
    let j = serde_json::to_string(&report).unwrap();
    print!("{}", j);
}

fn show() {
    let reports = get_reports();
    for report in reports {
        //println!("{:?}", report);
        println!("=================================================================");
        println!("{} | {}", report.status, report.title);
        println!("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for l in report.description {
            println!("{}", l);
        }
    }
}

fn get_reports() -> Vec<BugReport> {
    let mut ret: Vec<BugReport> = Vec::new();
    let output = Command::new("git")
        .arg("ls-tree")
        .arg("--full-tree")
        .arg("-r")
        .arg("refs/notes/devtools/future-me")
        .output()
        .expect("Error with git ls-tree");
    //println!("{}", output.status);
    if output.status.success() {
        let lines = String::from_utf8_lossy(&output.stdout);
        for line in lines.lines() {
            if let Some(after_blob) = line.split_once("blob ") {
                if let Some((hash, _)) = after_blob.1.split_once('\t') {
                    let blob = Command::new("git")
                        .arg("cat-file")
                        .arg("-p")
                        .arg(hash)
                        .output()
                        .expect("Error with git cat-file");
                        if blob.status.success() {
                            let lines = String::from_utf8_lossy(&blob.stdout);
                            // TODO: error handling
                            let bug_report: BugReport = serde_json::from_str(&lines).unwrap();
                            ret.push(bug_report);
                        }
                        else {
                            println!("{}", String::from_utf8_lossy(&blob.stderr));
                        }
                }
            }
        }
    }
    else {
        println!("{}", String::from_utf8_lossy(&output.stderr));
    }
    ret
}

fn main() {
    //new_bug();
    show();
}