Skip to content

Commit ff92249

Browse files
add set env and fix error
1 parent 0fe0f52 commit ff92249

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
- 支持文件和目录的移动,通过 `mv` 命令进行操作。
3434
- 提供文件压缩和解压功能,通过 `tar` 命令进行操作。
3535
- 支持退出当前进程,通过 `exit` 命令进行操作。
36+
- 支持&用于优先级执行,&&顺序执行,|管道符与 >重定向输出
3637

3738
## 使用方法
3839

3940
Tiks 提供了类似于常见 Linux 终端的使用方法,下面是一些常用命令的示例:
41+
详见[命令](./command/src/commands/command.rs)
4042

4143
```bash
4244
Usage: <command> [options] [arg]
@@ -58,6 +60,7 @@ Commands:
5860
tar -zxvf: 解压缩文件
5961
tar -xvf: 压缩文件
6062
exit 退出当前进程
63+
......
6164
```
6265

6366
# Rust 安装工具

command/src/commands/arg.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub fn execute_command(command: &str, option: &str, arg: &Vec<String>, session_c
150150
// match has arg's function
151151
pub fn execute_other_command(command: &str, option: &str, arg: &[String]) -> Result<(usize,String), std::io::Error> {
152152
match command {
153+
"help" => Ok((0,help())),
153154
"pwd" => pwd(),
154155
"time" => get_time(),
155156
"history" => history(),
@@ -232,10 +233,6 @@ pub fn split(commands: Commands) -> (String,String,Vec<String>){
232233
(command,option,arg)
233234
}
234235

235-
// ["cat", "README.md", ">", "a.txt"]
236-
237-
//command: ["cat", "README.md"]
238-
//file: a.txt
239236
pub fn stdout_other(arg: &Vec<String>) -> (String,Commands){
240237
let mut s = arg.split(|s| s==">");
241238
let output = s.next().unwrap().to_vec();

command/src/commands/command.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn whoami(session_context: &mut SessionContext) -> io::Result<(usize,String)
2323
pub fn help() -> String{
2424
let help = format!(
2525
"Usage: <command> [options] [arg]
26-
\0\x1B[32mCommands:
26+
\0\x1B[32m Commands:
2727
pwd View current directory apt -i .. Install package
2828
ls View all files in the current directory history View past Commands
2929
cd Change directory whoami || apt -update version
@@ -313,7 +313,7 @@ use crate::commands::download::{download_package, find_package};
313313
use crate::priority::get_priority;
314314
use crate::set::set::file_create_time;
315315
use crate::run::run;
316-
use crate::state_code::{empty_dir, empty_file, missing_pattern, STATUE_CODE};
316+
use crate::state_code::{empty_dir, empty_file, env, missing_pattern, STATUE_CODE};
317317
use super::download::update;
318318
use crate::root::SessionContext;
319319

@@ -571,6 +571,30 @@ fn get_env(id: String) -> String{
571571
}
572572
}
573573

574+
use std::path::PathBuf;
575+
fn set_env_command(key: &str, path: &str) -> io::Result<()>{
576+
let mut path_set = Vec::new();
577+
let s = PathBuf::from(format!("{}",path));
578+
579+
path_set.push(s);
580+
581+
let new_path = env::join_paths(path_set).expect("Failed join paths");
582+
583+
env::set_var(key, new_path);
584+
Ok(())
585+
}
586+
587+
pub fn set(key: &str, path: &str) -> io::Result<(usize,String)>{
588+
match set_env_command(key, path){
589+
Ok(())=>{
590+
return Ok(env());
591+
},
592+
Err(_) =>{
593+
let error_str = format!("Can't set env {}",key);
594+
return Ok((108,error_str));
595+
}
596+
}
597+
}
574598

575599
pub fn echo_print<T: std::fmt::Display + From<String>>(output: T) -> (usize,T){
576600
let var = format!("{}", output);

0 commit comments

Comments
 (0)