1use crate::{
2 command_result::CommandSuccess,
3 commands::{self, RootCommand},
4 config::{ServerSelection, TargetResolver, profiles::FileProfileStore},
5 credentials::{
6 CredentialStore, KeyringCredentialStore, SecretInputProvider, SystemSecretInputProvider,
7 },
8 error::AppError,
9 input::{ConfirmationProvider, SystemConfirmationProvider},
10};
11
12pub struct App<S, P, F = FileProfileStore, C = SystemConfirmationProvider> {
13 target_resolver: TargetResolver,
14 credential_store: S,
15 secret_input: P,
16 profile_store: F,
17 confirmation: C,
18}
19
20impl
21 App<
22 KeyringCredentialStore,
23 SystemSecretInputProvider,
24 FileProfileStore,
25 SystemConfirmationProvider,
26 >
27{
28 pub fn production(selection: ServerSelection) -> Self {
29 Self::production_with_confirmation_interactivity(selection, true)
30 }
31
32 pub fn production_with_confirmation_interactivity(
33 selection: ServerSelection,
34 allow_interactive_confirmation: bool,
35 ) -> Self {
36 Self::with_dependencies(
37 selection,
38 KeyringCredentialStore,
39 SystemSecretInputProvider,
40 FileProfileStore::production(),
41 SystemConfirmationProvider::for_output(allow_interactive_confirmation),
42 )
43 }
44}
45
46impl<S, P, F> App<S, P, F, SystemConfirmationProvider>
47where
48 S: CredentialStore,
49 P: SecretInputProvider,
50 F: crate::config::profiles::ProfileStore,
51{
52 pub const fn with_profile_store(
53 selection: ServerSelection,
54 credential_store: S,
55 secret_input: P,
56 profile_store: F,
57 ) -> Self {
58 Self::with_dependencies(
59 selection,
60 credential_store,
61 secret_input,
62 profile_store,
63 SystemConfirmationProvider::interactive(),
64 )
65 }
66}
67
68impl<S, P, F, C> App<S, P, F, C>
69where
70 S: CredentialStore,
71 P: SecretInputProvider,
72 F: crate::config::profiles::ProfileStore,
73 C: ConfirmationProvider,
74{
75 pub const fn with_dependencies(
76 selection: ServerSelection,
77 credential_store: S,
78 secret_input: P,
79 profile_store: F,
80 confirmation: C,
81 ) -> Self {
82 Self {
83 target_resolver: TargetResolver::new(selection),
84 credential_store,
85 secret_input,
86 profile_store,
87 confirmation,
88 }
89 }
90
91 pub fn with_confirmation_provider<C2>(self, confirmation: C2) -> App<S, P, F, C2>
92 where
93 C2: ConfirmationProvider,
94 {
95 App {
96 target_resolver: self.target_resolver,
97 credential_store: self.credential_store,
98 secret_input: self.secret_input,
99 profile_store: self.profile_store,
100 confirmation,
101 }
102 }
103
104 pub const fn credential_store(&self) -> &S {
105 &self.credential_store
106 }
107
108 pub async fn execute(&self, command: RootCommand) -> Result<CommandSuccess, AppError> {
109 if let RootCommand::Profile(args) = command {
110 if self.target_resolver.has_explicit_target() {
111 return Err(AppError::invalid_input(
112 "--server and --profile cannot be used with profile-management commands",
113 ));
114 }
115 return commands::profile::dispatch(
116 args.command,
117 &self.profile_store,
118 &self.credential_store,
119 &self.confirmation,
120 );
121 }
122 let policy = match &command {
123 RootCommand::Auth(args) => args.command.missing_profile_resolution(),
124 _ => crate::config::MissingProfileResolution::Reject,
125 };
126 let mut target = self.target_resolver.resolve(&self.profile_store, policy)?;
127 let profile = target.profile().to_owned();
128 let result = match command {
129 RootCommand::Admin(args) => {
130 commands::admin::dispatch(
131 args.command,
132 target.client_factory(),
133 &self.credential_store,
134 &self.secret_input,
135 &self.confirmation,
136 )
137 .await
138 }
139 RootCommand::Rule(args) => {
140 commands::rules::dispatch(
141 args.command,
142 target.client_factory(),
143 &self.credential_store,
144 &self.confirmation,
145 )
146 .await
147 }
148 RootCommand::Integration(args) => {
149 commands::integrations::dispatch(
150 args.command,
151 target.client_factory(),
152 &self.credential_store,
153 &self.secret_input,
154 &self.confirmation,
155 )
156 .await
157 }
158 RootCommand::Dependency(args) => {
159 commands::dependencies::dispatch(
160 args.command,
161 target.client_factory(),
162 &self.credential_store,
163 &self.confirmation,
164 )
165 .await
166 }
167 RootCommand::Attachment(args) => {
168 commands::attachments::dispatch(
169 args.command,
170 target.client_factory(),
171 &self.credential_store,
172 &self.confirmation,
173 )
174 .await
175 }
176 RootCommand::Api(args) => {
177 commands::api::dispatch(
178 args.command,
179 target.client_factory(),
180 &self.credential_store,
181 &self.confirmation,
182 )
183 .await
184 }
185 RootCommand::User(args) => {
186 commands::users::dispatch(
187 args.command,
188 target.client_factory(),
189 &self.credential_store,
190 &self.secret_input,
191 &self.confirmation,
192 )
193 .await
194 }
195 RootCommand::Board(args) => {
196 commands::boards::dispatch(
197 args.command,
198 target.client_factory(),
199 &self.credential_store,
200 &self.confirmation,
201 )
202 .await
203 }
204 RootCommand::List(args) => {
205 commands::lists::dispatch(
206 args.command,
207 target.client_factory(),
208 &self.credential_store,
209 &self.confirmation,
210 )
211 .await
212 }
213 RootCommand::Card(args) => {
214 commands::cards::dispatch(
215 args.command,
216 target.client_factory(),
217 &self.credential_store,
218 &self.confirmation,
219 )
220 .await
221 }
222 RootCommand::Checklist(args) => {
223 commands::checklists::dispatch(
224 args.command,
225 target.client_factory(),
226 &self.credential_store,
227 &self.confirmation,
228 )
229 .await
230 }
231 RootCommand::CustomField(args) => {
232 commands::custom_fields::dispatch(
233 args.command,
234 target.client_factory(),
235 &self.credential_store,
236 &self.confirmation,
237 )
238 .await
239 }
240 RootCommand::Comment(args) => {
241 commands::comments::dispatch(
242 args.command,
243 target.client_factory(),
244 &self.credential_store,
245 &self.confirmation,
246 )
247 .await
248 }
249 RootCommand::Swimlane(args) => {
250 commands::swimlanes::dispatch(
251 args.command,
252 target.client_factory(),
253 &self.credential_store,
254 &self.confirmation,
255 )
256 .await
257 }
258 RootCommand::Auth(args) => {
259 commands::auth::dispatch(
260 args.command,
261 &mut target,
262 &self.credential_store,
263 &self.secret_input,
264 &self.confirmation,
265 )
266 .await
267 }
268 RootCommand::Profile(_) => {
269 unreachable!("local commands return before target resolution")
270 }
271 };
272 result.map_err(|error| {
273 let error = error.with_profile_context(profile);
274 if policy.allows_initialization() {
275 error.with_profile_state(target.profile_created(), target.profile_active())
276 } else {
277 error
278 }
279 })
280 }
281}
282
283#[cfg(test)]
284mod tests;