Skip to content

Commit

Permalink
Change InteractiveShell's exit_result to result
Browse files Browse the repository at this point in the history
This simplifies the attribute and brings it in line with a standard set
by Broker.
  • Loading branch information
JacobCallahan committed Apr 15, 2024
1 parent 2b7ae5a commit df8e30b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hussh"
version = "0.1.4"
version = "0.1.5"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ print(tf.contents)
# Interactive Shell
If you need to keep a shell open to perform more complex interactions, you can get an `InteractiveShell` instance from the `Connection` class instance.
To use the interactive shell, it is recommended to use the `shell()` context manager from the `Connection` class.
You can send commands to the shell using the `send` method, then get the results from `exit_result` when you exit the context manager.
You can send commands to the shell using the `send` method, then get the results from `result` when you exit the context manager.

```python
with conn.shell() as shell:
shell.send("ls")
shell.send("pwd")
shell.send("whoami")

print(shell.exit_result.stdout)
print(shell.result.stdout)
```
**Note:** The `read` method sends an EOF to the shell, so you won't be able to send more commands after calling `read`. If you want to send more commands, you would need to create a new `InteractiveShell` instance.

Expand Down
14 changes: 7 additions & 7 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! If you don't pass a username, "root" is used.
//!
//! To use the interactive shell, it is recommended to use the shell() context manager from the Connection class.
//! You can send commands to the shell using the `send` method, then get the results from exit_result when you exit the context manager.
//! You can send commands to the shell using the `send` method, then get the results from result when you exit the context manager.
//! Due to the nature of reading from the shell, do not use the `read` method if you want to send more commands.
//!
//! ```python
Expand All @@ -50,7 +50,7 @@
//! shell.send("pwd")
//! shell.send("whoami")
//!
//! print(shell.exit_result.stdout)
//! print(shell.result.stdout)
//! ```
//!
//! Note: The `read` method sends an EOF to the shell, so you won't be able to send more commands after calling `read`. If you want to send more commands, you would need to create a new `InteractiveShell` instance.
Expand Down Expand Up @@ -491,7 +491,7 @@ impl Connection {
/// with conn.shell() as shell:
/// shell.send("ls")
/// shell.send("pwd")
/// print(shell.exit_result.stdout)
/// print(shell.result.stdout)
/// ```
fn shell(&self, pty: Option<bool>) -> PyResult<InteractiveShell> {
let mut channel = self.session.channel_session().unwrap();
Expand All @@ -504,7 +504,7 @@ impl Connection {
Ok(InteractiveShell {
channel: ChannelWrapper { channel },
pty: pty.unwrap_or(false),
exit_result: None,
result: None,
})
}
}
Expand All @@ -521,7 +521,7 @@ struct InteractiveShell {
channel: ChannelWrapper,
pty: bool,
#[pyo3(get)]
exit_result: Option<SSHResult>,
result: Option<SSHResult>,
}

#[pymethods]
Expand All @@ -531,7 +531,7 @@ impl InteractiveShell {
InteractiveShell {
channel,
pty,
exit_result: None,
result: None,
}
}

Expand Down Expand Up @@ -575,7 +575,7 @@ impl InteractiveShell {
if self.pty {
self.send("exit\n".to_string(), Some(false)).unwrap();
}
self.exit_result = Some(self.read());
self.result = Some(self.read());
Ok(())
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ def test_shell_context(conn):
with conn.shell() as sh:
sh.send("echo test shell")
sh.send("bad command")
assert "test shell" in sh.exit_result.stdout
assert "command not found" in sh.exit_result.stderr
assert sh.exit_result.status != 0
assert "test shell" in sh.result.stdout
assert "command not found" in sh.result.stderr
assert sh.result.status != 0


def test_pty_shell_context(conn):
"""Test that we can run multiple commands in a pty shell context."""
with conn.shell(pty=True) as sh:
sh.send("echo test shell")
sh.send("bad command")
assert "test shell" in sh.exit_result.stdout
assert "command not found" in sh.exit_result.stdout
assert sh.exit_result.status != 0
assert "test shell" in sh.result.stdout
assert "command not found" in sh.result.stdout
assert sh.result.status != 0


def test_connection_timeout():
Expand Down

0 comments on commit df8e30b

Please sign in to comment.