-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
288 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use clap::{Command, Arg, ArgAction}; | ||
use proxy_rs::proxy_manager; | ||
|
||
pub fn execute() { | ||
let mut command = Command::new("Proxy Manager") | ||
.version("1.0") | ||
.author("Mystic") | ||
.about("Manages proxy settings for git and npm") | ||
.arg( | ||
Arg::new("enable") | ||
.long("enable") | ||
.action(ArgAction::Set) // 设置为 true 当标志被使用时 | ||
.value_name("PROXY_URL") | ||
.help("Enables the proxy with the given URL") | ||
.num_args(1), // 接受一个参数 | ||
) | ||
.arg( | ||
Arg::new("disable") | ||
.long("disable") | ||
.action(ArgAction::SetFalse) // 设置为 false 当标志被使用时 | ||
.help("Disables the proxy") | ||
.num_args(0), // 不接受参数 | ||
); | ||
|
||
let matches = command.clone().get_matches(); | ||
|
||
if let Some(proxy_url) = matches.get_one::<String>("enable") { | ||
proxy_manager::enable_proxy(proxy_url); | ||
} else if matches.contains_id("disable") { | ||
proxy_manager::disable_proxy(); | ||
} else { | ||
command.print_help().unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// lib.rs | ||
pub mod proxy_manager { | ||
use std::process::Command; | ||
|
||
pub fn enable_proxy(proxy_url: &str) { | ||
set_config("http.proxy", proxy_url); | ||
set_config("https.proxy", proxy_url); | ||
set_env("all_proxy", proxy_url); | ||
set_env("http_proxy", proxy_url); | ||
set_env("https_proxy", proxy_url); | ||
println!("Proxy enabled"); | ||
} | ||
|
||
pub fn disable_proxy() { | ||
unset_config("http.proxy"); | ||
unset_config("https.proxy"); | ||
unset_env("all_proxy"); | ||
unset_env("http_proxy"); | ||
unset_env("https_proxy"); | ||
println!("Proxy disabled"); | ||
} | ||
|
||
fn set_config(key: &str, value: &str) { | ||
Command::new("git") | ||
.args(&["config", "--global", key, value]) | ||
.output() | ||
.expect("Failed to execute command"); | ||
Command::new("npm") | ||
.args(&["config", "set", "proxy", value]) | ||
.output() | ||
.expect("Failed to execute command"); | ||
} | ||
|
||
fn unset_config(key: &str) { | ||
Command::new("git") | ||
.args(&["config", "--global", "--unset", key]) | ||
.output() | ||
.expect("Failed to execute command"); | ||
Command::new("npm") | ||
.args(&["config", "delete", "proxy"]) | ||
.output() | ||
.expect("Failed to execute command"); | ||
} | ||
|
||
fn set_env(key: &str, value: &str) { | ||
std::env::set_var(key, value); | ||
} | ||
|
||
fn unset_env(key: &str) { | ||
std::env::remove_var(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
mod cmd { | ||
pub mod root; | ||
} | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
cmd::root::execute(); | ||
} |