aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/git-sugpatch.rs
blob: 4560c9ad0f5e2ab20e85c4879a63852661b736a2 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::env;
use std::process;

use exitcode;

use github_suggestion::{Client, Suggestion, SuggestionUrl};
use github_suggestion_cli::{gseprintln, is_suggestion_id, owner_repo};
use github_suggestion_cli::config::Config;
use github_suggestion_cli::error::Error;


fn main() {
    let args: Vec<_> = env::args().collect();

    let config = match Config::get(&args) {
        Ok(c) => c,
        Err(e) => {
            gseprintln!(e);

            process::exit(exitcode::CONFIG);
        },
    };

    if config.suggestions.is_empty() {
        process::exit(111);
    }

    // let mut owner_repo_error: owner_repo::Error;
    // let o_r = match config.o_r {
    //     Ok(o_r) => o_r,
    //     Err(e @ owner_repo::Error::NoRemote(_)) => owner_repo_error = e,
    //     Err(e) => {
    //         gseprintln!(e);
    //
    //         process::exit(111);
    //     },
    // };

    for suggestion_arg in config.suggestions {
        let suggestion = if match is_suggestion_id(&suggestion_arg) {
            Ok(p) => p,
            Err(e) => {
                gseprintln!(e);

                process::exit(exitcode::SOFTWARE);
            }
        } {
            // let o_r = match config.o_r {
            //     Ok(o_r) => o_r,
            //     Err(e) => {
            //         gseprintln!(e);
            //
            //         process::exit(exitcode::CONFIG);
            //     }
            // };
            // let o_r = match owner_repo {
            //     Ok(o_r) => o_r,
            //     Err(e) => {
            //         gseprintln!(e);
            //
            //         process::exit(exitcode::CONFIG);
            //     },
            // };
            // if let owner_repo::Error::NoRemote(e) = owner_repo_error {
            //     gseprintln!(e);
            //
            //     process::exit(exitcode::CONFIG);
            // }
            let o_r = match &config.o_r {
                Ok(o_r) => o_r,
                Err(e) => {
                    gseprintln!(e);
                    process::exit(exitcode::CONFIG);
                },
            };

            let client = Client::new(
                &config.github_token,
                &o_r.owner,
                &o_r.repo,
            ).unwrap();

            client.fetch(&suggestion_arg).unwrap()
        } else {
            let url: SuggestionUrl = args[1].parse().unwrap();

            let client = Client::new(
                &config.github_token,
                &url.owner,
                &url.repo,
            ).unwrap();

            client.fetch(&url.comment_id).unwrap()
        };

        print!("{}", suggestion.diff().unwrap());
    }

    // let suggestions: Vec<Result<Suggestion, Error>> = config.suggestions
    //     .iter()
    //     .map(|s| {
    //         let suggestion = if is_suggestion_id(s)? {
    //             let o_r = owner_repo?;
    //
    //             let client = Client::new(
    //                 &config.github_token,
    //                 &o_r.owner,
    //                 &o_r.repo,
    //             ).unwrap();
    //
    //             client.fetch(&s).unwrap()
    //         } else {
    //             let url: SuggestionUrl = args[1].parse().unwrap();
    //
    //             let client = Client::new(
    //                 &config.github_token,
    //                 &url.owner,
    //                 &url.repo,
    //             ).unwrap();
    //
    //             client.fetch(&url.comment_id).unwrap()
    //         };
    //
    //         Ok(suggestion)
    //     })
    //     .collect();
    //
    // let errors: Vec<&Error> = suggestions.iter()
    //     .filter(|r| r.is_err())
    //
    //     // We know these `Results` are `Err`s.
    //     .map(|r| r.as_ref().err().unwrap())
    //     .collect();
    //
    // if !errors.is_empty() {
    //     for error in errors {
    //         eprintln!("error: {}", error);
    //     }
    //
    //     return;
    // }
    //
    // suggestions
    //     .iter()
    //
    //     // We've already checked for `Err`s above.
    //     .map(|r| r.as_ref().unwrap())
    //     .for_each(|s| print!("{}", s.diff().unwrap()));
}