Struct Shell
pub struct Shell { /* private fields */ }
A Shell is the main API entry point.
Almost all of the crate's functionality is available as methods of the
Shell object.
Shell is a stateful object. It maintains a logical working directory and
an environment map. They are independent from process's
std::env::current_dir and std::env::var, and only affect paths and
commands passed to the Shell.
By convention, variable holding the shell is named sh.
Example
use xshell::{cmd, Shell};
let sh = Shell::new()?;
let _d = sh.push_dir("./target");
let cwd = sh.current_dir();
cmd!(sh, "echo current dir is {cwd}").run()?;
let process_cwd = std::env::current_dir().unwrap();
assert_eq!(cwd, process_cwd.join("./target"));
# Ok::<(), xshell::Error>(())
Implementations
impl Shell
fn new() -> Result<Shell>Creates a new
Shell.Fails if
std::env::current_dirreturns an error.fn current_dir(&self) -> PathBufReturns the working directory for this
Shell.All relative paths are interpreted relative to this directory, rather than
std::env::current_dir.fn change_dir<P: AsRef<Path>>(&self, dir: P)Changes the working directory for this
Shell.Note that this doesn't affect
std::env::current_dir.fn push_dir<P: AsRef<Path>>(&self, path: P) -> PushDir<'_>Temporary changes the working directory of this
Shell.Returns a RAII guard which reverts the working directory to the old value when dropped.
Note that this doesn't affect
std::env::current_dir.fn var<K: AsRef<OsStr>>(&self, key: K) -> Result<String>Fetches the environmental variable
keyfor thisShell.Returns an error if the variable is not set, or set to a non-utf8 value.
Environment of the
Shellaffects all commands spawned via this shell.fn var_os<K: AsRef<OsStr>>(&self, key: K) -> Option<OsString>Fetches the environmental variable
keyfor thisShellasOsStringReturnsNoneif the variable is not set.Environment of the
Shellaffects all commands spawned via this shell.fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(&self, key: K, val: V)Sets the value of
keyenvironment variable for thisShelltoval.Note that this doesn't affect
std::env::var.fn push_env<K: AsRef<OsStr>, V: AsRef<OsStr>>(&self, key: K, val: V) -> PushEnv<'_>Temporary sets the value of
keyenvironment variable for thisShelltoval.Returns a RAII guard which restores the old environment when dropped.
Note that this doesn't affect
std::env::var.fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<String>Read the entire contents of a file into a string.
fn read_binary_file<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>>Read the entire contents of a file into a vector of bytes.
fn read_dir<P: AsRef<Path>>(&self, path: P) -> Result<Vec<PathBuf>>Returns a sorted list of paths directly contained in the directory at
path.fn write_file<P: AsRef<Path>, C: AsRef<[u8]>>(&self, path: P, contents: C) -> Result<()>Write a slice as the entire contents of a file.
This function will create the file and all intermediate directories if they don't exist.
fn copy_file<S: AsRef<Path>, D: AsRef<Path>>(&self, src: S, dst: D) -> Result<()>Copies
srcintodst.srcmust be a file, butdstneed not be. Ifdstis an existing directory,srcwill be copied into a file in thedstdirectory whose name is same as that ofsrc.Otherwise,
dstis a file or does not exist, andsrcwill be copied into it.fn hard_link<S: AsRef<Path>, D: AsRef<Path>>(&self, src: S, dst: D) -> Result<()>Hardlinks
srctodst.fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>Creates the specified directory.
All intermediate directories will also be created.
fn create_temp_dir(&self) -> Result<TempDir>Creates an empty named world-readable temporary directory.
Returns a
TempDirRAII guard with the path to the directory. When dropped, the temporary directory and all of its contents will be removed.Note that this is an insecure method -- any other process on the system will be able to read the data.
fn remove_path<P: AsRef<Path>>(&self, path: P) -> Result<()>Removes the file or directory at the given path.
fn path_exists<P: AsRef<Path>>(&self, path: P) -> boolReturns whether a file or directory exists at the given path.
fn cmd<P: AsRef<Path>>(&self, program: P) -> Cmd<'_>Creates a new
Cmdthat executes the givenprogram.
Trait Implementations
impl Clone for Shell
fn clone(&self) -> Self
impl Debug for Shell
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl RefUnwindSafe for Shell
impl UnwindSafe for Shell
Auto Trait Implementations
impl !Freeze for Shell
impl !Sync for Shell
impl Send for Shell
impl Unpin for Shell
impl UnsafeUnpin for Shell
Blanket Implementations
impl<T> Any for Shell
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Shell
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Shell
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Shell
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Shell
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Shell
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Shell
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Shell
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Shell
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>