Skip to content

Commit 219cd81

Browse files
committed
Merge branch 'feature/update' into development
2 parents c1bbba1 + 73ff4d5 commit 219cd81

11 files changed

Lines changed: 49 additions & 80 deletions

File tree

crates/cli/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
## Installation
2424

25-
One can install this program in different ways.
25+
You can install the CLI a few different ways.
2626

2727
### With Cargo
2828

@@ -51,7 +51,7 @@ pip install smbcloud-cli
5151

5252
## Update
5353

54-
Simply rerun the installation command.
54+
To update the CLI, run the same install command again.
5555

5656
## Uninstall
5757

@@ -78,16 +78,16 @@ smb --help
7878

7979
## Contribution
8080

81-
- Setup your Rust tooling.
81+
- Set up your Rust tooling.
8282
- Clone the repo.
83-
- Provide the environment variables in the .env.local.
83+
- Add the required environment variables to `.env.local`.
8484
- Run `cargo run`.
8585

8686
## Credits
8787

88-
This repo is inspired by [Sugar](https://github.com/metaplex-foundation/sugar).
88+
This repo draws inspiration from [Sugar](https://github.com/metaplex-foundation/sugar).
8989

90-
This repo tries to follow [the 12 factor CLI app](https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46) principles by
90+
It also borrows ideas from [the 12 factor CLI app](https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46).
9191

9292
> Explore more on the [smbCloud Services](https://smbcloud.xyz/services) page.
9393

crates/cli/src/account/cli.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use clap::Subcommand;
22

33
#[derive(Subcommand)]
44
pub enum Commands {
5-
#[clap(about = "Create an account. Use your email as your username.")]
5+
#[clap(about = "Create an account with your email address.")]
66
Signup {},
7-
#[clap(about = "Login to your account. To create an account, use smb signup.")]
7+
#[clap(about = "Log in to your account. If you need one first, run `smb signup`.")]
88
Login {},
9-
#[clap(about = "Logout all session.")]
9+
#[clap(about = "Log out of your current session.")]
1010
Logout {},
11-
#[clap(about = "Forgot email? Use this command to reset your password.")]
11+
#[clap(about = "Start the password reset flow.")]
1212
Forgot {},
1313
}

crates/cli/src/account/forgot/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use smbcloud_utils::email_validation;
1313
use spinners::Spinner;
1414

1515
pub async fn process_forgot(env: Environment) -> Result<CommandResult> {
16-
println!("Provide your login credentials.");
16+
println!("Enter your email address.");
1717
let email = Input::<String>::with_theme(&ColorfulTheme::default())
1818
.with_prompt("Email")
1919
.validate_with(|email: &String| email_validation(email))

crates/cli/src/account/lib.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ use {
2525
};
2626

2727
pub async fn authorize_github(env: &Environment) -> Result<SmbAuthorization> {
28-
// Spin up a simple localhost server to listen for the GitHub OAuth callback
29-
// setup_oauth_callback_server();
30-
// Open the GitHub OAuth URL in the user's browser
28+
// Listen for the GitHub OAuth callback on localhost, then open the browser.
3129
let mut spinner = Spinner::new(
3230
spinners::Spinners::BouncingBall,
3331
style("🚀 Getting your GitHub information...")
@@ -61,7 +59,6 @@ pub async fn authorize_github(env: &Environment) -> Result<SmbAuthorization> {
6159
match rx.recv() {
6260
Ok(code) => {
6361
debug!("Got code from channel: {:#?}", &code);
64-
//Err(anyhow!("Failed to get code from channel."))
6562
process_connect_github(*env, code).await
6663
}
6764
Err(e) => {
@@ -144,7 +141,6 @@ fn handle_connection(mut stream: TcpStream, tx: Sender<String>) {
144141
stream.flush().unwrap();
145142
}
146143

147-
// Get access token
148144
pub async fn process_connect_github(env: Environment, code: String) -> Result<SmbAuthorization> {
149145
let response = Client::new()
150146
.post(build_authorize_smb_url(env))
@@ -160,32 +156,25 @@ pub async fn process_connect_github(env: Environment, code: String) -> Result<Sm
160156
.bold()
161157
.to_string(),
162158
);
163-
// println!("Response: {:#?}", &response);
164159
match response.status() {
165160
StatusCode::OK => {
166-
// Account authorized and token received
167161
spinner.stop_and_persist("✅", "You are logged in with your GitHub account!".into());
168162
save_token(env, &response).await?;
169163
let result = response.json().await?;
170164
// println!("Result: {:#?}", &result);
171165
Ok(result)
172166
}
173167
StatusCode::NOT_FOUND => {
174-
// Account not found and we show signup option
175-
spinner.stop_and_persist("🥲", "Account not found. Please signup!".into());
168+
spinner.stop_and_persist("🥲", "Account not found. Please sign up.".into());
176169
let result = response.json().await?;
177-
// println!("Result: {:#?}", &result);
178170
Ok(result)
179171
}
180172
StatusCode::UNPROCESSABLE_ENTITY => {
181-
// Account found but email not verified
182-
spinner.stop_and_persist("🥹", "Unverified email!".into());
173+
spinner.stop_and_persist("🥹", "Please verify your email address.".into());
183174
let result = response.json().await?;
184-
// println!("Result: {:#?}", &result);
185175
Ok(result)
186176
}
187177
_ => {
188-
// Other errors
189178
let error = anyhow!("Error while authorizing with GitHub.");
190179
Err(error)
191180
}
@@ -222,13 +211,14 @@ fn github_base_url_builder() -> URLBuilder {
222211

223212
pub async fn save_token(env: Environment, response: &Response) -> Result<()> {
224213
let headers = response.headers();
225-
// println!("Headers: {:#?}", &headers);
226214
match headers.get("Authorization") {
227215
Some(token) => {
228216
debug!("{}", token.to_str()?);
229217
store_token(env, token.to_str()?.to_string()).await
230218
}
231-
None => Err(anyhow!("Failed to get token. Probably a backend issue.")),
219+
None => Err(anyhow!(
220+
"Failed to get the access token from the server response."
221+
)),
232222
}
233223
}
234224

crates/cli/src/account/login/process.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ pub async fn process_login(env: Environment, is_logged_in: Option<bool>) -> Resu
7878
}
7979
}
8080

81-
// Private functions
82-
8381
async fn login_with_github(env: Environment) -> Result<CommandResult> {
8482
match authorize_github(&env).await {
8583
Ok(result) => process_authorization(env, result).await,
@@ -91,30 +89,26 @@ async fn login_with_github(env: Environment) -> Result<CommandResult> {
9189
}
9290

9391
async fn process_authorization(env: Environment, auth: SmbAuthorization) -> Result<CommandResult> {
94-
// What to do if not logged in with GitHub?
95-
// Check error_code first
92+
// Handle the account state returned by the OAuth flow before treating it as a login.
9693
if let Some(error_code) = auth.error_code {
9794
debug!("{}", error_code);
9895
match error_code {
9996
EmailNotFound => return create_new_account(env, auth.user_email, auth.user_info).await,
10097
EmailUnverified => return send_email_verification(env, auth.user).await,
10198
PasswordNotSet => {
102-
// Only for email and password login
10399
let error = anyhow!("Password not set.");
104100
return Err(error);
105101
}
106102
GithubNotLinked => return connect_github_account(env, auth).await,
107103
}
108104
}
109105

110-
// Logged in with GitHub!
111-
// Token handling is in the lib.rs account module.
106+
// Token handling lives in the account module.
112107
if let Some(user) = auth.user {
113108
let spinner = Spinner::new(
114109
spinners::Spinners::SimpleDotsScrolling,
115110
style("Logging you in...").green().bold().to_string(),
116111
);
117-
// We're logged in with GitHub.
118112
return Ok(CommandResult {
119113
spinner,
120114
symbol: "✅".to_owned(),
@@ -142,7 +136,6 @@ async fn create_new_account(
142136
}
143137
};
144138

145-
// Create account if user confirms
146139
if !confirm {
147140
let spinner = Spinner::new(
148141
spinners::Spinners::SimpleDotsScrolling,
@@ -169,11 +162,10 @@ async fn create_new_account(
169162
return do_signup(env, &params).await;
170163
}
171164

172-
Err(anyhow!("Shouldn't be here."))
165+
Err(anyhow!("GitHub returned incomplete account details."))
173166
}
174167

175168
async fn send_email_verification(env: Environment, user: Option<User>) -> Result<CommandResult> {
176-
// Return early if user is null
177169
if let Some(user) = user {
178170
let confirm = match Confirm::with_theme(&ColorfulTheme::default())
179171
.with_prompt("Do you want to send a new verification email?")
@@ -186,16 +178,15 @@ async fn send_email_verification(env: Environment, user: Option<User>) -> Result
186178
}
187179
};
188180

189-
// Send verification email if user confirms
190181
if !confirm {
191182
let spinner = Spinner::new(
192183
spinners::Spinners::SimpleDotsScrolling,
193-
style("Cancel operation.").green().bold().to_string(),
184+
style("Cancelled.").green().bold().to_string(),
194185
);
195186
return Ok(CommandResult {
196187
spinner,
197188
symbol: succeed_symbol(),
198-
msg: succeed_message("Doing nothing."),
189+
msg: succeed_message("Cancelled."),
199190
});
200191
}
201192
resend_email_verification(env, user).await
@@ -238,16 +229,15 @@ async fn connect_github_account(env: Environment, auth: SmbAuthorization) -> Res
238229
}
239230
};
240231

241-
// Link GitHub account if user confirms
242232
if !confirm {
243233
let spinner = Spinner::new(
244234
spinners::Spinners::SimpleDotsScrolling,
245-
succeed_message("Cancel operation."),
235+
succeed_message("Cancelled."),
246236
);
247237
return Ok(CommandResult {
248238
spinner,
249239
symbol: succeed_symbol(),
250-
msg: succeed_message("Doing nothing."),
240+
msg: succeed_message("Cancelled."),
251241
});
252242
}
253243

@@ -278,7 +268,7 @@ async fn connect_github_account(env: Environment, auth: SmbAuthorization) -> Res
278268
}
279269

280270
async fn login_with_email(env: Environment) -> Result<CommandResult> {
281-
println!("Provide your login credentials.");
271+
println!("Enter your login details.");
282272
let username = match Input::<String>::with_theme(&ColorfulTheme::default())
283273
.with_prompt("Email")
284274
.validate_with(|email: &String| email_validation(email))
@@ -293,9 +283,7 @@ async fn login_with_email(env: Environment) -> Result<CommandResult> {
293283

294284
match check_email(env, client(), &username).await {
295285
Ok(auth) => {
296-
// Only continue with password input if email is found and confirmed.
297286
if auth.error_code.is_some() {
298-
// Check if email is in the database, unconfirmed. Only presents password input if email is found and confirmed.
299287
let spinner = Spinner::new(
300288
spinners::Spinners::SimpleDotsScrolling,
301289
succeed_message("Checking email"),
@@ -382,11 +370,11 @@ async fn after_checking_email_step(
382370
}
383371
_ => {
384372
spinner.stop_and_persist(&fail_symbol(), fail_message("An error occurred."));
385-
Err(anyhow!("Idk what happened."))
373+
Err(anyhow!("The server returned an unexpected account state."))
386374
}
387375
}
388376
}
389-
None => Err(anyhow!("Shouldn't be here.")),
377+
None => Err(anyhow!("The server did not return an account state.")),
390378
}
391379
}
392380

@@ -421,13 +409,12 @@ async fn action_on_account_status(
421409
}
422410
_ => {
423411
spinner.stop_and_persist(&fail_symbol(), fail_message("An error occurred."));
424-
Err(anyhow!("Idk what happened."))
412+
Err(anyhow!("The server returned an unexpected account state."))
425413
}
426414
}
427415
}
428416

429417
async fn send_reset_password(env: Environment, user: Option<User>) -> Result<CommandResult> {
430-
// Return early if user is null
431418
if let Some(user) = user {
432419
let confirm = match Confirm::with_theme(&ColorfulTheme::default())
433420
.with_prompt("Do you want to reset your password?")
@@ -440,16 +427,15 @@ async fn send_reset_password(env: Environment, user: Option<User>) -> Result<Com
440427
}
441428
};
442429

443-
// Send verification email if user confirms
444430
if !confirm {
445431
let spinner = Spinner::new(
446432
spinners::Spinners::SimpleDotsScrolling,
447-
style("Cancel operation.").green().bold().to_string(),
433+
style("Cancelled.").green().bold().to_string(),
448434
);
449435
return Ok(CommandResult {
450436
spinner,
451437
symbol: succeed_symbol(),
452-
msg: succeed_message("Doing nothing."),
438+
msg: succeed_message("Cancelled."),
453439
});
454440
}
455441
resend_reset_password_instruction(env, user).await

crates/cli/src/account/logout/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use {
1414
};
1515

1616
pub async fn process_logout(env: Environment) -> Result<CommandResult> {
17-
// Logout if user confirms
1817
if let Some(token_path) = smb_token_file_path(env) {
1918
let confirm = match Confirm::with_theme(&ColorfulTheme::default())
2019
.with_prompt("Do you want to logout? y/n")
@@ -30,10 +29,10 @@ pub async fn process_logout(env: Environment) -> Result<CommandResult> {
3029
return Ok(CommandResult {
3130
spinner: Spinner::new(
3231
spinners::Spinners::SimpleDotsScrolling,
33-
succeed_message("Cancel operation"),
32+
succeed_message("Cancelled."),
3433
),
3534
symbol: succeed_symbol(),
36-
msg: succeed_message("Doing nothing."),
35+
msg: succeed_message("Cancelled."),
3736
});
3837
}
3938

@@ -42,7 +41,6 @@ pub async fn process_logout(env: Environment) -> Result<CommandResult> {
4241
succeed_message("Logging you out"),
4342
);
4443

45-
// Call backend
4644
match do_process_logout(env).await {
4745
Ok(_) => {
4846
fs::remove_file(token_path)?;

crates/smbcloud-model/src/error_codes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub enum ErrorCode {
3939
MissingConfig = 4,
4040
// #[error("Missing id in repository. Please regenerate with 'smb init'.")]
4141
// MissingId,
42-
#[error("Cancel operation.")]
42+
#[error("Cancelled.")]
4343
Cancel = 5,
4444
// Account
4545
#[error("Unauthorized access.")]
@@ -122,7 +122,7 @@ impl ErrorCode {
122122
// CLI Generic errors
123123
ErrorCode::InputError => "Input error.",
124124
ErrorCode::MissingConfig => "Missing config.",
125-
ErrorCode::Cancel => "Cancelled operation.",
125+
ErrorCode::Cancel => "Cancelled.",
126126
// Projects
127127
ErrorCode::ProjectNotFound => "Project not found.",
128128
ErrorCode::UnsupportedRunner => "Unsupported runner.",

crates/smbcloud-networking/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Low-level HTTP client helpers for smbCloud services.
44

5-
This crate was inspired by this excellent blog post: [Designing a Web API client in Rust](https://nullderef.com/blog/web-api-client/).
5+
This crate takes a few cues from [Designing a Web API client in Rust](https://nullderef.com/blog/web-api-client/).
66

77
> Explore more on the [smbCloud Services](https://smbcloud.xyz/services) page.
88

npm/smbcloud-cli/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ function getExePath() {
2020
}
2121

2222
try {
23-
// Since the binary will be located inside `node_modules`, we can simply call `require.resolve`
24-
return require.resolve(`@smbcloud/cli-${os}-${arch}/bin/smb${extension}`);
23+
// The binary lives in `node_modules`, so `require.resolve` gives us the installed path.
24+
return require.resolve(
25+
`@smbcloud/cli-${os}-${arch}/bin/smb${extension}`,
26+
);
2527
} catch (e) {
2628
throw new Error(
27-
`Couldn't find application binary inside node_modules for ${os}-${arch}`
29+
`Couldn't find application binary inside node_modules for ${os}-${arch}`,
2830
);
2931
}
3032
}
3133

3234
/**
33-
* Runs the application with args using nodejs spawn
35+
* Runs the native CLI with the current process arguments.
3436
*/
3537
function run() {
3638
const args = process.argv.slice(2);
3739
const processResult = spawnSync(getExePath(), args, { stdio: "inherit" });
3840
process.exit(processResult.status ?? 0);
3941
}
4042

41-
run();
43+
run();

0 commit comments

Comments
 (0)