forked from aws/amazon-q-developer-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.rs
More file actions
159 lines (144 loc) · 4.59 KB
/
Copy pathcli.rs
File metadata and controls
159 lines (144 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::collections::HashMap;
use clap::{
Args,
Parser,
Subcommand,
ValueEnum,
};
#[derive(Debug, Clone, PartialEq, Eq, Default, Parser)]
pub struct Chat {
/// (Deprecated, use --trust-all-tools) Enabling this flag allows the model to execute
/// all commands without first accepting them.
#[arg(short, long, hide = true)]
pub accept_all: bool,
/// Print the first response to STDOUT without interactive mode. This will fail if the
/// prompt requests permissions to use a tool, unless --trust-all-tools is also used.
#[arg(long)]
pub no_interactive: bool,
/// Start a new conversation and overwrites any previous conversation from this directory.
#[arg(long)]
pub new: bool,
/// The first question to ask
pub input: Option<String>,
/// Context profile to use
#[arg(long = "profile")]
pub profile: Option<String>,
/// Allows the model to use any tool to run commands without asking for confirmation.
#[arg(long)]
pub trust_all_tools: bool,
/// Trust only this set of tools. Example: trust some tools:
/// '--trust-tools=fs_read,fs_write', trust no tools: '--trust-tools='
#[arg(long, value_delimiter = ',', value_name = "TOOL_NAMES")]
pub trust_tools: Option<Vec<String>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Subcommand)]
pub enum Mcp {
/// Add or replace a configured server
Add(McpAdd),
/// Remove a server from the MCP configuration
#[command(alias = "rm")]
Remove(McpRemove),
/// List configured servers
List(McpList),
/// Import a server configuration from another file
Import(McpImport),
/// Get the status of a configured server
Status {
#[arg(long)]
name: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Args)]
pub struct McpAdd {
/// Name for the server
#[arg(long)]
pub name: String,
/// The command used to launch the server
#[arg(long)]
pub command: String,
/// Where to add the server to. For profile scope, the name of the profile must specified with
/// --profile.
#[arg(long, value_enum)]
pub scope: Option<Scope>,
/// Name of the profile to add the server config to. Not compatible with workspace scope or
/// global scope.
#[arg(long)]
pub profile: Option<String>,
/// Environment variables to use when launching the server
#[arg(long, value_parser = parse_env_vars)]
pub env: Vec<HashMap<String, String>>,
/// Server launch timeout, in milliseconds
#[arg(long)]
pub timeout: Option<u64>,
/// Overwrite an existing server with the same name
#[arg(long, default_value_t = false)]
pub force: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Args)]
pub struct McpRemove {
#[arg(long)]
pub name: String,
#[arg(long, value_enum)]
pub scope: Option<Scope>,
#[arg(long)]
pub profile: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Args)]
pub struct McpList {
#[arg(value_enum)]
pub scope: Option<Scope>,
#[arg(long)]
pub profile: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Args)]
pub struct McpImport {
#[arg(long)]
pub file: String,
#[arg(value_enum)]
pub scope: Option<Scope>,
#[arg(long)]
pub profile: Option<String>,
/// Overwrite an existing server with the same name
#[arg(long, default_value_t = false)]
pub force: bool,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum Scope {
Workspace,
Profile,
Global,
}
impl std::fmt::Display for Scope {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Scope::Workspace => write!(f, "workspace"),
Scope::Profile => write!(f, "profile"),
Scope::Global => write!(f, "global"),
}
}
}
#[derive(Debug)]
struct EnvVarParseError(String);
impl std::fmt::Display for EnvVarParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Failed to parse environment variables: {}", self.0)
}
}
impl std::error::Error for EnvVarParseError {}
fn parse_env_vars(arg: &str) -> Result<HashMap<String, String>, EnvVarParseError> {
let mut vars = HashMap::new();
for pair in arg.split(",") {
match pair.split_once('=') {
Some((key, value)) => {
vars.insert(key.trim().to_string(), value.trim().to_string());
},
None => {
return Err(EnvVarParseError(format!(
"Invalid environment variable '{}'. Expected 'name=value'",
pair
)));
},
}
}
Ok(vars)
}