mirror of https://github.com/chipsenkbeil/distant
Add capabilities support to server and manager
parent
c19df9f538
commit
53fd8d0c4f
@ -0,0 +1,198 @@
|
||||
use super::CapabilityKind;
|
||||
use derive_more::{From, Into, IntoIterator};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
collections::HashSet,
|
||||
hash::{Hash, Hasher},
|
||||
ops::{BitAnd, BitOr, BitXor},
|
||||
str::FromStr,
|
||||
};
|
||||
use strum::{EnumMessage, IntoEnumIterator};
|
||||
|
||||
/// Set of supported capabilities for a server
|
||||
#[derive(Clone, Debug, From, Into, PartialEq, Eq, IntoIterator, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
#[serde(transparent)]
|
||||
pub struct Capabilities(#[into_iterator(owned, ref)] HashSet<Capability>);
|
||||
|
||||
impl Capabilities {
|
||||
/// Return set of capabilities encompassing all possible capabilities
|
||||
pub fn all() -> Self {
|
||||
Self(CapabilityKind::iter().map(Capability::from).collect())
|
||||
}
|
||||
|
||||
/// Return empty set of capabilities
|
||||
pub fn none() -> Self {
|
||||
Self(HashSet::new())
|
||||
}
|
||||
|
||||
/// Returns true if the capability with described kind is included
|
||||
pub fn contains(&self, kind: impl AsRef<str>) -> bool {
|
||||
let cap = Capability {
|
||||
kind: kind.as_ref().to_string(),
|
||||
description: String::new(),
|
||||
};
|
||||
self.0.contains(&cap)
|
||||
}
|
||||
|
||||
/// Adds the specified capability to the set of capabilities
|
||||
///
|
||||
/// * If the set did not have this capability, returns `true`
|
||||
/// * If the set did have this capability, returns `false`
|
||||
pub fn insert(&mut self, cap: impl Into<Capability>) -> bool {
|
||||
self.0.insert(cap.into())
|
||||
}
|
||||
|
||||
/// Removes the capability with the described kind, returning the capability
|
||||
pub fn take(&mut self, kind: impl AsRef<str>) -> Option<Capability> {
|
||||
let cap = Capability {
|
||||
kind: kind.as_ref().to_string(),
|
||||
description: String::new(),
|
||||
};
|
||||
self.0.take(&cap)
|
||||
}
|
||||
|
||||
/// Converts into vec of capabilities sorted by kind
|
||||
pub fn into_sorted_vec(self) -> Vec<Capability> {
|
||||
let mut this = self.0.into_iter().collect::<Vec<_>>();
|
||||
|
||||
this.sort_unstable();
|
||||
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl Capabilities {
|
||||
pub fn root_schema() -> schemars::schema::RootSchema {
|
||||
schemars::schema_for!(Capabilities)
|
||||
}
|
||||
}
|
||||
|
||||
impl BitAnd for &Capabilities {
|
||||
type Output = Capabilities;
|
||||
|
||||
fn bitand(self, rhs: Self) -> Self::Output {
|
||||
Capabilities(self.0.bitand(&rhs.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr for &Capabilities {
|
||||
type Output = Capabilities;
|
||||
|
||||
fn bitor(self, rhs: Self) -> Self::Output {
|
||||
Capabilities(self.0.bitor(&rhs.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr<Capability> for &Capabilities {
|
||||
type Output = Capabilities;
|
||||
|
||||
fn bitor(self, rhs: Capability) -> Self::Output {
|
||||
let mut other = Capabilities::none();
|
||||
other.0.insert(rhs);
|
||||
|
||||
self.bitor(&other)
|
||||
}
|
||||
}
|
||||
|
||||
impl BitXor for &Capabilities {
|
||||
type Output = Capabilities;
|
||||
|
||||
fn bitxor(self, rhs: Self) -> Self::Output {
|
||||
Capabilities(self.0.bitxor(&rhs.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<Capability> for Capabilities {
|
||||
fn from_iter<I: IntoIterator<Item = Capability>>(iter: I) -> Self {
|
||||
let mut this = Capabilities::none();
|
||||
|
||||
for capability in iter {
|
||||
this.0.insert(capability);
|
||||
}
|
||||
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
/// Capability tied to a server. A capability is equivalent based on its kind and not description.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
#[serde(rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub struct Capability {
|
||||
/// Label describing the kind of capability
|
||||
pub kind: String,
|
||||
|
||||
/// Information about the capability
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl Capability {
|
||||
/// Will convert the [`Capability`]'s `kind` into a known [`CapabilityKind`] if possible,
|
||||
/// returning None if the capability is unknown
|
||||
pub fn to_capability_kind(&self) -> Option<CapabilityKind> {
|
||||
CapabilityKind::from_str(&self.kind).ok()
|
||||
}
|
||||
|
||||
/// Returns true if the described capability is unknown
|
||||
pub fn is_unknown(&self) -> bool {
|
||||
self.to_capability_kind().is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Capability {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.kind.eq_ignore_ascii_case(&other.kind)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Capability {}
|
||||
|
||||
impl PartialOrd for Capability {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Capability {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.kind
|
||||
.to_ascii_lowercase()
|
||||
.cmp(&other.kind.to_ascii_lowercase())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Capability {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.kind.to_ascii_lowercase().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CapabilityKind> for Capability {
|
||||
/// Creates a new capability using the kind's default message
|
||||
fn from(kind: CapabilityKind) -> Self {
|
||||
Self {
|
||||
kind: kind.to_string(),
|
||||
description: kind
|
||||
.get_message()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl Capability {
|
||||
pub fn root_schema() -> schemars::schema::RootSchema {
|
||||
schemars::schema_for!(Capability)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl CapabilityKind {
|
||||
pub fn root_schema() -> schemars::schema::RootSchema {
|
||||
schemars::schema_for!(CapabilityKind)
|
||||
}
|
||||
}
|
@ -0,0 +1,203 @@
|
||||
use super::ManagerCapabilityKind;
|
||||
use derive_more::{From, Into, IntoIterator};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
collections::HashSet,
|
||||
hash::{Hash, Hasher},
|
||||
ops::{BitAnd, BitOr, BitXor},
|
||||
str::FromStr,
|
||||
};
|
||||
use strum::{EnumMessage, IntoEnumIterator};
|
||||
|
||||
/// Set of supported capabilities for a manager
|
||||
#[derive(Clone, Debug, From, Into, PartialEq, Eq, IntoIterator, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
#[serde(transparent)]
|
||||
pub struct ManagerCapabilities(#[into_iterator(owned, ref)] HashSet<ManagerCapability>);
|
||||
|
||||
impl ManagerCapabilities {
|
||||
/// Return set of capabilities encompassing all possible capabilities
|
||||
pub fn all() -> Self {
|
||||
Self(
|
||||
ManagerCapabilityKind::iter()
|
||||
.map(ManagerCapability::from)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Return empty set of capabilities
|
||||
pub fn none() -> Self {
|
||||
Self(HashSet::new())
|
||||
}
|
||||
|
||||
/// Returns true if the capability with described kind is included
|
||||
pub fn contains(&self, kind: impl AsRef<str>) -> bool {
|
||||
let cap = ManagerCapability {
|
||||
kind: kind.as_ref().to_string(),
|
||||
description: String::new(),
|
||||
};
|
||||
self.0.contains(&cap)
|
||||
}
|
||||
|
||||
/// Adds the specified capability to the set of capabilities
|
||||
///
|
||||
/// * If the set did not have this capability, returns `true`
|
||||
/// * If the set did have this capability, returns `false`
|
||||
pub fn insert(&mut self, cap: impl Into<ManagerCapability>) -> bool {
|
||||
self.0.insert(cap.into())
|
||||
}
|
||||
|
||||
/// Removes the capability with the described kind, returning the capability
|
||||
pub fn take(&mut self, kind: impl AsRef<str>) -> Option<ManagerCapability> {
|
||||
let cap = ManagerCapability {
|
||||
kind: kind.as_ref().to_string(),
|
||||
description: String::new(),
|
||||
};
|
||||
self.0.take(&cap)
|
||||
}
|
||||
|
||||
/// Converts into vec of capabilities sorted by kind
|
||||
pub fn into_sorted_vec(self) -> Vec<ManagerCapability> {
|
||||
let mut this = self.0.into_iter().collect::<Vec<_>>();
|
||||
|
||||
this.sort_unstable();
|
||||
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl ManagerCapabilities {
|
||||
pub fn root_schema() -> schemars::schema::RootSchema {
|
||||
schemars::schema_for!(ManagerCapabilities)
|
||||
}
|
||||
}
|
||||
|
||||
impl BitAnd for &ManagerCapabilities {
|
||||
type Output = ManagerCapabilities;
|
||||
|
||||
fn bitand(self, rhs: Self) -> Self::Output {
|
||||
ManagerCapabilities(self.0.bitand(&rhs.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr for &ManagerCapabilities {
|
||||
type Output = ManagerCapabilities;
|
||||
|
||||
fn bitor(self, rhs: Self) -> Self::Output {
|
||||
ManagerCapabilities(self.0.bitor(&rhs.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr<ManagerCapability> for &ManagerCapabilities {
|
||||
type Output = ManagerCapabilities;
|
||||
|
||||
fn bitor(self, rhs: ManagerCapability) -> Self::Output {
|
||||
let mut other = ManagerCapabilities::none();
|
||||
other.0.insert(rhs);
|
||||
|
||||
self.bitor(&other)
|
||||
}
|
||||
}
|
||||
|
||||
impl BitXor for &ManagerCapabilities {
|
||||
type Output = ManagerCapabilities;
|
||||
|
||||
fn bitxor(self, rhs: Self) -> Self::Output {
|
||||
ManagerCapabilities(self.0.bitxor(&rhs.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<ManagerCapability> for ManagerCapabilities {
|
||||
fn from_iter<I: IntoIterator<Item = ManagerCapability>>(iter: I) -> Self {
|
||||
let mut this = ManagerCapabilities::none();
|
||||
|
||||
for capability in iter {
|
||||
this.0.insert(capability);
|
||||
}
|
||||
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
/// ManagerCapability tied to a manager. A capability is equivalent based on its kind and not
|
||||
/// description.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
#[serde(rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub struct ManagerCapability {
|
||||
/// Label describing the kind of capability
|
||||
pub kind: String,
|
||||
|
||||
/// Information about the capability
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl ManagerCapability {
|
||||
/// Will convert the [`ManagerCapability`]'s `kind` into a known [`ManagerCapabilityKind`] if
|
||||
/// possible, returning None if the capability is unknown
|
||||
pub fn to_capability_kind(&self) -> Option<ManagerCapabilityKind> {
|
||||
ManagerCapabilityKind::from_str(&self.kind).ok()
|
||||
}
|
||||
|
||||
/// Returns true if the described capability is unknown
|
||||
pub fn is_unknown(&self) -> bool {
|
||||
self.to_capability_kind().is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for ManagerCapability {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.kind.eq_ignore_ascii_case(&other.kind)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for ManagerCapability {}
|
||||
|
||||
impl PartialOrd for ManagerCapability {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for ManagerCapability {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.kind
|
||||
.to_ascii_lowercase()
|
||||
.cmp(&other.kind.to_ascii_lowercase())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for ManagerCapability {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.kind.to_ascii_lowercase().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ManagerCapabilityKind> for ManagerCapability {
|
||||
/// Creates a new capability using the kind's default message
|
||||
fn from(kind: ManagerCapabilityKind) -> Self {
|
||||
Self {
|
||||
kind: kind.to_string(),
|
||||
description: kind
|
||||
.get_message()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl ManagerCapability {
|
||||
pub fn root_schema() -> schemars::schema::RootSchema {
|
||||
schemars::schema_for!(ManagerCapability)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl ManagerCapabilityKind {
|
||||
pub fn root_schema() -> schemars::schema::RootSchema {
|
||||
schemars::schema_for!(ManagerCapabilityKind)
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
use crate::cli::fixtures::*;
|
||||
use assert_cmd::Command;
|
||||
use indoc::indoc;
|
||||
use rstest::*;
|
||||
|
||||
const EXPECTED_TABLE: &str = indoc! {"
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| kind | description |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| capabilities | Supports retrieving capabilities |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| copy | Supports copying files, directories, and symlinks |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| dir_create | Supports creating directory |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| dir_read | Supports reading directory |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| exists | Supports checking if a path exists |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| file_append | Supports appending to binary file |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| file_append_text | Supports appending to text file |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| file_read | Supports reading binary file |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| file_read_text | Supports reading text file |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| file_write | Supports writing binary file |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| file_write_text | Supports writing text file |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| metadata | Supports retrieving metadata about a file, directory, or symlink |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| proc_kill | Supports killing a spawned process |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| proc_resize_pty | Supports resizing the pty of a spawned process |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| proc_spawn | Supports spawning a process |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| proc_stdin | Supports sending stdin to a spawned process |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| remove | Supports removing files, directories, and symlinks |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| rename | Supports renaming files, directories, and symlinks |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| system_info | Supports retrieving system information |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| unwatch | Supports unwatching filesystem for changes |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
| watch | Supports watching filesystem for changes |
|
||||
+------------------+------------------------------------------------------------------+
|
||||
"};
|
||||
|
||||
#[rstest]
|
||||
fn should_output_capabilities(mut action_cmd: CtxCommand<Command>) {
|
||||
// distant action capabilities
|
||||
action_cmd
|
||||
.arg("capabilities")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(EXPECTED_TABLE)
|
||||
.stderr("");
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
use crate::cli::fixtures::*;
|
||||
use indoc::indoc;
|
||||
use rstest::*;
|
||||
|
||||
const EXPECTED_TABLE: &str = indoc! {"
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| kind | description |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| capabilities | Supports retrieving capabilities |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| channel | Supports sending data through a channel with a remote server |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| close_channel | Supports closing a channel with a remote server |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| connect | Supports connecting to remote servers |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| info | Supports retrieving connection-specific information |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| kill | Supports killing a remote connection |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| launch | Supports launching distant on remote servers |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| list | Supports retrieving a list of managed connections |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| open_channel | Supports opening a channel with a remote server |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
| shutdown | Supports being shut down on demand |
|
||||
+---------------+--------------------------------------------------------------+
|
||||
"};
|
||||
|
||||
#[rstest]
|
||||
fn should_output_capabilities(ctx: DistantManagerCtx) {
|
||||
// distant action capabilities
|
||||
ctx.new_assert_cmd(vec!["manager", "capabilities"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(format!("{EXPECTED_TABLE}\n"))
|
||||
.stderr("");
|
||||
}
|
@ -0,0 +1 @@
|
||||
mod capabilities;
|
@ -1,5 +1,6 @@
|
||||
mod action;
|
||||
mod fixtures;
|
||||
mod manager;
|
||||
mod repl;
|
||||
mod scripts;
|
||||
mod utils;
|
||||
|
@ -0,0 +1,33 @@
|
||||
use crate::cli::fixtures::*;
|
||||
use distant_core::data::{Capabilities, Capability};
|
||||
use rstest::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn should_support_json_capabilities(mut json_repl: CtxCommand<Repl>) {
|
||||
let id = rand::random::<u64>().to_string();
|
||||
let req = json!({
|
||||
"id": id,
|
||||
"payload": { "type": "capabilities" },
|
||||
});
|
||||
|
||||
let res = json_repl.write_and_read_json(req).await.unwrap().unwrap();
|
||||
|
||||
assert_eq!(res["origin_id"], id);
|
||||
assert_eq!(res["payload"]["type"], "capabilities");
|
||||
|
||||
let supported: Capabilities = res["payload"]["supported"]
|
||||
.as_array()
|
||||
.expect("Field 'supported' was not an array")
|
||||
.iter()
|
||||
.map(|value| {
|
||||
serde_json::from_value::<Capability>(value.clone())
|
||||
.expect("Could not read array value as capability")
|
||||
})
|
||||
.collect();
|
||||
|
||||
// NOTE: Our local server api should always support all capabilities since it is the reference
|
||||
// implementation for our api
|
||||
assert_eq!(supported, Capabilities::all());
|
||||
}
|
Loading…
Reference in New Issue