Skip to content

Commit

Permalink
feat: add cli parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Dec 10, 2023
1 parent 5ef359a commit e999d8e
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 2 deletions.
162 changes: 162 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ name = "proxy-rs"
version = "0.1.0"
edition = "2021"
description = "A simple proxy config tool."
authors = ["Mystic"]
repository = "https://github.com/pplmx/proxy-rs"
license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
# Name of the binary (optional, default is package name)
name = "x"
path = "src/main.rs"

[dependencies]
clap = "4.4.11"
29 changes: 28 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
.PHONY: help
.PHONY: help build check run
.DEFAULT_GOAL := help

BIN_NAME := x

# cargo build
build:
@cargo build

# cargo check
check:
@cargo check

# clean cargo cache
clean:
@cargo clean

# cargo build --release
release:
@cargo build --release

# cargo run
run:
@cargo run

# run binary generated by cargo build
run-bin:
@./target/debug/$(BIN_NAME)


# Show help
help:
@echo ""
Expand Down
34 changes: 34 additions & 0 deletions src/cmd/root.rs
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();
}
}
52 changes: 52 additions & 0 deletions src/lib.rs
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);
}
}
6 changes: 5 additions & 1 deletion src/main.rs
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();
}

0 comments on commit e999d8e

Please sign in to comment.