Skip to content

Commit

Permalink
Add auto_sudo feature
Browse files Browse the repository at this point in the history
Cargo clippy
  • Loading branch information
JojiiOfficial committed Oct 19, 2020
1 parent deda0d2 commit 7558c68
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ serde_derive = "1.0.117"
serde_yaml = "0.8.13"
clap = "3.0.0-beta.2"
clap_generate = "3.0.0-beta.2"
sudo = {version = "0.5.0", optional = true}

[features]
default = ["auto_sudo"]
auto_sudo = ["sudo"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The runit sv command rewritten in rust with nice new features.
- Listing services
- Custom timeout
- Much cleaner code than the original sv command
- Automatically use sudo (like yay) (Planned, not yet implemented)
- Automatically use sudo if ran as user (feature: `auto_sudo`. Used by default)

# Installation

Expand Down Expand Up @@ -83,4 +83,4 @@ sudo rsv start cupsd # start cupsd service (enable if service is disabled)
# TODO
- [x] Listing services
- [x] Shell completion
- [ ] Auto sudo
- [x] Auto sudo
7 changes: 5 additions & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub fn run(app: &ArgMatches) -> Result<String, Box<dyn error::Error>> {
// Get current subcommand
let (subcommand, matches) = app
.subcommand()
.ok_or("No subcommand provided".to_owned())?;
.ok_or_else(|| "No subcommand provided".to_owned())?;

#[cfg(feature = "auto_sudo")]
sudo::escalate_if_needed()?;

if subcommand == "list" {
return run_list_command(config, matches);
Expand Down Expand Up @@ -85,7 +88,7 @@ fn format_services(services: Vec<Service>) -> String {
return format!("{}", err);
}

s.push_str(format!("{}", item.format_status(status.unwrap())).as_str());
s.push_str(item.format_status(status.unwrap()).as_str());
}

s
Expand Down
2 changes: 1 addition & 1 deletion src/sv/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Error {
Error::ServiceAlreadyEnabled(name) => format!("Service '{}' already enabled", name),
Error::ServiceAlreadyDisabled(name) => format!("Service '{}' already disabled", name),
Error::IoError(err) => format!("{}", err),
Error::ForceKilled() => format!("killed"),
Error::ForceKilled() => "killed".to_string(),
}
}
}
2 changes: 1 addition & 1 deletion src/sv/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Service {

let service_uri = item.unwrap().file_name().into_string().unwrap();

if services.iter().find(|s| s.uri == service_uri).is_some() {
if services.iter().any(|s| s.uri == service_uri) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/sv/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ impl ServiceState {

impl ServiceStatus {
pub fn no_state_available() -> ServiceStatus {
return ServiceStatus {
ServiceStatus {
pid: 0,
time: Duration::from_secs(0),
state: ServiceState::Down,
normallyup: false,
paused: false,
want: Wants::NoWant,
term: false,
};
}
}

pub fn new(service: &Service, buff: [u8; 20]) -> Result<ServiceStatus, Error> {
Expand Down

0 comments on commit 7558c68

Please sign in to comment.