1pub mod cli;
4pub mod commands;
5pub mod env;
6pub mod templates;
7
8use colored::Colorize;
9use std::fs;
10use std::path::Path;
11use std::sync::atomic::{AtomicBool, Ordering};
12
13pub const TIDEWAY_VERSION: &str = env!("TIDEWAY_VERSION");
14
15static JSON_OUTPUT: AtomicBool = AtomicBool::new(false);
16static PLAN_MODE: AtomicBool = AtomicBool::new(false);
17
18pub fn set_json_output(enabled: bool) {
19 JSON_OUTPUT.store(enabled, Ordering::Relaxed);
20}
21
22pub fn is_json_output() -> bool {
23 JSON_OUTPUT.load(Ordering::Relaxed)
24}
25
26pub fn set_plan_mode(enabled: bool) {
27 PLAN_MODE.store(enabled, Ordering::Relaxed);
28}
29
30pub fn is_plan_mode() -> bool {
31 PLAN_MODE.load(Ordering::Relaxed)
32}
33
34fn print_json(level: &str, message: &str) {
35 let payload = serde_json::json!({
36 "level": level,
37 "message": message,
38 });
39 println!("{}", payload);
40}
41
42pub fn print_success(message: &str) {
44 if is_json_output() {
45 print_json("success", message);
46 } else {
47 println!("{} {}", "✓".green().bold(), message);
48 }
49}
50
51pub fn print_info(message: &str) {
53 if is_json_output() {
54 print_json("info", message);
55 } else {
56 println!("{} {}", "→".blue(), message);
57 }
58}
59
60pub fn print_warning(message: &str) {
62 if is_json_output() {
63 print_json("warning", message);
64 } else {
65 println!("{} {}", "!".yellow().bold(), message);
66 }
67}
68
69pub fn print_error(message: &str) {
71 if is_json_output() {
72 print_json("error", message);
73 } else {
74 println!("{} {}", "✗".red().bold(), message);
75 }
76}
77
78pub fn ensure_dir(path: &Path) -> std::io::Result<()> {
79 if is_plan_mode() {
80 print_info(&format!("Plan: create directory {}", path.display()));
81 Ok(())
82 } else {
83 fs::create_dir_all(path)
84 }
85}
86
87pub fn write_file(path: &Path, contents: &str) -> std::io::Result<()> {
88 if is_plan_mode() {
89 print_info(&format!("Plan: write file {}", path.display()));
90 Ok(())
91 } else {
92 fs::write(path, contents)
93 }
94}
95
96pub fn remove_file(path: &Path) -> std::io::Result<()> {
97 if is_plan_mode() {
98 print_info(&format!("Plan: remove file {}", path.display()));
99 Ok(())
100 } else {
101 fs::remove_file(path)
102 }
103}
104
105pub fn remove_dir(path: &Path) -> std::io::Result<()> {
106 if is_plan_mode() {
107 print_info(&format!("Plan: remove directory {}", path.display()));
108 Ok(())
109 } else {
110 fs::remove_dir(path)
111 }
112}