-
Notifications
You must be signed in to change notification settings - Fork 1
/
paths.rs
172 lines (142 loc) · 4.85 KB
/
paths.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use super::config::*;
use super::shells::SupportedShell;
use glob::glob;
pub const DEFAULT_ORBITER_HOME: &str = ".orbiter";
pub const DEFAULT_ORBITER_PAYLOADS_HOME: &str = "payloads";
pub const DEFAULT_ORBITER_PAYLOADS_CURRENT_INSTALL_HOME: &str = "current";
pub const DEFAULT_ORBITER_DASHBOARD_HOME: &str = "dashboard";
pub const DEFAULT_ORBITER_DASHBOARD_BIN_HOME: &str = "bin";
pub const DEFAULT_ORBITER_CONFIG_FILENAME: &str = ".orbiter.config.yml";
pub const DEFAULT_ORBITER_PAYLOAD_CONFIG_DIR: &str = ".__orbiter__";
pub const ORBITER_CONFIG_ENV_KEY: &str = "ORBITER_CONFIG";
pub const ORBITER_HOME_ENV_KEY: &str = "ORBITER_HOME";
// .orbiter.config.yml
fn get_dflt_config_path() -> PathBuf {
dirs::home_dir()
.unwrap()
.join(DEFAULT_ORBITER_CONFIG_FILENAME)
}
pub fn get_config_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
Ok(match env::var(ORBITER_CONFIG_ENV_KEY) {
Ok(orbiter_config_envvar) => {
let path = Path::new(&orbiter_config_envvar);
if path.exists() {
path.to_path_buf()
} else {
get_dflt_config_path()
}
}
Err(_) => get_dflt_config_path(),
})
}
// .orbiter/
fn get_dflt_home_dir_path() -> PathBuf {
dirs::home_dir().unwrap().join(DEFAULT_ORBITER_HOME)
}
pub fn get_home_dir_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
Ok(match env::var(ORBITER_HOME_ENV_KEY) {
Ok(orbiter_config_envvar) => {
let path = Path::new(&orbiter_config_envvar);
if path.exists() {
path.to_path_buf()
} else {
get_dflt_home_dir_path()
}
}
Err(_) => get_dflt_home_dir_path(),
})
}
// .orbiter/payloads/<payload id>
pub fn get_payload_dir_path(payload: &Payload) -> Result<PathBuf, Box<dyn std::error::Error>> {
Ok(get_home_dir_path()?
.join(DEFAULT_ORBITER_PAYLOADS_HOME)
.join(payload.id.as_ref().unwrap()))
}
// .orbiter/payloads/<payload id>/current
pub fn get_payload_current_install_dir_path(
payload: &Payload,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
let dir = get_payload_dir_path(payload)?.join(DEFAULT_ORBITER_PAYLOADS_CURRENT_INSTALL_HOME);
Ok(dir)
}
// .orbiter/payloads/<payload id>/.__orbiter__
pub fn get_payload_config_dir_path(
payload: &Payload,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
let payload_config_dir =
get_payload_dir_path(payload)?.join(DEFAULT_ORBITER_PAYLOAD_CONFIG_DIR);
Ok(payload_config_dir)
}
pub fn get_bin_dir_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
let bin_path = get_home_dir_path()?
.join(DEFAULT_ORBITER_DASHBOARD_HOME)
.join(DEFAULT_ORBITER_DASHBOARD_BIN_HOME);
Ok(bin_path)
}
pub fn get_bin_file_path(bin_fname: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
let bin_file_path = get_bin_dir_path()?.join(bin_fname);
Ok(bin_file_path)
}
pub fn resolve_single_path(file_path: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
if file_path.contains("*") {
let entry = glob(&file_path)?
.next()
.ok_or(format!("unable to locate {}", file_path))??;
return Ok(fs::canonicalize(&entry)?);
}
Ok(fs::canonicalize(file_path)?)
}
pub fn get_file_name(func: &str) -> Result<String, Box<dyn std::error::Error>> {
let file_name = Path::new(&func)
.file_name()
.and_then(OsStr::to_str)
.unwrap();
Ok(String::from(file_name))
}
pub fn get_dir(bin_path: &PathBuf) -> Result<PathBuf, Box<dyn std::error::Error>> {
let mut bin_path = bin_path.clone();
bin_path.pop(); // get parent dir path
Ok(bin_path)
}
pub fn update_path(current_shell: &SupportedShell) {
match current_shell {
SupportedShell::Sh => export_path_sh(),
SupportedShell::Bash => export_path_sh(),
SupportedShell::Zsh => export_path_sh(),
SupportedShell::Fish => export_path_fish(),
SupportedShell::PowerShell => export_path_powershell(),
SupportedShell::WinCmd => export_path_wincmd(),
}
}
fn export_path_sh() {
// update PATH with orbiter dashboard bin dir
println!(
"export PATH=\"{}:$PATH\"",
get_bin_dir_path().unwrap().display()
);
}
fn export_path_fish() {
// update PATH with orbiter dashboard bin dir
println!(
"set -x PATH \"{}\" $PATH",
get_bin_dir_path().unwrap().display()
);
}
fn export_path_powershell() {
// update PATH with orbiter dashboard bin dir
println!(
"$env:PATH = \"{};$env:PATH\"",
get_bin_dir_path().unwrap().display()
);
}
fn export_path_wincmd() {
// update PATH with orbiter dashboard bin dir
println!(
"setx PATH \"{};%PATH%\"",
get_bin_dir_path().unwrap().display()
);
}