Skip to content

Commit 4bcceb2

Browse files
committed
feat: add files
Signed-off-by: Kaiyang Wu <[email protected]>
1 parent 92f6e74 commit 4bcceb2

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

src/cli.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ pub enum Subcommands {
3131
},
3232
/// List 100 latest source updates
3333
Updates,
34+
/// List files for a package
35+
Files {
36+
arch: String,
37+
repo: String,
38+
package: String,
39+
version: String,
40+
},
3441
}
3542

3643
#[cfg(feature = "argh")]
@@ -50,6 +57,7 @@ pub enum Subcommands {
5057
Show(Show),
5158
Search(Search),
5259
Updates(Updates),
60+
Files(Files),
5361
}
5462

5563
#[cfg(feature = "argh")]
@@ -96,3 +104,18 @@ pub struct Search {
96104
/// List 100 latest source updates
97105
#[argh(subcommand, name = "updates")]
98106
pub struct Updates {}
107+
108+
#[cfg(feature = "argh")]
109+
#[derive(FromArgs, Debug)]
110+
/// List files for a package
111+
#[argh(subcommand, name = "files")]
112+
pub struct Files {
113+
#[argh(positional)]
114+
pub arch: String,
115+
#[argh(positional)]
116+
pub repo: String,
117+
#[argh(positional)]
118+
pub package: String,
119+
#[argh(positional)]
120+
pub version: String,
121+
}

src/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ async fn run() -> Result<()> {
4545
Subcommands::Updates => {
4646
print_res!(single pkgsite, updates, views::updates::UpdatesView);
4747
}
48+
Subcommands::Files {
49+
arch,
50+
repo,
51+
package,
52+
version,
53+
} => {
54+
print_res!(single pkgsite, files, views::files::FilesView, &arch, &repo, &package, &version);
55+
}
4856
},
4957
None => {
5058
print_res!(single pkgsite, index, views::index::IndexView);
@@ -72,6 +80,14 @@ async fn run() -> Result<()> {
7280
Subcommands::Updates(_) => {
7381
print_res!(single pkgsite, updates, views::updates::UpdatesView);
7482
}
83+
Subcommands::Files(Files {
84+
arch,
85+
repo,
86+
package,
87+
version,
88+
}) => {
89+
print_res!(single pkgsite, files, views::files::FilesView, &arch, &repo, &package, &version);
90+
}
7591
},
7692
None => {
7793
print_res!(single pkgsite, index, views::index::IndexView);

src/views/files.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use pkgsite_lib::files::Files;
2+
use std::fmt::Display;
3+
use tabled::{
4+
builder::Builder,
5+
settings::{Alignment, Modify, Padding, Settings, Style, object::SegmentAll},
6+
};
7+
8+
use pkgsite_tools::PADDING;
9+
10+
fn ls_perm(perm: &i32, ftype: &i16) -> String {
11+
// see https://docs.rs/tar/latest/src/tar/entry_type.rs.html#70-87
12+
let ftype = match ftype {
13+
1 => 'l',
14+
3 => 'c',
15+
4 => 'b',
16+
5 => 'd',
17+
6 => 'p',
18+
_ => '-',
19+
};
20+
21+
let perm: String = format!("{perm:b}")
22+
.chars()
23+
.zip("rwxrwxrwx".chars())
24+
.map(|(a, b)| if a == '1' { b } else { '-' })
25+
.collect();
26+
27+
format!("{ftype}{perm}")
28+
}
29+
30+
pub struct FilesView<'a> {
31+
inner: &'a Files,
32+
}
33+
34+
impl<'a> From<&'a Files> for FilesView<'a> {
35+
fn from(inner: &'a Files) -> Self {
36+
Self { inner }
37+
}
38+
}
39+
40+
impl Display for FilesView<'_> {
41+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42+
let mut file_matrix = Builder::default();
43+
for row in &self.inner.files {
44+
file_matrix.push_record(vec![
45+
ls_perm(&row.perm, &row.ftype),
46+
format!("{}({})", row.uname, row.uid),
47+
format!("{}({})", row.gname, row.gid),
48+
row.size.to_string(),
49+
row.filename.clone().unwrap_or_default(),
50+
]);
51+
}
52+
53+
let table_settings = Settings::default().with(Style::blank()).with(
54+
Modify::new(SegmentAll)
55+
.with(Alignment::left())
56+
.with(Padding::new(0, PADDING, 0, 0)),
57+
);
58+
59+
write!(
60+
f,
61+
"Files in \"{}\" ({})
62+
Repository: {}/{}
63+
Package Time: {}
64+
Size: {}
65+
SHA256: {}
66+
Installed Size: {}
67+
Maintainer: {}{}{}
68+
69+
Files:
70+
{}",
71+
&self.inner.pkg.package,
72+
&self.inner.pkg.version,
73+
&self.inner.pkg.architecture,
74+
&self.inner.pkg.repo,
75+
&self.inner.pkg_debtime,
76+
&self.inner.pkg.size,
77+
&self.inner.pkg.sha256,
78+
&self.inner.pkg.installed_size,
79+
&self.inner.pkg.maintainer,
80+
if self.inner.sodepends.is_empty() {
81+
String::new()
82+
} else {
83+
format!("\nLibrary Depends: {}", &self.inner.sodepends.join(", "))
84+
},
85+
if self.inner.soprovides.is_empty() {
86+
String::new()
87+
} else {
88+
format!("\nLibrary Provides: {}", &self.inner.soprovides.join(", "))
89+
},
90+
file_matrix.build().with(table_settings),
91+
)
92+
}
93+
}

src/views/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod depends;
2+
pub mod files;
23
pub mod index;
34
pub mod info;
45
pub mod rdepends;

0 commit comments

Comments
 (0)