-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
db.rs
237 lines (222 loc) · 6.6 KB
/
db.rs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::collections::BTreeMap;
use crate::model::asset_name::AssetName;
use crate::model::tool::{ToolInfo, ToolInfoTag};
const NOT_SUPPORTED: &str = "NOT_SUPPORTED";
/// Get info about known tools from a hardcoded database
pub fn lookup_tool(tool_name: &str) -> Option<ToolInfo> {
let mut known_db = build_db();
known_db.remove(tool_name)
}
pub fn build_db() -> BTreeMap<String, ToolInfo> {
let mut tools = BTreeMap::<&'static str, StaticToolInfo>::new();
tools.insert(
"bat",
StaticToolInfo {
owner: "sharkdp",
repo: "bat",
exe_name: "bat",
linux: "x86_64-unknown-linux-musl",
macos: "x86_64-apple-darwin",
windows: "x86_64-pc-windows-msvc",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"difftastic",
StaticToolInfo {
owner: "Wilfred",
repo: "difftastic",
exe_name: "difft",
linux: "x86_64-unknown-linux-gnu",
macos: "x86_64-apple-darwin",
windows: "x86_64-pc-windows-msvc",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"exa",
StaticToolInfo {
owner: "ogham",
repo: "exa",
exe_name: "exa",
linux: "linux-x86_64-musl",
macos: "macos-x86_64",
windows: NOT_SUPPORTED,
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"fd",
StaticToolInfo {
owner: "sharkdp",
repo: "fd",
exe_name: "fd",
linux: "x86_64-unknown-linux-musl",
macos: "x86_64-apple-darwin",
windows: "x86_64-pc-windows-msvc",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"hyperfine",
StaticToolInfo {
owner: "sharkdp",
repo: "hyperfine",
exe_name: "hyperfine",
linux: "x86_64-unknown-linux-musl",
macos: "x86_64-apple-darwin",
windows: "x86_64-pc-windows-msvc",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"just",
StaticToolInfo {
owner: "casey",
repo: "just",
exe_name: "just",
linux: "x86_64-unknown-linux-musl",
macos: "x86_64-apple-darwin",
windows: "x86_64-pc-windows-msvc",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"k9s",
StaticToolInfo {
owner: "derailed",
repo: "k9s",
exe_name: "k9s",
linux: "Linux_amd64",
macos: "Darwin_amd64",
windows: "Windows_amd64",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"procs",
StaticToolInfo {
owner: "dalance",
repo: "procs",
exe_name: "procs",
linux: "x86_64-linux",
macos: "x86_64-mac",
windows: "x86_64-windows.zip",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"ripgrep",
StaticToolInfo {
owner: "BurntSushi",
repo: "ripgrep",
exe_name: "rg",
linux: "unknown-linux-musl",
macos: "apple-darwin",
windows: "x86_64-pc-windows-msvc",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"tool-sync",
StaticToolInfo {
owner: "chshersh",
repo: "tool-sync",
exe_name: "tool",
linux: "x86_64-unknown-linux-musl.tar.gz",
macos: "x86_64-apple-darwin.tar.gz",
windows: "x86_64-pc-windows-msvc.zip",
tag: ToolInfoTag::Latest,
},
);
tools.insert(
"github",
StaticToolInfo {
owner: "cli",
repo: "cli",
exe_name: "gh",
linux: "linux_amd64.tar.gz",
macos: "macOS_amd64",
windows: "windows_amd64.zip",
tag: ToolInfoTag::Latest,
},
);
//tools.insert(
// "tokei",
// StaticToolInfo {
// owner: "XAMPPRocky",
// repo: "tokei",
// exe_name: "tokei",
// linux: "x86_64-unknown-linux-musl",
// macos: "apple-darwin",
// windows: "x86_64-pc-windows-msvc",
// tag: ToolInfoTag::Latest,
// }
//);
BTreeMap::from_iter(
tools
.into_iter()
.map(|(name, tool_info)| (name.to_owned(), tool_info.into())),
)
}
/// Format tool names and info of the database using a name formatting function
/// The result is something like this (depending on a function)
///
/// ```toml
/// # [bat] # https://github.com/ogham/exa
/// # [exa] # https://github.com/sharkdp/bat
/// ```
pub fn fmt_tool_names_info<F: FnMut(&String) -> String>(mut fmt_name: F) -> String {
let known_db: BTreeMap<String, ToolInfo> = build_db();
let max_name_len: usize = known_db.keys().map(|a| a.len()).max().unwrap_or_default() + 1;
known_db
.iter()
.map(|(name, info): (&String, &ToolInfo)| {
format!(
"{formatted_name} {delim:>padding$} https://github.com/{owner}/{repo}",
formatted_name = fmt_name(name),
delim = "#",
padding = max_name_len - name.len(),
owner = info.owner,
repo = info.repo,
)
})
.collect::<Vec<String>>()
.join("\n")
}
struct StaticToolInfo {
/// GitHub repository author
pub owner: &'static str,
/// GitHub repository name
pub repo: &'static str,
/// Executable name inside the .tar.gz or .zip archive
pub exe_name: &'static str,
/// Version tag
pub tag: ToolInfoTag,
pub linux: &'static str,
pub macos: &'static str,
pub windows: &'static str,
}
impl From<StaticToolInfo> for ToolInfo {
fn from(static_tool_info: StaticToolInfo) -> Self {
ToolInfo {
owner: static_tool_info.owner.to_string(),
repo: static_tool_info.repo.to_string(),
exe_name: static_tool_info.exe_name.to_string(),
asset_name: AssetName {
linux: from_supported_asset(static_tool_info.linux),
macos: from_supported_asset(static_tool_info.macos),
windows: from_supported_asset(static_tool_info.windows),
},
tag: static_tool_info.tag,
}
}
}
#[inline]
fn from_supported_asset(asset_name: &str) -> Option<String> {
if asset_name == NOT_SUPPORTED {
None
} else {
Some(asset_name.to_string())
}
}