Skip to content

Commit cd93931

Browse files
authored
Merge pull request #7891 from cakebaker/uptime_refactoring
uptime: some small refactorings
2 parents c981767 + 157f653 commit cd93931

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/uu/uptime/src/uptime.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ use chrono::{Local, TimeZone, Utc};
99
use clap::ArgMatches;
1010
use std::io;
1111
use thiserror::Error;
12-
use uucore::error::UError;
12+
use uucore::error::{UError, UResult};
1313
use uucore::libc::time_t;
1414
use uucore::uptime::*;
1515

16-
use uucore::error::UResult;
17-
1816
use clap::{Arg, ArgAction, Command, ValueHint, builder::ValueParser};
1917

2018
use uucore::{format_usage, help_about, help_usage};
@@ -35,6 +33,7 @@ const ABOUT: &str = concat!(
3533
const ABOUT: &str = help_about!("uptime.md");
3634

3735
const USAGE: &str = help_usage!("uptime.md");
36+
3837
pub mod options {
3938
pub static SINCE: &str = "since";
4039
pub static PATH: &str = "path";
@@ -54,6 +53,7 @@ pub enum UptimeError {
5453
#[error("extra operand '{0}'")]
5554
ExtraOperandError(String),
5655
}
56+
5757
impl UError for UptimeError {
5858
fn code(&self) -> i32 {
5959
1
@@ -160,7 +160,7 @@ fn uptime_with_file(file_path: &std::ffi::OsString) -> UResult<()> {
160160
show_error!("couldn't get boot time");
161161
print_time();
162162
print!("up ???? days ??:??,");
163-
print_nusers(Some(0))?;
163+
print_nusers(Some(0));
164164
print_loadavg();
165165
set_exit_code(1);
166166
return Ok(());
@@ -170,7 +170,7 @@ fn uptime_with_file(file_path: &std::ffi::OsString) -> UResult<()> {
170170
if non_fatal_error {
171171
print_time();
172172
print!("up ???? days ??:??,");
173-
print_nusers(Some(0))?;
173+
print_nusers(Some(0));
174174
print_loadavg();
175175
return Ok(());
176176
}
@@ -194,20 +194,19 @@ fn uptime_with_file(file_path: &std::ffi::OsString) -> UResult<()> {
194194

195195
#[cfg(target_os = "openbsd")]
196196
{
197-
user_count = get_nusers(file_path.to_str().expect("invalid utmp path file"));
198-
199197
let upsecs = get_uptime(None);
200-
if upsecs < 0 {
198+
if upsecs >= 0 {
199+
print_uptime(Some(upsecs))?;
200+
} else {
201201
show_error!("couldn't get boot time");
202202
set_exit_code(1);
203203

204204
print!("up ???? days ??:??,");
205-
} else {
206-
print_uptime(Some(upsecs))?;
207205
}
206+
user_count = get_nusers(file_path.to_str().expect("invalid utmp path file"));
208207
}
209208

210-
print_nusers(Some(user_count))?;
209+
print_nusers(Some(user_count));
211210
print_loadavg();
212211

213212
Ok(())
@@ -236,7 +235,7 @@ fn default_uptime(matches: &ArgMatches) -> UResult<()> {
236235

237236
print_time();
238237
print_uptime(None)?;
239-
print_nusers(None)?;
238+
print_nusers(None);
240239
print_loadavg();
241240

242241
Ok(())
@@ -276,7 +275,7 @@ fn process_utmpx(file: Option<&std::ffi::OsString>) -> (Option<time_t>, usize) {
276275
(boot_time, nusers)
277276
}
278277

279-
fn print_nusers(nusers: Option<usize>) -> UResult<()> {
278+
fn print_nusers(nusers: Option<usize>) {
280279
print!(
281280
"{}, ",
282281
match nusers {
@@ -288,7 +287,6 @@ fn print_nusers(nusers: Option<usize>) -> UResult<()> {
288287
}
289288
}
290289
);
291-
Ok(())
292290
}
293291

294292
fn print_time() {

0 commit comments

Comments
 (0)