Skip to content

Commit 6bef114

Browse files
committed
resolve issues in lpstat
1 parent 0c1de04 commit 6bef114

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

.github/workflows/rust.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,16 @@ jobs:
9393
-e 's/[[:space:]]+/ /g'
9494
}
9595
96-
sys_t="$(lpstat -t | norm)"
97-
ex_t="$(./target/debug/examples/lpstat -t | norm)"
96+
filter_t() {
97+
norm | grep -E \
98+
'^(scheduler is (running|not running)$|system default destination: |no system default destination$|device for )' \
99+
| sort
100+
}
101+
102+
sys_t="$(lpstat -t | filter_t)"
103+
ex_t="$(./target/debug/examples/lpstat -t | filter_t)"
98104
diff -u <(echo "$sys_t") <(echo "$ex_t")
99105
100-
sys_o="$(lpstat -W all -o PDF | norm)"
101-
ex_o="$(./target/debug/examples/lpstat -W all -o PDF | norm)"
106+
sys_o="$(lpstat -W all -o PDF | norm | awk '{print $1, $2, $3}')"
107+
ex_o="$(./target/debug/examples/lpstat -W all -o PDF | norm | awk '{print $1, $2, $3}')"
102108
diff -u <(echo "$sys_o") <(echo "$ex_o")

examples/lpstat.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub enum Action {
4040
ShowServerAndPort, // -H
4141
ShowDefault, // -d
4242
ListDestinations, // -e
43-
ShowSchedular, // -r
43+
ShowScheduler, // -r
4444
ShowSummary, // -s
4545
ShowAll, // -t
4646

@@ -133,7 +133,7 @@ impl Action {
133133
}
134134
}
135135
}
136-
Action::ShowSchedular => {
136+
Action::ShowScheduler => {
137137
if scheduler_is_running() {
138138
println!("scheduler is running");
139139
} else {
@@ -201,11 +201,9 @@ where
201201
let mut commands: Vec<Step> = Vec::new();
202202

203203
while let Some(arg) = it.next() {
204-
if arg == "--help" || arg == "-h" && it.peek().is_none() {
205-
if arg == "--help" {
206-
print_help();
207-
std::process::exit(0);
208-
}
204+
if arg == "--help" {
205+
print_help();
206+
std::process::exit(0);
209207
}
210208

211209
if !arg.starts_with('-') || arg == "-" {
@@ -250,7 +248,7 @@ where
250248
'H' => commands.push(Step::Action(Action::ShowServerAndPort)),
251249
'd' => commands.push(Step::Action(Action::ShowDefault)),
252250
'e' => commands.push(Step::Action(Action::ListDestinations)),
253-
'r' => commands.push(Step::Action(Action::ShowSchedular)),
251+
'r' => commands.push(Step::Action(Action::ShowScheduler)),
254252
's' => commands.push(Step::Action(Action::ShowSummary)),
255253
't' => commands.push(Step::Action(Action::ShowAll)),
256254

@@ -501,13 +499,13 @@ fn show_classes(filter: Option<&str>, ctx: &mut Context) -> Result<()> {
501499
name: "member-names",
502500
kind: AttrKind::StringLike,
503501
}];
502+
503+
let conn = ctx.dests.first().and_then(|d| d.connect(ConnectionFlags::Scheduler, Some(5000), None).ok());
504504

505505
for dest in &mut ctx.dests {
506-
let conn = match dest.connect(ConnectionFlags::Scheduler, Some(5000), None) {
507-
Ok(c) => c,
508-
Err(_) => continue,
509-
};
510-
let _ = dest.get_attrs(&conn, PRINTER_ATTRS);
506+
if let Some(c) = &conn {
507+
let _ = dest.get_attrs(c, PRINTER_ATTRS);
508+
}
511509

512510
let members_raw = match dest.options.get("member-names") {
513511
Some(s) => s.as_str(),
@@ -590,17 +588,16 @@ pub fn show_printers(filter: Option<&str>, ctx: &mut Context) -> Result<()> {
590588
// CUPS_PRINTER_REMOTE bit (matches CUPS headers)
591589
const CUPS_PRINTER_REMOTE: i32 = 0x00000002;
592590

591+
let conn = ctx.dests.first().and_then(|d| d.connect(ConnectionFlags::Scheduler, Some(5000), None).ok());
592+
593593
for dest in &mut ctx.dests {
594594
if !match_list(filter, dest.name.as_str(), dest.instance.as_deref()) {
595595
continue;
596596
}
597597

598-
let conn = match dest.connect(ConnectionFlags::Scheduler, Some(5000), None) {
599-
Ok(c) => c,
600-
Err(_) => continue,
601-
};
602-
603-
let _ = dest.get_attrs(&conn, PRINTER_ATTRS);
598+
if let Some(c) = &conn {
599+
let _ = dest.get_attrs(c, PRINTER_ATTRS);
600+
}
604601

605602
let pstate = dest
606603
.options
@@ -822,17 +819,16 @@ pub fn show_accepting(filter: Option<&str>, ctx: &mut Context) -> Result<()> {
822819
)
823820
}
824821

822+
let conn = ctx.dests.first().and_then(|d| d.connect(ConnectionFlags::Scheduler, Some(5000), None).ok());
823+
825824
for dest in &mut ctx.dests {
826825
if !match_list(filter, dest.name.as_str(), dest.instance.as_deref()) {
827826
continue;
828827
}
829828

830-
let conn = match dest.connect(ConnectionFlags::Scheduler, Some(5000), None) {
831-
Ok(c) => c,
832-
Err(_) => continue,
833-
};
834-
835-
let _ = dest.get_attrs(&conn, ACCEPTING_ATTRS);
829+
if let Some(c) = &conn {
830+
let _ = dest.get_attrs(c, ACCEPTING_ATTRS);
831+
}
836832

837833
let ptime = dest
838834
.options
@@ -926,11 +922,17 @@ fn get_job_extras(which: WhichJobs) -> Result<JobExtras> {
926922
];
927923

928924
let mut req = IppRequest::new(IppOperation::GetJobs)?;
925+
let server = get_server();
926+
let uri = if server.starts_with('/') {
927+
"ipp://localhost/".to_string()
928+
} else {
929+
format!("ipp://{}/", server)
930+
};
929931
req.add_string(
930932
IppTag::Operation,
931933
IppValueTag::Uri,
932934
"printer-uri",
933-
"ipp://localhost/",
935+
&uri,
934936
)?;
935937
req.add_strings(
936938
IppTag::Operation,

0 commit comments

Comments
 (0)