Skip to content

Commit bbe0165

Browse files
committed
Give no log-in advice to a caller that never sends the user's token
1 parent 2250cf3 commit bbe0165

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

‎src/main.rs‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ fn run(app: &Command, specs: &[ServiceSpec], matches: &ArgMatches, mode: Mode) -
15631563
CliAttempt::Unusable { error, why, .. } if !caller.user_fallback() => {
15641564
// It never sends the user's token, so it goes on as
15651565
// if they had none.
1566-
without_cli_token(*error, &why, dry_run, false).map_err(|e| *e)?;
1566+
without_cli_token(*error, &why, dry_run, false, false).map_err(|e| *e)?;
15671567
return executor::execute(
15681568
op,
15691569
op_matches,
@@ -1603,7 +1603,8 @@ fn run(app: &Command, specs: &[ServiceSpec], matches: &ArgMatches, mode: Mode) -
16031603

16041604
if let Some((failure, why)) = cli_failure {
16051605
if let Some(note) =
1606-
without_cli_token(*failure, &why, dry_run, token.is_some()).map_err(|e| *e)?
1606+
without_cli_token(*failure, &why, dry_run, token.is_some(), true)
1607+
.map_err(|e| *e)?
16071608
{
16081609
output::progress(&note);
16091610
}
@@ -1801,9 +1802,13 @@ fn without_cli_token(
18011802
why: &str,
18021803
dry_run: bool,
18031804
own_token: bool,
1805+
user_fallback: bool,
18041806
) -> std::result::Result<Option<String>, Box<CliError>> {
18051807
if dry_run {
18061808
Ok(None)
1809+
} else if !user_fallback {
1810+
// Logging in would not help: this caller never sends the user's token.
1811+
Err(Box::new(error))
18071812
} else if own_token {
18081813
Ok(Some(format!("{why} Using your own token instead.")))
18091814
} else {
@@ -3075,15 +3080,24 @@ mod cli_attempt_tests {
30753080
#[test]
30763081
fn without_a_token_of_their_own_the_user_is_told_to_log_in_unless_it_is_a_dry_run() {
30773082
let error = || CliError::new("cli_token_forbidden", "refused");
3078-
let failed = without_cli_token(error(), "why", false, false).expect_err("no token");
3083+
let failed = without_cli_token(error(), "why", false, false, true).expect_err("no token");
30793084
assert_eq!(failed.code, "cli_token_forbidden");
30803085
assert_eq!(failed.fix, no_own_token_remedy().fix);
30813086
assert!(matches!(
3082-
without_cli_token(error(), "why", true, false),
3087+
without_cli_token(error(), "why", true, false, true),
30833088
Ok(None)
30843089
));
3085-
let note = without_cli_token(error(), "Why.", false, true).expect("own token");
3090+
let note = without_cli_token(error(), "Why.", false, true, true).expect("own token");
30863091
assert_eq!(note.as_deref(), Some("Why. Using your own token instead."));
3092+
3093+
let no_fallback =
3094+
without_cli_token(error(), "why", false, true, false).expect_err("never falls back");
3095+
assert_eq!(no_fallback.code, "cli_token_forbidden");
3096+
assert_eq!(no_fallback.fix, None);
3097+
assert!(matches!(
3098+
without_cli_token(error(), "why", true, false, false),
3099+
Ok(None)
3100+
));
30873101
}
30883102

30893103
#[test]

0 commit comments

Comments
 (0)