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::Api(args) => {
130                commands::api::dispatch(
131                    args.command,
132                    target.client_factory(),
133                    &self.credential_store,
134                    &self.confirmation,
135                )
136                .await
137            }
138            RootCommand::User(args) => {
139                commands::users::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::Board(args) => {
149                commands::boards::dispatch(
150                    args.command,
151                    target.client_factory(),
152                    &self.credential_store,
153                    &self.confirmation,
154                )
155                .await
156            }
157            RootCommand::List(args) => {
158                commands::lists::dispatch(
159                    args.command,
160                    target.client_factory(),
161                    &self.credential_store,
162                    &self.confirmation,
163                )
164                .await
165            }
166            RootCommand::Card(args) => {
167                commands::cards::dispatch(
168                    args.command,
169                    target.client_factory(),
170                    &self.credential_store,
171                    &self.confirmation,
172                )
173                .await
174            }
175            RootCommand::Checklist(args) => {
176                commands::checklists::dispatch(
177                    args.command,
178                    target.client_factory(),
179                    &self.credential_store,
180                    &self.confirmation,
181                )
182                .await
183            }
184            RootCommand::ChecklistItem(args) => {
185                commands::checklist_items::dispatch(
186                    args.command,
187                    target.client_factory(),
188                    &self.credential_store,
189                    &self.confirmation,
190                )
191                .await
192            }
193            RootCommand::CustomField(args) => {
194                commands::custom_fields::dispatch(
195                    args.command,
196                    target.client_factory(),
197                    &self.credential_store,
198                    &self.confirmation,
199                )
200                .await
201            }
202            RootCommand::CustomFieldItem(args) => {
203                commands::custom_field_items::dispatch(
204                    args.command,
205                    target.client_factory(),
206                    &self.credential_store,
207                    &self.confirmation,
208                )
209                .await
210            }
211            RootCommand::Comment(args) => {
212                commands::comments::dispatch(
213                    args.command,
214                    target.client_factory(),
215                    &self.credential_store,
216                    &self.confirmation,
217                )
218                .await
219            }
220            RootCommand::Swimlane(args) => {
221                commands::swimlanes::dispatch(
222                    args.command,
223                    target.client_factory(),
224                    &self.credential_store,
225                    &self.confirmation,
226                )
227                .await
228            }
229            RootCommand::Auth(args) => {
230                commands::auth::dispatch(
231                    args.command,
232                    &mut target,
233                    &self.credential_store,
234                    &self.secret_input,
235                    &self.confirmation,
236                )
237                .await
238            }
239            RootCommand::Profile(_) => {
240                unreachable!("local commands return before target resolution")
241            }
242        };
243        result.map_err(|error| {
244            let error = error.with_profile_context(profile);
245            if policy.allows_initialization() {
246                error.with_profile_state(target.profile_created(), target.profile_active())
247            } else {
248                error
249            }
250        })
251    }
252}
253
254#[cfg(test)]
255mod tests;