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::Rule(args) => {
130 commands::rules::dispatch(
131 args.command,
132 target.client_factory(),
133 &self.credential_store,
134 &self.confirmation,
135 )
136 .await
137 }
138 RootCommand::Integration(args) => {
139 commands::integrations::dispatch(
140 args.command,
141 target.client_factory(),
142 &self.credential_store,
143 &self.secret_input,
144 &self.confirmation,
145 )
146 .await
147 }
148 RootCommand::Dependency(args) => {
149 commands::dependencies::dispatch(
150 args.command,
151 target.client_factory(),
152 &self.credential_store,
153 &self.confirmation,
154 )
155 .await
156 }
157 RootCommand::Attachment(args) => {
158 commands::attachments::dispatch(
159 args.command,
160 target.client_factory(),
161 &self.credential_store,
162 &self.confirmation,
163 )
164 .await
165 }
166 RootCommand::Api(args) => {
167 commands::api::dispatch(
168 args.command,
169 target.client_factory(),
170 &self.credential_store,
171 &self.confirmation,
172 )
173 .await
174 }
175 RootCommand::User(args) => {
176 commands::users::dispatch(
177 args.command,
178 target.client_factory(),
179 &self.credential_store,
180 &self.secret_input,
181 &self.confirmation,
182 )
183 .await
184 }
185 RootCommand::Board(args) => {
186 commands::boards::dispatch(
187 args.command,
188 target.client_factory(),
189 &self.credential_store,
190 &self.confirmation,
191 )
192 .await
193 }
194 RootCommand::List(args) => {
195 commands::lists::dispatch(
196 args.command,
197 target.client_factory(),
198 &self.credential_store,
199 &self.confirmation,
200 )
201 .await
202 }
203 RootCommand::Card(args) => {
204 commands::cards::dispatch(
205 args.command,
206 target.client_factory(),
207 &self.credential_store,
208 &self.confirmation,
209 )
210 .await
211 }
212 RootCommand::Checklist(args) => {
213 commands::checklists::dispatch(
214 args.command,
215 target.client_factory(),
216 &self.credential_store,
217 &self.confirmation,
218 )
219 .await
220 }
221 RootCommand::ChecklistItem(args) => {
222 commands::checklist_items::dispatch(
223 args.command,
224 target.client_factory(),
225 &self.credential_store,
226 &self.confirmation,
227 )
228 .await
229 }
230 RootCommand::CustomField(args) => {
231 commands::custom_fields::dispatch(
232 args.command,
233 target.client_factory(),
234 &self.credential_store,
235 &self.confirmation,
236 )
237 .await
238 }
239 RootCommand::CustomFieldItem(args) => {
240 commands::custom_field_items::dispatch(
241 args.command,
242 target.client_factory(),
243 &self.credential_store,
244 &self.confirmation,
245 )
246 .await
247 }
248 RootCommand::Comment(args) => {
249 commands::comments::dispatch(
250 args.command,
251 target.client_factory(),
252 &self.credential_store,
253 &self.confirmation,
254 )
255 .await
256 }
257 RootCommand::Swimlane(args) => {
258 commands::swimlanes::dispatch(
259 args.command,
260 target.client_factory(),
261 &self.credential_store,
262 &self.confirmation,
263 )
264 .await
265 }
266 RootCommand::Auth(args) => {
267 commands::auth::dispatch(
268 args.command,
269 &mut target,
270 &self.credential_store,
271 &self.secret_input,
272 &self.confirmation,
273 )
274 .await
275 }
276 RootCommand::Profile(_) => {
277 unreachable!("local commands return before target resolution")
278 }
279 };
280 result.map_err(|error| {
281 let error = error.with_profile_context(profile);
282 if policy.allows_initialization() {
283 error.with_profile_state(target.profile_created(), target.profile_active())
284 } else {
285 error
286 }
287 })
288 }
289}
290
291#[cfg(test)]
292mod tests;