Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ use crate::commands::playlist::PlaylistCommand;
use crate::commands::{CommandError, Formatter, OutputType, PlaylistFile};
const DEFAULT_ASSET_DURATION: u32 = 15;

/// Returns a user-friendly error message for authentication errors.
fn get_authentication_error_message(e: &AuthenticationError) -> String {
match e {
AuthenticationError::Io(io_err) if io_err.kind() == std::io::ErrorKind::NotFound => {
"Not logged in. Please run `screenly login` first to authenticate.".to_string()
}
_ => {
format!("Authentication error: {e}. Please run `screenly login` to authenticate.")
}
}
}

/// Creates an Authentication instance or exits with a user-friendly error message.
fn get_authentication() -> Authentication {
match Authentication::new() {
Ok(auth) => auth,
Err(e) => {
error!("{}", get_authentication_error_message(&e));
std::process::exit(1);
}
}
}

#[derive(Error, Debug)]
enum ParseError {
#[error("missing \"=\" symbol")]
Expand Down Expand Up @@ -547,7 +570,7 @@ fn get_user_input() -> String {
}

pub fn handle_cli_screen_command(command: &ScreenCommands) {
let authentication = Authentication::new().expect("Failed to load authentication.");
let authentication = get_authentication();
let screen_command = commands::screen::ScreenCommand::new(authentication);

match command {
Expand Down Expand Up @@ -591,8 +614,7 @@ pub fn handle_cli_screen_command(command: &ScreenCommands) {
}

pub fn handle_cli_playlist_command(command: &PlaylistCommands) {
let playlist_command =
PlaylistCommand::new(Authentication::new().expect("Failed to load authentication."));
let playlist_command = PlaylistCommand::new(get_authentication());
match command {
PlaylistCommands::Create {
json,
Expand Down Expand Up @@ -678,7 +700,7 @@ pub fn handle_cli_playlist_command(command: &PlaylistCommands) {
}

pub fn handle_cli_asset_command(command: &AssetCommands) {
let authentication = Authentication::new().expect("Failed to load authentication.");
let authentication = get_authentication();
let asset_command = commands::asset::AssetCommand::new(authentication);

match command {
Expand Down Expand Up @@ -819,7 +841,7 @@ pub fn handle_cli_asset_command(command: &AssetCommands) {
}

pub fn handle_cli_edge_app_command(command: &EdgeAppCommands) {
let authentication = Authentication::new().expect("Failed to load authentication.");
let authentication = get_authentication();
let edge_app_command = commands::edge_app::EdgeAppCommand::new(authentication);

match command {
Expand Down Expand Up @@ -1221,4 +1243,28 @@ mod tests {

assert_eq!(new_path, dir_path.join("screenly.yml"));
}

#[test]
fn test_get_authentication_error_message_when_not_logged_in() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let auth_err = AuthenticationError::Io(io_err);

let message = get_authentication_error_message(&auth_err);

assert_eq!(
message,
"Not logged in. Please run `screenly login` first to authenticate."
);
}

#[test]
fn test_get_authentication_error_message_for_other_errors() {
let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "permission denied");
let auth_err = AuthenticationError::Io(io_err);

let message = get_authentication_error_message(&auth_err);

assert!(message.contains("Authentication error"));
assert!(message.contains("Please run `screenly login` to authenticate"));
}
}
Loading