module AzuCLI::Utils

Overview

Centralized utilities module to eliminate code duplication Provides common functionality used across commands and generators

Extended Modules

Defined in:

azu_cli/utils.cr

Constant Summary

REQUIRED_CRYSTAL_VERSION = "1.16.0"

Required minimum Crystal version

Instance Method Summary

Instance Method Detail

def azu_project? : Bool #

Check if current directory is an Azu project


[View source]
def binary_name : String #

Get binary name from shard.yml targets Used by serve command to determine what to run


[View source]
def build_template_context(name : String, attributes : Hash(String, String) = {} of String => String, options : Hash(String, String) = {} of String => String) : Hash(String, String | Array(String) | Hash(String, String)) #

Build template context hash with common variables


[View source]
def camel_case(str : String) : String #

Convert string to camelCase "my_class" => "myClass"


[View source]
def check_crystal_version : Bool #

Check if current Crystal version meets requirements


[View source]
def check_shard_version(shard_name : String, required_version : String) : Bool #

Check if shard version is compatible


[View source]
def command_exists?(command : String) : Bool #

Check if command exists in PATH


[View source]
def compare_versions(v1 : String, v2 : String) : Int32 #

Compare semantic versions Returns -1 if v1 < v2, 0 if equal, 1 if v1 > v2


[View source]
def crystal_file?(path : String) : Bool #

Check if a file is a Crystal source file


[View source]
def crystal_version_info : NamedTuple(version: String, compatible: Bool) #

Get Crystal version info


[View source]
def dependency_version(shard_name : String) : String | Nil #

Get dependency version from shard.yml


[View source]
def detect_schema_info : Tuple(String, String) #

Detect schema name from database configuration Returns tuple of {schema_name, schema_symbol}


[View source]
def enforce_crystal_version! #

Check Crystal version and exit if incompatible


[View source]
def ensure_directory(path : String) #

Ensure directory exists, create if not


[View source]
def file_basename(path : String) : String #

Get file basename without extension


[View source]
def file_extension(path : String) : String #

Get file extension


[View source]
def generate_timestamp : String #

Generate timestamp for migrations (YYYYMMDDHHmmSS format)


[View source]
def generate_unique_timestamp : String #

Generate timestamp with microseconds for unique IDs


[View source]
def has_dependency?(shard_name : String) : Bool #

Check if a shard dependency exists in shard.yml


[View source]
def has_shard_yml? : Bool #

Check if shard.yml exists


[View source]
def indent(text : String, spaces : Int32 = 2) : String #

Indent text by specified spaces


[View source]
def kebab_case(str : String) : String #

Convert string to kebab-case "MyClass" => "my-class"


[View source]
def parse_migration_timestamp(filename : String) : String | Nil #

Parse migration timestamp from filename "20240101120000_create_users.cr" => "20240101120000"


[View source]
def pascal_case(str : String) : String #

Convert string to PascalCase "my_class" => "MyClass"


[View source]
def pluralize(word : String) : String #

Pluralize a word (simple implementation) Handles common cases, may not be perfect for all words


[View source]
def project_authors : Array(String) #

Get project authors from shard.yml


[View source]
def project_description : String | Nil #

Get project description from shard.yml


[View source]
def project_module_name : String #

Get project module name from shard.yml Returns PascalCase module name (e.g., "MyProject")


[View source]
def project_name : String #

Get project name from shard.yml Returns project name or "app" as fallback


[View source]
def project_version : String #

Get project version from shard.yml


[View source]
def reserved_keyword?(name : String) : Bool #

Check if string is a reserved Crystal keyword


[View source]
def run_command(command : String, args : Array(String) = [] of String) : NamedTuple(success: Bool, output: String, error: String) #

Run shell command and capture output


[View source]
def safe_identifier(name : String) : String | Nil #

Validate and sanitize user input for use as identifier Returns sanitized name or nil if invalid Use this when you need a guaranteed valid identifier


[View source]
def safe_read_file(path : String) : String | Nil #

Safely read file with error handling


[View source]
def sanitize_database_url(url : String) : String #

Sanitize database URL for safe logging


[View source]
def sanitize_filename(name : String) : String #

Sanitize user input for use as file/directory name More restrictive than identifier - prevents path traversal This is CRITICAL for security


[View source]
def sanitize_hash_for_logging(hash : Hash) : Hash #

Sanitize hash/options for logging (removes sensitive keys)


[View source]
def sanitize_identifier(name : String) : String #

Sanitize user input for use as Crystal identifier Removes/replaces invalid characters, ensures valid start This is critical for security - prevents code injection


[View source]
def sanitize_log_message(message : String) : String #

Sanitize log messages to remove sensitive information CRITICAL for security - prevents password/token leaks in logs


[View source]
def sanitize_path(path : String, base_dir : String = ".") : String | Nil #

Sanitize path - prevents directory traversal attacks CRITICAL for security when dealing with user-provided paths


[View source]
def sanitize_path_component(component : String) : String | Nil #

Validate and sanitize path component Use this for user-provided directory/file names in paths


[View source]
def schema_file_path : String #

Get schema file path


[View source]
def schema_name : String #

Get schema name from project


[View source]
def screaming_snake_case(str : String) : String #

Convert string to SCREAMING_SNAKE_CASE "myClass" => "MY_CLASS"


[View source]
def singularize(word : String) : String #

Singularize a word (simple implementation)


[View source]
def snake_case(str : String) : String #

Convert string to snake_case "MyClass" => "my_class"


[View source]
def system_info : Hash(String, String) #

Get system information for debugging


[View source]
def template_file?(path : String) : Bool #

Check if a file is a template file


[View source]
def valid_class_name?(name : String) : Bool #

Validate Crystal class/module name


[View source]
def valid_identifier?(name : String) : Bool #

Check if string is a valid Crystal identifier


[View source]
def wrap_text(text : String, width : Int32 = 80) : String #

Wrap text at specified width


[View source]