pub struct EnvPair {
    pub key: String,
    pub val: String,
}

Fields§

§key: String§val: String

Implementations§

使用此函式前需確保 line 非空字串

Examples found in repository?
src/util/main_util.rs (line 248)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
pub async fn run_n_times(
    repeat: u64,
    dummy: bool,
    entry: &mut RepoEntry<'_>,
    mut args: Vec<String>,
    res: &mut Vec<Error>,
    use_previous: bool,
    error_no_previous: bool,
    dir: Option<PathBuf>,
) -> Result {
    log::info!("執行 {:?}", entry.name);
    super::hijack_ctrlc_once();

    let mut env_vec = vec![];
    if use_previous {
        let dir = super::option_map_res(dir, |d| path::normalize_path(d))?;
        let historian = &entry.get_env().historian;
        match historian.previous_args(entry.id, dir.as_deref()).await? {
            None if error_no_previous => {
                return Err(Error::NoPreviousArgs);
            }
            None => log::warn!("無前一次參數,當作空的"),
            Some((arg_str, envs_str)) => {
                log::debug!("撈到前一次呼叫的參數 {}", arg_str);
                let mut prev_arg_vec: Vec<String> =
                    serde_json::from_str(&arg_str).context(format!("反序列失敗 {}", arg_str))?;
                env_vec =
                    serde_json::from_str(&envs_str).context(format!("反序列失敗 {}", envs_str))?;
                prev_arg_vec.extend(args.into_iter());
                args = prev_arg_vec;
            }
        }
    }

    let here = path::normalize_path(".").ok();
    let script_path = path::open_script(&entry.name, &entry.ty, Some(true))?;
    let content = super::read_file(&script_path)?;

    let mut hs_env_desc = vec![];
    for (need_save, line) in extract_env_from_content_help_aware(&content) {
        hs_env_desc.push(line.to_owned());
        if need_save {
            EnvPair::process_line(line, &mut env_vec);
        }
    }
    EnvPair::sort(&mut env_vec);
    let env_record = serde_json::to_string(&env_vec)?;

    let run_id = entry
        .update(|info| info.exec(content, &args, env_record, here))
        .await?;

    if dummy {
        log::info!("--dummy 不用真的執行,提早退出");
        return Ok(());
    }
    // Start packing hs tmpl val
    let hs_home = path::get_home();
    let hs_tags: Vec<_> = entry.tags.iter().map(|t| t.as_ref()).collect();
    let hs_cmd = std::env::args().next().unwrap_or_default();

    let hs_exe = std::env::current_exe()?;
    let hs_exe = hs_exe.to_string_lossy();

    let content = &entry.exec_time.as_ref().unwrap().data().unwrap().0;
    let hs_tmpl_val = json!({
        "path": script_path,
        "home": hs_home,
        "run_id": run_id,
        "tags": hs_tags,
        "cmd": hs_cmd,
        "exe": hs_exe,
        "env_desc": hs_env_desc,
        "name": &entry.name.key(),
        "content": content,
    });
    // End packing hs tmpl val

    for _ in 0..repeat {
        let run_res = run(&script_path, &*entry, &args, &hs_tmpl_val, &env_vec);
        let ret_code: i32;
        match run_res {
            Err(Error::ScriptError(code)) => {
                ret_code = code;
                res.push(run_res.unwrap_err());
            }
            Err(e) => return Err(e),
            Ok(_) => ret_code = 0,
        }
        entry
            .update(|info| info.exec_done(ret_code, run_id))
            .await?;
    }
    Ok(())
}
Examples found in repository?
src/util/main_util.rs (line 251)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
pub async fn run_n_times(
    repeat: u64,
    dummy: bool,
    entry: &mut RepoEntry<'_>,
    mut args: Vec<String>,
    res: &mut Vec<Error>,
    use_previous: bool,
    error_no_previous: bool,
    dir: Option<PathBuf>,
) -> Result {
    log::info!("執行 {:?}", entry.name);
    super::hijack_ctrlc_once();

    let mut env_vec = vec![];
    if use_previous {
        let dir = super::option_map_res(dir, |d| path::normalize_path(d))?;
        let historian = &entry.get_env().historian;
        match historian.previous_args(entry.id, dir.as_deref()).await? {
            None if error_no_previous => {
                return Err(Error::NoPreviousArgs);
            }
            None => log::warn!("無前一次參數,當作空的"),
            Some((arg_str, envs_str)) => {
                log::debug!("撈到前一次呼叫的參數 {}", arg_str);
                let mut prev_arg_vec: Vec<String> =
                    serde_json::from_str(&arg_str).context(format!("反序列失敗 {}", arg_str))?;
                env_vec =
                    serde_json::from_str(&envs_str).context(format!("反序列失敗 {}", envs_str))?;
                prev_arg_vec.extend(args.into_iter());
                args = prev_arg_vec;
            }
        }
    }

    let here = path::normalize_path(".").ok();
    let script_path = path::open_script(&entry.name, &entry.ty, Some(true))?;
    let content = super::read_file(&script_path)?;

    let mut hs_env_desc = vec![];
    for (need_save, line) in extract_env_from_content_help_aware(&content) {
        hs_env_desc.push(line.to_owned());
        if need_save {
            EnvPair::process_line(line, &mut env_vec);
        }
    }
    EnvPair::sort(&mut env_vec);
    let env_record = serde_json::to_string(&env_vec)?;

    let run_id = entry
        .update(|info| info.exec(content, &args, env_record, here))
        .await?;

    if dummy {
        log::info!("--dummy 不用真的執行,提早退出");
        return Ok(());
    }
    // Start packing hs tmpl val
    let hs_home = path::get_home();
    let hs_tags: Vec<_> = entry.tags.iter().map(|t| t.as_ref()).collect();
    let hs_cmd = std::env::args().next().unwrap_or_default();

    let hs_exe = std::env::current_exe()?;
    let hs_exe = hs_exe.to_string_lossy();

    let content = &entry.exec_time.as_ref().unwrap().data().unwrap().0;
    let hs_tmpl_val = json!({
        "path": script_path,
        "home": hs_home,
        "run_id": run_id,
        "tags": hs_tags,
        "cmd": hs_cmd,
        "exe": hs_exe,
        "env_desc": hs_env_desc,
        "name": &entry.name.key(),
        "content": content,
    });
    // End packing hs tmpl val

    for _ in 0..repeat {
        let run_res = run(&script_path, &*entry, &args, &hs_tmpl_val, &env_vec);
        let ret_code: i32;
        match run_res {
            Err(Error::ScriptError(code)) => {
                ret_code = code;
                res.push(run_res.unwrap_err());
            }
            Err(e) => return Err(e),
            Ok(_) => ret_code = 0,
        }
        entry
            .update(|info| info.exec_done(ret_code, run_id))
            .await?;
    }
    Ok(())
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.