Skip to main content

ralph_workflow/app/io/
effect_io.rs

1use std::fs;
2use std::io;
3use std::io::IsTerminal;
4use std::path::{Path, PathBuf};
5
6pub fn set_current_dir(path: &Path) -> Result<(), io::Error> {
7    std::env::set_current_dir(path)
8}
9
10pub fn write_file(path: &Path, content: String) -> Result<(), io::Error> {
11    if let Some(parent) = path.parent() {
12        fs::create_dir_all(parent)?;
13    }
14    fs::write(path, content)
15}
16
17pub fn read_file(path: &Path) -> Result<String, io::Error> {
18    fs::read_to_string(path)
19}
20
21pub fn delete_file(path: &Path) -> Result<(), io::Error> {
22    fs::remove_file(path)
23}
24
25pub fn create_dir(path: &Path) -> Result<(), io::Error> {
26    fs::create_dir_all(path)
27}
28
29pub fn path_exists(path: &Path) -> bool {
30    path.exists()
31}
32
33pub fn set_read_only(path: &Path, readonly: bool) -> Result<(), io::Error> {
34    let metadata = fs::metadata(path)?;
35    let mut permissions = metadata.permissions();
36    permissions.set_readonly(readonly);
37    fs::set_permissions(path, permissions)
38}
39
40pub fn get_env_var(name: &str) -> Result<String, std::env::VarError> {
41    std::env::var(name)
42}
43
44pub fn set_env_var(name: &str, value: &str) {
45    std::env::set_var(name, value);
46}
47
48pub fn resolve_path(workspace_root: &Option<PathBuf>, path: &Path) -> PathBuf {
49    if path.is_absolute() {
50        path.to_path_buf()
51    } else if let Some(ref root) = workspace_root {
52        root.join(path)
53    } else {
54        path.to_path_buf()
55    }
56}
57
58pub fn check_no_resume_prompt() -> bool {
59    std::env::var("RALPH_NO_RESUME_PROMPT").is_ok()
60}
61
62pub fn is_terminal_io() -> bool {
63    std::io::stdin().is_terminal()
64        && (std::io::stdout().is_terminal() || std::io::stderr().is_terminal())
65}
66
67pub fn get_current_dir() -> PathBuf {
68    std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
69}
70
71pub fn get_args() -> Vec<String> {
72    std::env::args().collect()
73}
74
75pub fn get_program_args() -> Vec<String> {
76    std::env::args().skip(1).collect()
77}
78
79pub fn get_process_id() -> u32 {
80    std::process::id()
81}
82
83pub fn exit_with_code(code: i32) -> ! {
84    std::process::exit(code)
85}
86
87pub fn read_user_input() -> String {
88    use std::io;
89    let mut input = String::new();
90    let _ = io::stdin().read_line(&mut input);
91    input
92}
93
94pub fn write_stdout(content: &str) -> std::io::Result<()> {
95    use std::io::Write;
96    std::io::stdout().write_all(content.as_bytes())
97}
98
99pub fn flush_stdout() -> std::io::Result<()> {
100    use std::io::Write;
101    std::io::stdout().flush()
102}