nuts_tool/cli/plugin.rs
1// MIT License
2//
3// Copyright (c) 2024 Robin Doer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22
23pub mod add;
24pub mod info;
25pub mod list;
26pub mod modify;
27pub mod remove;
28
29use anyhow::Result;
30use clap::{Args, Subcommand};
31use std::os::fd::RawFd;
32use std::path::PathBuf;
33
34use crate::cli::plugin::add::PluginAddArgs;
35use crate::cli::plugin::info::PluginInfoArgs;
36use crate::cli::plugin::list::PluginListArgs;
37use crate::cli::plugin::modify::PluginModifyArgs;
38use crate::cli::plugin::remove::PluginRemoveArgs;
39
40#[derive(Debug, Args)]
41#[clap(args_conflicts_with_subcommands = true, subcommand_required = true)]
42pub struct PluginArgs {
43 #[clap(subcommand)]
44 command: Option<PluginCommand>,
45
46 #[clap(long, hide = true)]
47 password_from_fd: Option<RawFd>,
48
49 #[clap(long, hide = true)]
50 password_from_file: Option<PathBuf>,
51}
52
53impl PluginArgs {
54 pub fn run(&self) -> Result<()> {
55 self.command
56 .as_ref()
57 .map_or(Ok(()), |command| command.run())
58 }
59}
60
61#[derive(Debug, Subcommand)]
62pub enum PluginCommand {
63 /// Assigns a new plugin
64 Add(PluginAddArgs),
65
66 /// Assigns a new plugin
67 Modify(PluginModifyArgs),
68
69 /// Removes a plugin again
70 Remove(PluginRemoveArgs),
71
72 /// Prints information about a plugin
73 Info(PluginInfoArgs),
74
75 /// Lists all configured plugins
76 List(PluginListArgs),
77}
78
79impl PluginCommand {
80 pub fn run(&self) -> Result<()> {
81 match self {
82 Self::Add(args) => args.run(),
83 Self::Modify(args) => args.run(),
84 Self::Remove(args) => args.run(),
85 Self::Info(args) => args.run(),
86 Self::List(args) => args.run(),
87 }
88 }
89}