Struct xshell::Shell

source ·
pub struct Shell { /* private fields */ }
Expand description

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"));

Implementations§

source§

impl Shell

source

pub fn new() -> Result<Shell>

Creates a new Shell.

Fails if std::env::current_dir returns an error.

source

pub fn current_dir(&self) -> PathBuf

Returns the working directory for this Shell.

All relative paths are interpreted relative to this directory, rather than std::env::current_dir.

source

pub 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.

source

pub 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.

source

pub fn var<K: AsRef<OsStr>>(&self, key: K) -> Result<String>

Fetches the environmental variable key for this Shell.

Returns an error if the variable is not set, or set to a non-utf8 value.

Environment of the Shell affects all commands spawned via this shell.

source

pub fn var_os<K: AsRef<OsStr>>(&self, key: K) -> Option<OsString>

Fetches the environmental variable key for this Shell as OsString Returns None if the variable is not set.

Environment of the Shell affects all commands spawned via this shell.

source

pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(&self, key: K, val: V)

Sets the value of key environment variable for this Shell to val.

Note that this doesn’t affect std::env::var.

source

pub fn push_env<K: AsRef<OsStr>, V: AsRef<OsStr>>( &self, key: K, val: V ) -> PushEnv<'_>

Temporary sets the value of key environment variable for this Shell to val.

Returns a RAII guard which restores the old environment when dropped.

Note that this doesn’t affect std::env::var.

source

pub fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<String>

Read the entire contents of a file into a string.

source

pub 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.

source

pub 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.

source

pub 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.

source

pub fn copy_file<S: AsRef<Path>, D: AsRef<Path>>( &self, src: S, dst: D ) -> Result<()>

Copies src into dst.

src must be a file, but dst need not be. If dst is an existing directory, src will be copied into a file in the dst directory whose name is same as that of src.

Otherwise, dst is a file or does not exist, and src will be copied into it.

Hardlinks src to dst.

source

pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf>

Creates the specified directory.

All intermediate directories will also be created.

source

pub fn create_temp_dir(&self) -> Result<TempDir>

Creates an empty named world-readable temporary directory.

Returns a TempDir RAII 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.

source

pub fn remove_path<P: AsRef<Path>>(&self, path: P) -> Result<()>

Removes the file or directory at the given path.

source

pub fn path_exists<P: AsRef<Path>>(&self, path: P) -> bool

Returns whether a file or directory exists at the given path.

source

pub fn cmd<P: AsRef<Path>>(&self, program: P) -> Cmd<'_>

Creates a new Cmd that executes the given program.

Trait Implementations§

source§

impl Clone for Shell

source§

fn clone(&self) -> Shell

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Shell

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl RefUnwindSafe for Shell

source§

impl UnwindSafe for Shell

Auto Trait Implementations§

§

impl !Freeze for Shell

§

impl Send for Shell

§

impl !Sync for Shell

§

impl Unpin for Shell

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.