1 use crate::{cli::Commands, create_auth_app, delete_auth_app, get_auth_app, get_auth_apps};
2 use anyhow::{anyhow, Result};
3 use console::style;
4 use dialoguer::{theme::ColorfulTheme, Confirm, Input};
5 use smbpndk_model::{AuthAppCreate, CommandResult};
6 use smbpndk_utils::{get_config, write_config};
7 use spinners::Spinner;
8
9 pub async fn process_auth_app(commands: Commands) -> Result<CommandResult> {
10 match commands {
11 Commands::New {} => {
12 let app_name = Input::<String>::with_theme(&ColorfulTheme::default())
13 .with_prompt("App name")
14 .interact()
15 .unwrap();
16 let description = Input::<String>::with_theme(&ColorfulTheme::default())
17 .with_prompt("Description")
18 .interact()
19 .unwrap();
20
21 let spinner = Spinner::new(
22 spinners::Spinners::SimpleDotsScrolling,
23 style("Creating an auth app...").green().bold().to_string(),
24 );
25
26 match create_auth_app(AuthAppCreate {
27 name: app_name.clone(),
28 description: description.clone(),
29 })
30 .await
31 {
32 Ok(_) => Ok(CommandResult {
33 spinner,
34 symbol: "✅".to_owned(),
35 msg: format!("An auth app created: {app_name}."),
36 }),
37 Err(e) => {
38 println!("Error: {e:#?}");
39 Ok(CommandResult {
40 spinner,
41 symbol: "😩".to_owned(),
42 msg: format!("Failed to create an auth app: {app_name}."),
43 })
44 }
45 }
46 }
47 Commands::List {} => {
48 let spinner = Spinner::new(
49 spinners::Spinners::SimpleDotsScrolling,
50 style("Loading...").green().bold().to_string(),
51 );
52 match get_auth_apps().await {
53 Ok(auth_apps) => {
54 println!("auth_apps: {auth_apps:#?}");
55 println!(
56 "{0: <5} | {1: <20} | {2: <30} | {3: <30}",
57 "ID", "Name", "Created at", "Updated at"
58 );
59 for project in auth_apps {
60 println!(
61 "{0: <5} | {1: <20} | {2: <30} | {3: <30}",
62 project.id, project.name, project.created_at, project.updated_at
63 );
64 }
65 Ok(CommandResult {
66 spinner,
67 symbol: "✅".to_owned(),
68 msg: "Showing all auth apps.".to_owned(),
69 })
70 }
71 Err(e) => {
72 println!("Error: {e:#?}");
73 Ok(CommandResult {
74 spinner,
75 symbol: "😩".to_owned(),
76 msg: "Failed to get all auth apps.".to_owned(),
77 })
78 }
79 }
80 }
81 Commands::Show { id } => {
82 let spinner = Spinner::new(
83 spinners::Spinners::SimpleDotsScrolling,
84 style("Loading...").green().bold().to_string(),
85 );
86 match get_auth_app(&id).await {
87 Ok(_) => Ok(CommandResult {
88 spinner,
89 symbol: "✅".to_owned(),
90 msg: "Showing auth app.".to_owned(),
91 }),
92 Err(e) => {
93 println!("Error: {e:#?}");
94 Ok(CommandResult {
95 spinner,
96 symbol: "😩".to_owned(),
97 msg: "Failed to get an auth app.".to_owned(),
98 })
99 }
100 }
101 }
102 Commands::Delete { id } => {
103 let spinner = Spinner::new(
104 spinners::Spinners::SimpleDotsScrolling,
105 style("Loading...").green().bold().to_string(),
106 );
107 match delete_auth_app(id).await {
108 Ok(_) => Ok(CommandResult {
109 spinner,
110 symbol: "✅".to_owned(),
111 msg: "Delete auth app succeed.".to_owned(),
112 }),
113 Err(e) => {
114 println!("Error: {e:#?}");
115 Ok(CommandResult {
116 spinner,
117 symbol: "😩".to_owned(),
118 msg: "Failed to delete auth app.".to_owned(),
119 })
120 }
121 }
122 }
123 Commands::Use { id } => {
124 let mut spinner = Spinner::new(
125 spinners::Spinners::SimpleDotsScrolling,
126 style("Checking config file...").green().bold().to_string(),
127 );
128
129 let auth_app = get_auth_app(&id).await?;
130 let mut config = get_config().await?;
131
132 if let Some(auth_app) = config.current_auth_app {
133 if auth_app.id != id {
134 spinner.stop_with_message("Found a config.".to_string());
135 let yes = Confirm::new()
136 .with_prompt(format!(
137 "Will change active auth_app to {}. Do you want to continue?",
138 &id
139 ))
140 .interact()?;
141 if !yes {
142 let spinner = Spinner::new(
143 spinners::Spinners::SimpleDotsScrolling,
144 style("Cancelling operation...").green().bold().to_string(),
145 );
146 return Ok(CommandResult {
147 spinner,
148 symbol: "✅".to_owned(),
149 msg: "Operation cancelled.".to_string(),
150 });
151 }
152 }
153 }
154
155 config.current_auth_app = Some(auth_app);
156 let spinner = Spinner::new(
157 spinners::Spinners::SimpleDotsScrolling,
158 style("Saving config...").green().bold().to_string(),
159 );
160 match write_config(config) {
161 Ok(_) => Ok(CommandResult {
162 spinner,
163 symbol: "✅".to_owned(),
164 msg: format!("Using auth_app: {:?}", &id),
165 }),
166 Err(_) => {
167 let error = anyhow!("Failed while writing config.");
168 Err(error)
169 }
170 }
171 }
172 }
173 }