Skip to main content

wekan_cli/
app.rs

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