11# --- Standard library imports ---
22import logging
3- from logging .handlers import RotatingFileHandler
4- import os
53import platform
4+ from logging .handlers import RotatingFileHandler
5+ from pathlib import Path
6+ from urllib .parse import urlparse
67
78# --- Third-party imports ---
89import questionary
910from rich .console import Console
1011from rich .logging import RichHandler
1112from rich .theme import Theme
1213
13- __all__ = [
14- "console" ,
15- "qy" ,
16- "DEFAULT_QY_STYLE" ,
17- "SCRIPT_HOME_DIR" ,
18- "SCRIPT_LOGGING_DIR" ,
19- "STEP_BIN" ,
20- "logger" ,
21- ]
22-
23-
2414custom_logging_theme = Theme (
2515 {
2616 "logging.level.info" : "none" ,
4232)
4333
4434# --- Directories and files ---
45- SCRIPT_HOME_DIR = os .path .expanduser ("~/.step-cli-tools" )
46- SCRIPT_LOGGING_DIR = os .path .normpath (os .path .join (SCRIPT_HOME_DIR , "logs" ))
35+ SCRIPT_HOME_DIR = Path .home () / ".step-cli-tools"
36+ SCRIPT_CACHE_DIR = SCRIPT_HOME_DIR / ".cache"
37+ SCRIPT_CERT_DIR = SCRIPT_HOME_DIR / "certs"
38+ SCRIPT_LOGGING_DIR = SCRIPT_HOME_DIR / "logs"
39+
40+ ALL_DIRS = [
41+ SCRIPT_HOME_DIR ,
42+ SCRIPT_CACHE_DIR ,
43+ SCRIPT_CERT_DIR ,
44+ SCRIPT_LOGGING_DIR ,
45+ ]
4746
4847
49- def _get_step_binary_path () -> str :
48+ def _get_step_binary_path () -> Path :
5049 """
5150 Get the absolute path to the step-cli binary based on the operating system.
5251
5352 Returns:
54- str: Absolute path to the step binary.
53+ The absolute path to the step-cli binary.
5554 """
5655
57- bin_dir = os . path . join ( SCRIPT_HOME_DIR , "bin" )
56+ bin_dir = SCRIPT_HOME_DIR / "bin"
5857 system = platform .system ()
5958 if system == "Windows" :
60- binary = os . path . join ( bin_dir , "step.exe" )
59+ binary = bin_dir / "step.exe"
6160 elif system in ("Linux" , "Darwin" ):
62- binary = os . path . join ( bin_dir , "step" )
61+ binary = bin_dir / "step"
6362 else :
6463 raise OSError (f"Unsupported platform: { system } " )
6564
66- return os . path . normpath ( binary )
65+ return binary
6766
6867
6968STEP_BIN = _get_step_binary_path ()
@@ -73,8 +72,8 @@ def _get_step_binary_path() -> str:
7372
7473def _setup_logger (
7574 name : str ,
76- log_file : str = "step-cli-tools.log" ,
77- level = logging .DEBUG ,
75+ log_file : Path = SCRIPT_LOGGING_DIR / "step-cli-tools.log" ,
76+ level : int = logging .DEBUG ,
7877 console : Console = console ,
7978 max_bytes : int = 5_000_000 ,
8079 backup_count : int = 5 ,
@@ -95,8 +94,8 @@ def _setup_logger(
9594 """
9695
9796 # Ensure log directory exists
98- if os . path . dirname ( log_file ) :
99- os . makedirs ( os . path . dirname ( log_file ) , exist_ok = True )
97+ if log_file . parent :
98+ log_file . parent . mkdir ( parents = True , exist_ok = True )
10099
101100 logger = logging .getLogger (name )
102101 logger .setLevel (level )
@@ -128,5 +127,25 @@ def _setup_logger(
128127
129128logger = _setup_logger (
130129 name = "main" ,
131- log_file = os .path .join (SCRIPT_LOGGING_DIR , "step-cli-tools.log" ),
132130)
131+
132+
133+ def get_masked_url_for_logging (url : str ) -> str :
134+ """
135+ Return a masked version of the URL suitable for logging.
136+
137+ Args:
138+ url: The URL to mask
139+
140+ Returns:
141+ A string containing only the scheme, hostname, and optional port
142+ """
143+
144+ parsed_url = urlparse (url )
145+ scheme = parsed_url .scheme or "unknown"
146+ hostname = parsed_url .hostname or "unknown"
147+ port = parsed_url .port
148+
149+ if port :
150+ return f"{ scheme } ://{ hostname } :{ port } "
151+ return f"{ scheme } ://{ hostname } "
0 commit comments