Browse Source

Use log and beautylog

master
Thomas Forgione 2 months ago
parent
commit
f7e8a96550
Signed by: Thomas Forgione <thomas@forgione.fr> GPG Key ID: 203DAEA747F48F41
7 changed files with 66 additions and 70 deletions
  1. 31
    4
      Cargo.lock
  2. 2
    0
      Cargo.toml
  3. 1
    4
      src/cache.rs
  4. 11
    6
      src/gclone.rs
  5. 1
    1
      src/git.rs
  6. 13
    46
      src/lib.rs
  7. 7
    9
      src/pgd.rs

+ 31
- 4
Cargo.lock View File

@@ -1,26 +1,50 @@
1 1
 [[package]]
2
+name = "beautylog"
3
+version = "0.1.0"
4
+source = "registry+https://github.com/rust-lang/crates.io-index"
5
+dependencies = [
6
+ "colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
7
+ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
8
+]
9
+
10
+[[package]]
11
+name = "cfg-if"
12
+version = "0.1.6"
13
+source = "registry+https://github.com/rust-lang/crates.io-index"
14
+
15
+[[package]]
2 16
 name = "colored"
3 17
 version = "1.7.0"
4 18
 source = "registry+https://github.com/rust-lang/crates.io-index"
5 19
 dependencies = [
6
- "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
20
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
7 21
 ]
8 22
 
9 23
 [[package]]
10 24
 name = "gclone"
11 25
 version = "0.1.0"
12 26
 dependencies = [
27
+ "beautylog 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
13 28
  "colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
14
- "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
29
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
30
+ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
15 31
  "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
16 32
 ]
17 33
 
18 34
 [[package]]
19 35
 name = "lazy_static"
20
-version = "1.2.0"
36
+version = "1.3.0"
21 37
 source = "registry+https://github.com/rust-lang/crates.io-index"
22 38
 
23 39
 [[package]]
40
+name = "log"
41
+version = "0.4.6"
42
+source = "registry+https://github.com/rust-lang/crates.io-index"
43
+dependencies = [
44
+ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
45
+]
46
+
47
+[[package]]
24 48
 name = "same-file"
25 49
 version = "1.0.4"
26 50
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -66,8 +90,11 @@ version = "0.4.0"
66 90
 source = "registry+https://github.com/rust-lang/crates.io-index"
67 91
 
68 92
 [metadata]
93
+"checksum beautylog 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1457a99b95a92f1de6f3a1fe08324f200cd68e0f43b13d4289752ead6ed531a2"
94
+"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
69 95
 "checksum colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e9a455e156a4271e12fd0246238c380b1e223e3736663c7a18ed8b6362028a9"
70
-"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
96
+"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
97
+"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
71 98
 "checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267"
72 99
 "checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1"
73 100
 "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"

+ 2
- 0
Cargo.toml View File

@@ -8,6 +8,8 @@ edition = "2018"
8 8
 colored = "1.7.0"
9 9
 walkdir = "2.2.7"
10 10
 lazy_static = "1.2.0"
11
+log = "0.4.6"
12
+beautylog = "0.1.0"
11 13
 
12 14
 [[bin]]
13 15
 name = "gclone"

+ 1
- 4
src/cache.rs View File

@@ -98,10 +98,7 @@ impl Cache {
98 98
             1 => Ok(matches[0].clone()),
99 99
 
100 100
             len => {
101
-                eprintln!(
102
-                    "{}",
103
-                    "info: multiple entries found, please select one".bold()
104
-                );
101
+                info!("{}", "multiple entries found, please select one");
105 102
 
106 103
                 for (i, j) in matches.iter().enumerate() {
107 104
                     eprintln!(

+ 11
- 6
src/gclone.rs View File

@@ -1,8 +1,9 @@
1
+#[macro_use]
2
+extern crate log;
3
+
1 4
 use std::path::PathBuf;
2 5
 use std::process::exit;
3 6
 
4
-use colored::*;
5
-
6 7
 use gclone::git::{clone, parse_url, GCLONE_PATH};
7 8
 use gclone::{first_arg, Cache, Result};
8 9
 
@@ -11,6 +12,10 @@ fn help() {
11 12
 }
12 13
 
13 14
 fn main_result() -> Result<()> {
15
+
16
+    // Init beautylog
17
+    beautylog::init(log::LevelFilter::Info).ok();
18
+
14 19
     // Parse args
15 20
     let url = first_arg()?;
16 21
 
@@ -33,9 +38,9 @@ fn main_result() -> Result<()> {
33 38
     path.push(&repo);
34 39
 
35 40
     // Clone repository
36
-    eprintln!("{} {} {}{}", "info:".bold(), "cloning", url, "...");
37
-    clone(&url, &path.display().to_string())?;
38
-    eprintln!("{} {}", "info:".bold(), "done!");
41
+    info!("{} {}{}", "cloning", url, "...");
42
+    clone(&url, &path)?;
43
+    info!("{}", "done!");
39 44
 
40 45
     // Append to cache
41 46
     let mut cache = Cache::read()?;
@@ -47,7 +52,7 @@ fn main_result() -> Result<()> {
47 52
 
48 53
 fn main() {
49 54
     if let Err(e) = main_result() {
50
-        eprintln!("{}", e);
55
+        error!("{}", e);
51 56
         exit(1);
52 57
     }
53 58
 }

+ 1
- 1
src/git.rs View File

@@ -16,7 +16,7 @@ lazy_static! {
16 16
             Ok(d) => d,
17 17
             Err(e) => {
18 18
                 let e: Error = e.into();
19
-                eprintln!("{}", e);
19
+                error!("couldn't read environnement variable GCLONE_PATH: {}", e);
20 20
                 exit(1);
21 21
             },
22 22
         }

+ 13
- 46
src/lib.rs View File

@@ -12,11 +12,12 @@
12 12
 
13 13
 #![warn(missing_docs)]
14 14
 
15
+#[macro_use]
16
+extern crate log;
17
+
15 18
 use std::io;
16 19
 use std::{env, error, fmt, num, result};
17 20
 
18
-use colored::*;
19
-
20 21
 mod cache;
21 22
 pub use cache::Cache;
22 23
 
@@ -71,68 +72,34 @@ impl fmt::Display for Error {
71 72
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
72 73
         match self {
73 74
             Error::NoGclonePath(e) => write!(
74
-                fmt,
75
-                "{} {} {}",
76
-                "error:".bold().red(),
77
-                "couldn't read environment variable GCLONE_PATH".bold(),
78
-                e
75
+                fmt, "couldn't read environment variable GCLONE_PATH {}", e
79 76
             ),
80 77
             Error::IoError(e) => write!(
81
-                fmt,
82
-                "{} {} {}",
83
-                "error:".bold().red(),
84
-                "couldn't read or write cache:".bold(),
85
-                e
78
+                fmt, "couldn't read or write cache: {}", e
86 79
             ),
87 80
             Error::GitUrlParseError => write!(
88
-                fmt,
89
-                "{} {}",
90
-                "error:".bold().red(),
91
-                "couldn't guess server, owner and repo from url".bold()
81
+                fmt, "couldn't guess server, owner and repo from url"
92 82
             ),
93 83
             Error::GitCloneError(Some(e)) => write!(
94
-                fmt,
95
-                "{} {} {}",
96
-                "error:".bold().red(),
97
-                "couldn't clone git repository:".bold(),
98
-                e
84
+                fmt, "couldn't clone git repository: {}", e
99 85
             ),
100 86
             Error::GitCloneError(None) => write!(
101
-                fmt,
102
-                "{} {}",
103
-                "error:".bold().red(),
104
-                "couldn't clone git repository".bold()
87
+                fmt, "couldn't clone git repository"
105 88
             ),
106 89
             Error::PathAlreadyExists => write!(
107
-                fmt,
108
-                "{} {}",
109
-                "error:".bold().red(),
110
-                "the path corresponding to the repository already exists".bold()
90
+                fmt, "the path corresponding to the repository already exists"
111 91
             ),
112 92
             Error::WrongArgument => write!(
113
-                fmt,
114
-                "{} {}",
115
-                "error:".bold().red(),
116
-                "this program expects exactly one argument".bold()
93
+                fmt, "this program expects exactly one argument"
117 94
             ),
118 95
             Error::ParseIntError(e) => write!(
119
-                fmt,
120
-                "{} {} {}",
121
-                "error:".bold().red(),
122
-                "couldn't parse integer:".bold(),
123
-                e
96
+                fmt, "couldn't parse integer: {}", e
124 97
             ),
125 98
             Error::OutsideRange => write!(
126
-                fmt,
127
-                "{} {}",
128
-                "error:".bold().red(),
129
-                "integer is outside the range".bold()
99
+                fmt, "integer is outside the range"
130 100
             ),
131 101
             Error::NoSuchDirectory => write!(
132
-                fmt,
133
-                "{} {}",
134
-                "error:".bold().red(),
135
-                "no such directory was found".bold()
102
+                fmt, "no such directory was found"
136 103
             ),
137 104
         }
138 105
     }

+ 7
- 9
src/pgd.rs View File

@@ -1,12 +1,14 @@
1
-use std::process::exit;
2
-
3
-use colored::*;
1
+#[macro_use]
2
+extern crate log;
4 3
 
4
+use std::process::exit;
5 5
 use gclone::{first_arg, Cache, Error, Result};
6 6
 
7 7
 fn main() {
8
+    beautylog::init(log::LevelFilter::Info).ok();
9
+
8 10
     if let Err(e) = main_result() {
9
-        eprintln!("{}", e);
11
+        error!("{}", e);
10 12
         exit(1);
11 13
     }
12 14
 }
@@ -28,11 +30,7 @@ fn main_with_cache(request: &str, regen: bool) -> Result<()> {
28 30
 
29 31
     let only_match = match cache.find_interactive(&request) {
30 32
         Err(Error::NoSuchDirectory) if regen && !just_generated => {
31
-            eprintln!(
32
-                "{} {}",
33
-                "warning:".bold().yellow(),
34
-                "directory not found, regenerating cache...".bold()
35
-            );
33
+            warn!("directory not found, regenerating cache...");
36 34
             let cache = Cache::generate();
37 35
             cache.write()?;
38 36
             return main_with_cache(request, false);

Loading…
Cancel
Save