Skip to content

Commit

Permalink
Merge pull request #17 from Nerox9/suspend
Browse files Browse the repository at this point in the history
Add suspend and wakeup commands
  • Loading branch information
Nerox9 authored Sep 23, 2024
2 parents 3043389 + 11e4ae1 commit c50f663
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 24 deletions.
57 changes: 33 additions & 24 deletions api/admin/admin.pb.go

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

2 changes: 2 additions & 0 deletions api/admin/admin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ service AdminService {
rpc StopApplication(ApplicationRequest) returns (ApplicationResponse) {}
rpc Poweroff(Empty) returns (Empty) {}
rpc Reboot(Empty) returns (Empty) {}
rpc Suspend(Empty) returns (Empty) {}
rpc Wakeup(Empty) returns (Empty) {}

rpc QueryList(Empty) returns (QueryListResponse) {}
rpc Watch(Empty) returns (stream WatchItem) {}
Expand Down
74 changes: 74 additions & 0 deletions api/admin/admin_grpc.pb.go

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

10 changes: 10 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ impl AdminClient {
let _response = self.connect_to().await?.poweroff(request).await?;
Ok(())
}
pub async fn suspend(&self) -> anyhow::Result<()> {
let request = pb::admin::Empty {};
let _response = self.connect_to().await?.suspend(request).await?;
Ok(())
}
pub async fn wakeup(&self) -> anyhow::Result<()> {
let request = pb::admin::Empty {};
let _response = self.connect_to().await?.wakeup(request).await?;
Ok(())
}

pub async fn query(
&self,
Expand Down
2 changes: 2 additions & 0 deletions nixos/modules/host.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ in
default = [
"reboot.target"
"poweroff.target"
"sleep.target"
"suspend.target"
];
example = "[ 'my-service.service' ]";
};
Expand Down
4 changes: 4 additions & 0 deletions nixos/tests/admin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ in
"[email protected]"
"poweroff.target"
"reboot.target"
"sleep.target"
"suspend.target"
];
tls = mkTls "ghaf-host";
};
Expand Down Expand Up @@ -154,6 +156,8 @@ in
services = [
"poweroff.target"
"reboot.target"
"sleep.target"
"suspend.target"
];
};

Expand Down
24 changes: 24 additions & 0 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,30 @@ impl pb::admin_service_server::AdminService for AdminService {
})
.await
}
async fn suspend(
&self,
request: tonic::Request<Empty>,
) -> std::result::Result<tonic::Response<Empty>, tonic::Status> {
escalate(request, |_| async {
self.inner
.send_system_command(String::from("suspend.target"))
.await?;
Ok(Empty {})
})
.await
}
async fn wakeup(
&self,
request: tonic::Request<Empty>,
) -> std::result::Result<tonic::Response<Empty>, tonic::Status> {
escalate(request, |_| async {
self.inner
.send_system_command(String::from("sleep.target"))
.await?;
Ok(Empty {})
})
.await
}

async fn query_list(
&self,
Expand Down
4 changes: 4 additions & 0 deletions src/bin/givc-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ enum Commands {
},
Reboot {},
Poweroff {},
Suspend {},
Wakeup {},
Query {
#[arg(long, default_value_t = false)]
as_json: bool, // Would it useful for scripts?
Expand Down Expand Up @@ -140,6 +142,8 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
Commands::Resume { app } => admin.resume(app).await?,
Commands::Reboot {} => admin.reboot().await?,
Commands::Poweroff {} => admin.poweroff().await?,
Commands::Suspend {} => admin.suspend().await?,
Commands::Wakeup {} => admin.wakeup().await?,

Commands::Query {
by_type,
Expand Down

0 comments on commit c50f663

Please sign in to comment.