From 572cfab8ca25745ba3a3220e2a3e97195d385053 Mon Sep 17 00:00:00 2001 From: rzmk <30333942+rzmk@users.noreply.github.com> Date: Wed, 3 Sep 2025 08:34:53 -0400 Subject: [PATCH] refactor: colored text --- src/main.rs | 100 ++++++++++++++++---------------------------------- src/styles.rs | 29 +++++++++++++++ 2 files changed, 61 insertions(+), 68 deletions(-) create mode 100644 src/styles.rs diff --git a/src/main.rs b/src/main.rs index 4f1637b..f0ecb33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ +mod styles; + +use crate::styles::{highlighted_text, important_text, step_text, success_text}; use anyhow::Result; use clap::Parser; use human_panic::{metadata, setup_panic}; use inquire::Confirm; -use owo_colors::{OwoColorize, Stream::Stdout, Style}; use serde_json::json; use std::{path::PathBuf, str::FromStr}; use xshell::cmd; @@ -24,25 +26,20 @@ fn main() -> Result<()> { let args = Args::parse(); - // Color styles - let highlight_style = Style::new().on_blue().white(); - let important_style = Style::new().on_bright_red().white(); - let step_style = Style::new().on_magenta().white(); - let success_style = Style::new().on_green().white(); - println!("Welcome to the ckan-devstaller!"); println!( "ckan-devstaller is provided by datHere - {}\n", - "https://datHere.com".if_supports_color(Stdout, |t| t.style(highlight_style)), + highlighted_text("https://datHere.com"), ); println!( "This installer should assist in setting up {} from a source installation along with ckan-compose (https://github.com/tino097/ckan-compose). If you have any issues, please report them at https://github.com/dathere/ckan-devstaller/issues.", - "CKAN 2.11.3".if_supports_color(Stdout, |t| t.style(highlight_style)) + highlighted_text("CKAN 2.11.3") ); println!( "{}", - "This installer is only intended for a brand new installation of Ubuntu 22.04." - .if_supports_color(Stdout, |t| t.style(important_style)) + important_text( + "This installer is only intended for a brand new installation of Ubuntu 22.04." + ) ); let ans = if args.default { @@ -58,33 +55,27 @@ fn main() -> Result<()> { let username = cmd!(sh, "whoami").read()?; println!( "\n{} Running {} and {}...", - "1.".if_supports_color(Stdout, |t| t.style(step_style)), - "sudo apt update -y".if_supports_color(Stdout, |t| t.style(highlight_style)), - "sudo apt upgrade -y".if_supports_color(Stdout, |t| t.style(highlight_style)) + step_text("1."), + highlighted_text("sudo apt update -y"), + highlighted_text("sudo apt upgrade -y") ); println!( "{}", - "You may need to provide your sudo password." - .if_supports_color(Stdout, |t| t.style(important_style)) + important_text("You may need to provide your sudo password.") ); cmd!(sh, "sudo apt update -y").run()?; // Ignoring xrdp error with .ignore_status() for now cmd!(sh, "sudo apt upgrade -y").ignore_status().run()?; println!( "{}", - "✅ 1. Successfully ran update and upgrade commands." - .if_supports_color(Stdout, |t| t.style(success_style)) + success_text("✅ 1. Successfully ran update and upgrade commands.") ); - println!( - "\n{} Installing curl and enabling SSH...", - "2.".if_supports_color(Stdout, |t| t.style(step_style)), - ); + println!("\n{} Installing curl and enabling SSH...", step_text("2.")); cmd!(sh, "sudo apt install curl openssh-server -y").run()?; println!( "{}", - "✅ 2. Successfully installed curl and enabled SSH." - .if_supports_color(Stdout, |t| t.style(success_style)) + success_text("✅ 2. Successfully installed curl and enabled SSH.") ); let dpkg_l_output = cmd!(sh, "dpkg -l").read()?; @@ -95,21 +86,14 @@ fn main() -> Result<()> { .status .success(); if !has_docker { - println!( - "{} Installing Docker...", - "3.".if_supports_color(Stdout, |t| t.style(step_style)), - ); + println!("{} Installing Docker...", step_text("3."),); cmd!( sh, "curl -fsSL https://get.docker.com -o /home/{username}/get-docker.sh" ) .run()?; cmd!(sh, "sudo sh /home/{username}/get-docker.sh").run()?; - println!( - "{}", - "✅ 3. Successfully installed Docker." - .if_supports_color(Stdout, |t| t.style(success_style)) - ); + println!("{}", success_text("✅ 3. Successfully installed Docker.")); } let has_docker_compose = cmd!(sh, "grep docker-compose") @@ -122,23 +106,16 @@ fn main() -> Result<()> { cmd!(sh, "sudo apt install docker-compose -y").run()?; } - println!( - "\n{} Installing Ahoy...", - "4.".if_supports_color(Stdout, |t| t.style(step_style)), - ); + println!("\n{} Installing Ahoy...", step_text("4."),); sh.change_dir(format!("/home/{username}")); cmd!(sh, "sudo curl -LO https://github.com/ahoy-cli/ahoy/releases/download/v2.5.0/ahoy-bin-linux-amd64").run()?; cmd!(sh, "mv ./ahoy-bin-linux-amd64 ./ahoy").run()?; cmd!(sh, "sudo chmod +x ./ahoy").run()?; - println!( - "{}", - "✅ 4. Successfully installed Ahoy." - .if_supports_color(Stdout, |t| t.style(success_style)) - ); + println!("{}", success_text("✅ 4. Successfully installed Ahoy.")); println!( "\n{} Downloading, installing, and starting ckan-compose...", - "5.".if_supports_color(Stdout, |t| t.style(step_style)), + step_text("5."), ); if !std::fs::exists(format!("/home/{username}/ckan-compose"))? { cmd!(sh, "git clone https://github.com/tino097/ckan-compose.git").run()?; @@ -150,16 +127,9 @@ DATASTORE_READONLY_PASSWORD=pass POSTGRES_PASSWORD=pass"; std::fs::write(format!("/home/{username}/ckan-compose/.env"), env_data)?; cmd!(sh, "sudo ../ahoy up").run()?; - println!( - "{}", - "✅ 5. Successfully ran ckan-compose." - .if_supports_color(Stdout, |t| t.style(success_style)) - ); + println!("{}", success_text("✅ 5. Successfully ran ckan-compose.")); - println!( - "\n{} Installing CKAN 2.11.3...", - "6.".if_supports_color(Stdout, |t| t.style(step_style)), - ); + println!("\n{} Installing CKAN 2.11.3...", step_text("6."),); cmd!(sh, "sudo apt install python3-dev libpq-dev python3-pip python3-venv git-core redis-server -y").run()?; cmd!(sh, "sudo mkdir -p /usr/lib/ckan/default").run()?; cmd!(sh, "sudo chown {username} /usr/lib/ckan/default").run()?; @@ -195,14 +165,11 @@ POSTGRES_PASSWORD=pass"; "ckan -c /etc/ckan/default/ckan.ini sysadmin add {username}" ) .run()?; - println!( - "{}", - "✅ 6. Installed CKAN 2.11.3.".if_supports_color(Stdout, |t| t.style(success_style)) - ); + println!("{}", success_text("✅ 6. Installed CKAN 2.11.3.")); println!( "\n{} Enabling DataStore plugin, adding config URLs in /etc/ckan/default/ckan.ini and updating permissions...", - "7.".if_supports_color(Stdout, |t| t.style(step_style)), + step_text("7."), ); // TODO: use the ckan config-tool command instead of rust-ini let mut conf = ini::Ini::load_from_file("/etc/ckan/default/ckan.ini")?; @@ -246,13 +213,14 @@ POSTGRES_PASSWORD=pass"; cmd!(sh, "sudo docker exec {postgres_container_id} psql -U ckan_default --set ON_ERROR_STOP=1 -f permissions.sql").run()?; println!( "{}", - "✅ 7. Enabled DataStore plugin, set DataStore URLs in /etc/ckan/default/ckan.ini, and updated permissions." - .if_supports_color(Stdout, |t| t.style(success_style)) + success_text( + "✅ 7. Enabled DataStore plugin, set DataStore URLs in /etc/ckan/default/ckan.ini, and updated permissions." + ) ); println!( - "\n{} Installing ckanext-scheming and DataPusher+ extensions...", - "8.".if_supports_color(Stdout, |t| t.style(step_style)), + "{}", + step_text("\n{} Installing ckanext-scheming and DataPusher+ extensions..."), ); cmd!( sh, @@ -393,14 +361,10 @@ ckanext.datapusher_plus.enable_form_redirect = true .run()?; println!( "{}", - "✅ 8. Installed ckanext-scheming and DataPusher+ extensions." - .if_supports_color(Stdout, |t| t.style(success_style)) + success_text("✅ 8. Installed ckanext-scheming and DataPusher+ extensions.") ); - println!( - "\n{}", - "✅ 9. Running CKAN instance...".if_supports_color(Stdout, |t| t.style(success_style)) - ); + println!("\n{}", success_text("✅ 9. Running CKAN instance...")); cmd!(sh, "ckan -c /etc/ckan/default/ckan.ini run").run()?; } @@ -417,7 +381,7 @@ fn get_config_from_prompts() -> Result { .with_help_message( format!( "This step would install {}", - "openssh-server".if_supports_color(Stdout, |t| t.on_blue().white()) + highlighted_text("openssh-server") ) .as_str(), ) diff --git a/src/styles.rs b/src/styles.rs new file mode 100644 index 0000000..11234dd --- /dev/null +++ b/src/styles.rs @@ -0,0 +1,29 @@ +use owo_colors::{OwoColorize, Stream::Stdout}; + +pub fn highlighted_text(text: &str) -> impl std::fmt::Display { + format!( + "{}", + text.if_supports_color(Stdout, |t| t.on_blue().white()) + ) +} + +pub fn important_text(text: &str) -> impl std::fmt::Display { + format!( + "{}", + text.if_supports_color(Stdout, |t| t.on_bright_red().white()) + ) +} + +pub fn step_text(text: &str) -> impl std::fmt::Display { + format!( + "{}", + text.if_supports_color(Stdout, |t| t.on_magenta().white()) + ) +} + +pub fn success_text(text: &str) -> impl std::fmt::Display { + format!( + "{}", + text.if_supports_color(Stdout, |t| t.on_green().white()) + ) +}