-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(master) docstrings and improvements to hooks api
- Loading branch information
1 parent
737ccce
commit 25e64ec
Showing
13 changed files
with
171 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,45 @@ | ||
//! Command output is used by shell builtins as well as shell languages to pass return state of | ||
//! commands or programs. This captures stdout and stderr output, as well as exit code | ||
|
||
use std::{os::unix::process::ExitStatusExt, process::ExitStatus}; | ||
|
||
/// Describes the output of a command | ||
#[derive(Clone, Debug)] | ||
pub struct CmdOutput { | ||
pub stdout: String, | ||
pub stderr: String, | ||
pub status: ExitStatus, | ||
} | ||
|
||
impl CmdOutput { | ||
pub fn new(status: i32) -> Self { | ||
/// Create a new [CmdOutput] with a specified exit code | ||
pub fn from_status(status: i32) -> Self { | ||
CmdOutput { | ||
stdout: String::new(), | ||
stderr: String::new(), | ||
status: ExitStatus::from_raw(status << 8), | ||
} | ||
} | ||
|
||
/// Create a new [CmdOutput] with a successful exit code of 0 | ||
pub fn success() -> Self { | ||
CmdOutput::new(0) | ||
CmdOutput::from_status(0) | ||
} | ||
|
||
/// Create a new [CmdOutput] with an erroneous exit code of 1 | ||
pub fn error() -> Self { | ||
CmdOutput::new(1) | ||
CmdOutput::from_status(1) | ||
} | ||
pub fn error_with_status(status: i32) -> Self { | ||
CmdOutput::new(status) | ||
|
||
// Set the stdout | ||
pub fn stdout<S: ToString>(&mut self, stdout: S) -> &mut Self { | ||
self.stdout = stdout.to_string(); | ||
self | ||
} | ||
pub fn set_output(&mut self, out: String, err: String) { | ||
self.stdout = out; | ||
self.stderr = err; | ||
|
||
// Set the stderr | ||
pub fn stderr<S: ToString>(&mut self, stderr: S) -> &mut Self { | ||
self.stderr = stderr.to_string(); | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.