Struct hyper_scripter::script_type::ScriptTypeConfig
source · pub struct ScriptTypeConfig {
pub ext: Option<String>,
pub color: String,
pub cmd: Option<String>,
/* private fields */
}Fields§
§ext: Option<String>§color: String§cmd: Option<String>Implementations§
source§impl ScriptTypeConfig
impl ScriptTypeConfig
sourcepub fn args(&self, info: &Value) -> Result<Vec<String>, Error>
pub fn args(&self, info: &Value) -> Result<Vec<String>, Error>
Examples found in repository?
src/util/main_util.rs (line 188)
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
fn run(
script_path: &Path,
info: &ScriptInfo,
remaining: &[String],
hs_tmpl_val: &serde_json::Value,
remaining_envs: &[EnvPair],
) -> Result<()> {
let conf = Config::get();
let ty = &info.ty;
let script_conf = conf.get_script_conf(ty)?;
let cmd_str = if let Some(cmd) = &script_conf.cmd {
cmd
} else {
return Err(Error::PermissionDenied(vec![script_path.to_path_buf()]));
};
let env = conf.gen_env(&hs_tmpl_val)?;
let ty_env = script_conf.gen_env(&hs_tmpl_val)?;
let pre_run_script = prepare_pre_run(None)?;
let (cmd, shebang) = super::shebang_handle::handle(&pre_run_script)?;
let args = shebang
.iter()
.map(|s| s.as_ref())
.chain(std::iter::once(pre_run_script.as_os_str()))
.chain(remaining.iter().map(|s| s.as_ref()));
let set_cmd_envs = |cmd: &mut Command| {
cmd.envs(ty_env.iter().map(|(a, b)| (a, b)));
cmd.envs(env.iter().map(|(a, b)| (a, b)));
cmd.envs(remaining_envs.iter().map(|p| (&p.key, &p.val)));
};
let mut cmd = super::create_cmd(cmd, args);
set_cmd_envs(&mut cmd);
let stat = super::run_cmd(cmd)?;
log::info!("預腳本執行結果:{:?}", stat);
if !stat.success() {
// TODO: 根據返回值做不同表現
let code = stat.code().unwrap_or_default();
return Err(Error::PreRunError(code));
}
let args = script_conf.args(&hs_tmpl_val)?;
let full_args = args
.iter()
.map(|s| s.as_str())
.chain(remaining.iter().map(|s| s.as_str()));
let mut cmd = super::create_cmd(&cmd_str, full_args);
set_cmd_envs(&mut cmd);
let stat = super::run_cmd(cmd)?;
log::info!("程式執行結果:{:?}", stat);
if !stat.success() {
let code = stat.code().unwrap_or_default();
Err(Error::ScriptError(code))
} else {
Ok(())
}
}sourcepub fn gen_env(&self, info: &Value) -> Result<Vec<(String, String)>, Error>
pub fn gen_env(&self, info: &Value) -> Result<Vec<(String, String)>, Error>
Examples found in repository?
src/util/main_util.rs (line 161)
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
fn run(
script_path: &Path,
info: &ScriptInfo,
remaining: &[String],
hs_tmpl_val: &serde_json::Value,
remaining_envs: &[EnvPair],
) -> Result<()> {
let conf = Config::get();
let ty = &info.ty;
let script_conf = conf.get_script_conf(ty)?;
let cmd_str = if let Some(cmd) = &script_conf.cmd {
cmd
} else {
return Err(Error::PermissionDenied(vec![script_path.to_path_buf()]));
};
let env = conf.gen_env(&hs_tmpl_val)?;
let ty_env = script_conf.gen_env(&hs_tmpl_val)?;
let pre_run_script = prepare_pre_run(None)?;
let (cmd, shebang) = super::shebang_handle::handle(&pre_run_script)?;
let args = shebang
.iter()
.map(|s| s.as_ref())
.chain(std::iter::once(pre_run_script.as_os_str()))
.chain(remaining.iter().map(|s| s.as_ref()));
let set_cmd_envs = |cmd: &mut Command| {
cmd.envs(ty_env.iter().map(|(a, b)| (a, b)));
cmd.envs(env.iter().map(|(a, b)| (a, b)));
cmd.envs(remaining_envs.iter().map(|p| (&p.key, &p.val)));
};
let mut cmd = super::create_cmd(cmd, args);
set_cmd_envs(&mut cmd);
let stat = super::run_cmd(cmd)?;
log::info!("預腳本執行結果:{:?}", stat);
if !stat.success() {
// TODO: 根據返回值做不同表現
let code = stat.code().unwrap_or_default();
return Err(Error::PreRunError(code));
}
let args = script_conf.args(&hs_tmpl_val)?;
let full_args = args
.iter()
.map(|s| s.as_str())
.chain(remaining.iter().map(|s| s.as_str()));
let mut cmd = super::create_cmd(&cmd_str, full_args);
set_cmd_envs(&mut cmd);
let stat = super::run_cmd(cmd)?;
log::info!("程式執行結果:{:?}", stat);
if !stat.success() {
let code = stat.code().unwrap_or_default();
Err(Error::ScriptError(code))
} else {
Ok(())
}
}sourcepub fn default_script_types() -> HashMap<ScriptType, ScriptTypeConfig>
pub fn default_script_types() -> HashMap<ScriptType, ScriptTypeConfig>
Examples found in repository?
src/config.rs (line 135)
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
fn default() -> Self {
fn gen_alias(from: &str, after: &[&str]) -> (String, Alias) {
(
from.to_owned(),
Alias {
after: after.iter().map(|s| s.to_string()).collect(),
},
)
}
Config {
last_modified: None,
recent: Some(999999), // NOTE: 顯示兩千多年份的資料!
editor: vec!["vim".to_string()],
prompt_level: PromptLevel::Smart,
tag_selectors: vec![
NamedTagSelector {
content: "+pin,util".parse().unwrap(),
name: "pin".to_owned(),
inactivated: false,
},
NamedTagSelector {
content: "+all,^hide!".parse().unwrap(),
name: "no-hidden".to_owned(),
inactivated: false,
},
NamedTagSelector {
content: "+all,^remove!".parse().unwrap(),
name: "no-removed".to_owned(),
inactivated: false,
},
],
main_tag_selector: "+all".parse().unwrap(),
types: ScriptTypeConfig::default_script_types(),
alias: [
gen_alias("la", &["ls", "-a"]),
gen_alias("ll", &["ls", "-l"]),
gen_alias("l", &["ls", "--grouping", "none", "--limit", "5"]),
gen_alias("e", &["edit"]),
gen_alias("gc", &["rm", "--timeless", "--purge", "-s", "remove", "*"]),
gen_alias("t", &["tags"]),
gen_alias("p", &["run", "--previous"]),
gen_alias("pc", &["=util/historian!", "--sequence", "c", "--show-env"]),
gen_alias("pr", &["=util/historian!", "--sequence", "r", "--show-env"]),
gen_alias("purge", &["rm", "--purge"]),
gen_alias("h", &["=util/historian!", "--show-env"]),
]
.into_iter()
.collect(),
env: [
("NAME", "{{name}}"),
("HS_HOME", "{{home}}"),
("HS_CMD", "{{cmd}}"),
("HS_RUN_ID", "{{run_id}}"),
(
"HS_TAGS",
"{{#each tags}}{{{this}}}{{#unless @last}} {{/unless}}{{/each}}",
),
(
"HS_ENV_DESC",
"{{#each env_desc}}{{{this}}}{{#unless @last}}\n{{/unless}}{{/each}}",
),
("HS_EXE", "{{exe}}"),
("HS_SOURCE", "{{home}}/.hs_source"),
("TMP_DIR", "/tmp"),
]
.into_iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect(),
}
}Trait Implementations§
source§impl Clone for ScriptTypeConfig
impl Clone for ScriptTypeConfig
source§fn clone(&self) -> ScriptTypeConfig
fn clone(&self) -> ScriptTypeConfig
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl Debug for ScriptTypeConfig
impl Debug for ScriptTypeConfig
source§impl<'de> Deserialize<'de> for ScriptTypeConfig
impl<'de> Deserialize<'de> for ScriptTypeConfig
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl PartialEq<ScriptTypeConfig> for ScriptTypeConfig
impl PartialEq<ScriptTypeConfig> for ScriptTypeConfig
source§fn eq(&self, other: &ScriptTypeConfig) -> bool
fn eq(&self, other: &ScriptTypeConfig) -> bool
This method tests for
self and other values to be equal, and is used
by ==.source§impl Serialize for ScriptTypeConfig
impl Serialize for ScriptTypeConfig
impl Eq for ScriptTypeConfig
impl StructuralEq for ScriptTypeConfig
impl StructuralPartialEq for ScriptTypeConfig
Auto Trait Implementations§
impl RefUnwindSafe for ScriptTypeConfig
impl Send for ScriptTypeConfig
impl Sync for ScriptTypeConfig
impl Unpin for ScriptTypeConfig
impl UnwindSafe for ScriptTypeConfig
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.