aboutsummaryrefslogtreecommitdiff
path: root/mling/src/cli/namespace_mgr.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-04-28 16:18:12 +0800
committer魏曹先生 <1992414357@qq.com>2026-04-28 16:18:12 +0800
commit881e7399b2417c32fa996d94c6b389c1e06d8eb1 (patch)
treefd88cb181e9c5a0bae8677c43dff859f4cd82d80 /mling/src/cli/namespace_mgr.rs
parentdbc811d84fd809ea606a8bbed84b3e78e8cda334 (diff)
Add scaffolding CLI tool `mling`
Diffstat (limited to 'mling/src/cli/namespace_mgr.rs')
-rw-r--r--mling/src/cli/namespace_mgr.rs128
1 files changed, 128 insertions, 0 deletions
diff --git a/mling/src/cli/namespace_mgr.rs b/mling/src/cli/namespace_mgr.rs
new file mode 100644
index 0000000..9781040
--- /dev/null
+++ b/mling/src/cli/namespace_mgr.rs
@@ -0,0 +1,128 @@
+use mingling::{
+ ShellContext, Suggest, SuggestItem,
+ macros::{chain, completion, dispatcher, pack, r_println, renderer, route, suggest},
+ parser::{Picker, Yes},
+};
+
+use crate::{
+ ThisProgram,
+ namespace_manager::{list_namespaces, remove_namespace, set_namespace_trusted},
+};
+
+dispatcher!("trust", TrustNamespaceCommand => TrustNamespaceEntry);
+dispatcher!("untrust", UntrustNamespaceCommand => UntrustNamespaceEntry);
+
+dispatcher!("set-trust", SetTrustNamespaceCommand => SetTrustNamespaceEntry);
+
+dispatcher!("rm-namespace", RemoveNamespaceCommand => RemoveNamespaceEntry);
+
+pack!(ErrorNamespaceNotProvided = ());
+pack!(ResultNamespaceTrustChanged = ());
+pack!(ResultNamespaceRemoved = ());
+
+#[completion(TrustNamespaceEntry)]
+pub(crate) fn comp_trust(ctx: &ShellContext) -> Suggest {
+ if ctx.previous_word == "trust" {
+ return Suggest::Suggest(
+ list_namespaces(false, true, true)
+ .into_iter()
+ .map(|i| SuggestItem::new(i))
+ .collect::<std::collections::BTreeSet<_>>(),
+ );
+ }
+ return suggest!();
+}
+
+#[completion(UntrustNamespaceEntry)]
+pub(crate) fn comp_untrust(ctx: &ShellContext) -> Suggest {
+ if ctx.previous_word == "untrust" {
+ return Suggest::Suggest(
+ list_namespaces(true, false, true)
+ .into_iter()
+ .map(|i| SuggestItem::new(i))
+ .collect::<std::collections::BTreeSet<_>>(),
+ );
+ }
+ return suggest!();
+}
+
+#[completion(SetTrustNamespaceEntry)]
+pub(crate) fn comp_set_trust(ctx: &ShellContext) -> Suggest {
+ if ctx.typing_argument() {
+ return suggest!(
+ "-t": "Whether to trust this namespace",
+ "--trusted": "Whether to trust this namespace",
+ );
+ }
+ if ctx.filling_argument_first(["-t", "--trusted"]) {
+ return suggest!("yes", "no");
+ }
+ if ctx.previous_word == "set-trust" {
+ return Suggest::Suggest(
+ list_namespaces(true, true, true)
+ .into_iter()
+ .map(|i| SuggestItem::new(i))
+ .collect::<std::collections::BTreeSet<_>>(),
+ );
+ }
+ return suggest!();
+}
+
+#[completion(RemoveNamespaceEntry)]
+pub(crate) fn comp_remove_namespace(ctx: &ShellContext) -> Suggest {
+ if ctx.previous_word == "rm-namespace" {
+ return Suggest::Suggest(
+ list_namespaces(true, true, true)
+ .into_iter()
+ .map(|i| SuggestItem::new(i))
+ .collect::<std::collections::BTreeSet<_>>(),
+ );
+ }
+ return suggest!();
+}
+
+#[chain]
+pub(crate) fn handle_set_trust(p: SetTrustNamespaceEntry) -> NextProcess {
+ let (trusted, namespace) = route!(
+ Picker::new(p.inner)
+ .pick::<Yes>(["-t", "--trusted"])
+ .pick_or_route((), ErrorNamespaceNotProvided::default().to_render())
+ .unpack()
+ );
+ set_namespace_trusted(namespace, trusted.is_yes());
+ ResultNamespaceTrustChanged::default().to_render()
+}
+
+#[chain]
+pub(crate) fn handle_trust(p: TrustNamespaceEntry) -> NextProcess {
+ SetTrustNamespaceEntry::new({
+ let mut args = p.inner.clone();
+ args.extend(vec!["-t".to_string(), "yes".to_string()]);
+ args
+ })
+}
+
+#[chain]
+pub(crate) fn handle_untrust(p: UntrustNamespaceEntry) -> NextProcess {
+ SetTrustNamespaceEntry::new({
+ let mut args = p.inner.clone();
+ args.extend(vec!["-t".to_string(), "no".to_string()]);
+ args
+ })
+}
+
+#[chain]
+pub(crate) fn handle_remove_namespace(p: RemoveNamespaceEntry) -> NextProcess {
+ let namespace = route!(
+ Picker::new(p.inner)
+ .pick_or_route((), ErrorNamespaceNotProvided::default().to_render())
+ .unpack()
+ );
+ remove_namespace(namespace);
+ ResultNamespaceRemoved::default().to_render()
+}
+
+#[renderer]
+pub(crate) fn render_error_namespace_not_provided(_prev: ErrorNamespaceNotProvided) {
+ r_println!("Error: no namespace was provided!")
+}