Deployment and lifecycle management for Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cleanup commands, use inherited io, clean reboot

+22 -31
+22 -31
cli/src/sower.rs
··· 1 1 use clap::ValueEnum; 2 2 use serde::Deserialize; 3 3 use std::fs; 4 - use std::io::{self, Write}; 5 4 use std::process::Command; 6 5 use strum::{Display, VariantNames}; 7 6 ··· 16 15 17 16 impl Seed { 18 17 pub fn realize(&self) -> Result<&Self, String> { 19 - let output = Command::new("nix-store") 20 - .args(["--realize", &self.out_path]) 21 - .output() 22 - .unwrap_or_else(|_| panic!("failed to realize {:?}", &self.out_path)); 23 - 24 - io::stdout().write_all(&output.stdout).unwrap(); 25 - io::stderr().write_all(&output.stderr).unwrap(); 26 - 27 - match output.status.success() { 18 + match run_command( 19 + "nix-store".to_string(), 20 + vec!["--realize".to_string(), self.out_path.clone()], 21 + ) { 28 22 true => Ok(self), 29 23 false => Err(format!("failed to realize: {}", &self.out_path)), 30 24 } ··· 39 33 } 40 34 41 35 fn activate_generic(&self) -> Result<&Self, String> { 42 - let result = &mut Command::new(format!("{}/activate", &self.out_path)); 43 - 44 - let output = result.output().expect("failed to get output"); 45 - 46 - let _exit_code = result.status().expect("failed to set system profile"); 47 - 48 - io::stdout().write_all(&output.stdout).unwrap(); 49 - io::stderr().write_all(&output.stderr).unwrap(); 50 - 51 - match output.status.success() { 36 + match run_command(format!("{}/activate", &self.out_path), vec![]) { 52 37 true => Ok(self), 53 38 false => Err(format!("failed to realize: {}", &self.out_path)), 54 39 } ··· 162 147 } 163 148 164 149 pub fn run_reboot() { 165 - println!("Rebooting") 150 + run_command( 151 + "systemd-run".to_string(), 152 + vec![ 153 + "--on-active=5s".to_string(), 154 + "--no-block".to_string(), 155 + "--unit=sower-tree-reboot".to_string(), 156 + "systemctl".to_string(), 157 + "reboot".to_string(), 158 + ], 159 + ); 160 + println!("Rebooting in ~5 seconds"); 161 + std::process::exit(0) 166 162 } 167 163 } 168 164 169 165 fn run_command(command: String, args: Vec<String>) -> bool { 170 - let command = &mut Command::new(command); 171 - let result = command.args(args); 172 - 173 - let output = result.output().expect("failed to get output"); 174 - 175 - let _exit_code = result.status().expect("failed to set system profile"); 176 - 177 - io::stdout().write_all(&output.stdout).unwrap(); 178 - io::stderr().write_all(&output.stderr).unwrap(); 166 + let status = &mut Command::new(command) 167 + .args(args) 168 + .status() 169 + .expect("failed to execute command"); 179 170 180 - output.status.success() 171 + status.success() 181 172 }