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