cli/
utils.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::env;
16use std::path::Path;
17
18use snafu::ResultExt;
19
20use crate::error::{GetCurrentDirSnafu, Result};
21
22/// Resolves the relative path to an absolute path.
23pub fn resolve_relative_path(current_dir: impl AsRef<Path>, path_str: &str) -> String {
24    let path = Path::new(path_str);
25    if path.is_relative() {
26        let path = current_dir.as_ref().join(path);
27        common_telemetry::debug!("Resolved relative path: {}", path.to_string_lossy());
28        path.to_string_lossy().to_string()
29    } else {
30        path_str.to_string()
31    }
32}
33
34/// Resolves the relative path to an absolute path.
35pub fn resolve_relative_path_with_current_dir(path_str: &str) -> Result<String> {
36    let current_dir = env::current_dir().context(GetCurrentDirSnafu)?;
37    Ok(resolve_relative_path(current_dir, path_str))
38}
39
40#[cfg(test)]
41mod tests {
42    use std::env;
43    use std::path::PathBuf;
44
45    use super::*;
46
47    #[test]
48    fn test_resolve_relative_path_absolute() {
49        let abs_path = if cfg!(windows) {
50            "C:\\foo\\bar"
51        } else {
52            "/foo/bar"
53        };
54        let current_dir = PathBuf::from("/tmp");
55        let result = resolve_relative_path(&current_dir, abs_path);
56        assert_eq!(result, abs_path);
57    }
58
59    #[test]
60    fn test_resolve_relative_path_relative() {
61        let current_dir = PathBuf::from("/tmp");
62        let rel_path = "foo/bar";
63        let expected = "/tmp/foo/bar";
64        let result = resolve_relative_path(&current_dir, rel_path);
65        // On Windows, the separator is '\', so normalize for comparison
66        // '/' is as a normal character in Windows paths
67        if cfg!(windows) {
68            assert!(result.ends_with("foo/bar"));
69            assert!(result.contains("/tmp\\"));
70        } else {
71            assert_eq!(result, expected);
72        }
73    }
74
75    #[test]
76    fn test_resolve_relative_path_with_current_dir_absolute() {
77        let abs_path = if cfg!(windows) {
78            "C:\\foo\\bar"
79        } else {
80            "/foo/bar"
81        };
82        let result = resolve_relative_path_with_current_dir(abs_path).unwrap();
83        assert_eq!(result, abs_path);
84    }
85
86    #[test]
87    fn test_resolve_relative_path_with_current_dir_relative() {
88        let rel_path = "foo/bar";
89        let current_dir = env::current_dir().unwrap();
90        let expected = current_dir.join(rel_path).to_string_lossy().to_string();
91        let result = resolve_relative_path_with_current_dir(rel_path).unwrap();
92        assert_eq!(result, expected);
93    }
94}