mirror of
https://github.com/bytedance/g3.git
synced 2026-05-03 14:10:39 +00:00
initial commit
This commit is contained in:
commit
13716f4923
1425 changed files with 163227 additions and 0 deletions
42
g3proxy/utils/lua/src/cmd_path.rs
Normal file
42
g3proxy/utils/lua/src/cmd_path.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright 2023 ByteDance and/or its affiliates.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use anyhow::anyhow;
|
||||
use clap::Command;
|
||||
use mlua::Lua;
|
||||
|
||||
pub const COMMAND: &str = "path";
|
||||
|
||||
pub fn command() -> Command {
|
||||
Command::new(COMMAND)
|
||||
}
|
||||
|
||||
pub fn display(lua: &Lua) -> anyhow::Result<()> {
|
||||
let path = lua
|
||||
.load("package.path")
|
||||
.eval::<String>()
|
||||
.map_err(|e| anyhow!("failed to load lua path: {e}"))?;
|
||||
|
||||
let cpath = lua
|
||||
.load("package.cpath")
|
||||
.eval::<String>()
|
||||
.map_err(|e| anyhow!("failed to load lua cpath: {e}"))?;
|
||||
|
||||
println!("lua path: {path}");
|
||||
println!("lua cpath: {cpath}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
101
g3proxy/utils/lua/src/cmd_run.rs
Normal file
101
g3proxy/utils/lua/src/cmd_run.rs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Copyright 2023 ByteDance and/or its affiliates.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use anyhow::anyhow;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command, ValueHint};
|
||||
use mlua::{Lua, Value};
|
||||
|
||||
pub const COMMAND: &str = "run";
|
||||
|
||||
const COMMAND_ARG_SCRIPT: &str = "script";
|
||||
const COMMAND_ARG_VERBOSE: &str = "verbose";
|
||||
|
||||
pub fn command() -> Command {
|
||||
Command::new(COMMAND)
|
||||
.arg(
|
||||
Arg::new(COMMAND_ARG_SCRIPT)
|
||||
.help("the script file to run")
|
||||
.value_name("SCRIPT FILE")
|
||||
.num_args(1)
|
||||
.value_parser(value_parser!(PathBuf))
|
||||
.value_hint(ValueHint::FilePath)
|
||||
.required(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new(COMMAND_ARG_VERBOSE)
|
||||
.help("output verbose level")
|
||||
.num_args(0)
|
||||
.action(ArgAction::Count)
|
||||
.short('v')
|
||||
.long("verbose"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn run(lua: &Lua, args: &ArgMatches) -> anyhow::Result<()> {
|
||||
let script = args
|
||||
.get_one::<PathBuf>(COMMAND_ARG_SCRIPT)
|
||||
.ok_or_else(|| anyhow!("no script file to run"))?;
|
||||
|
||||
let verbose_level = args
|
||||
.get_one::<u8>(COMMAND_ARG_VERBOSE)
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
let code = std::fs::read_to_string(script)
|
||||
.map_err(|e| anyhow!("failed to read script file {}: {e:?}", script.display()))?;
|
||||
|
||||
let code = lua.load(&code);
|
||||
|
||||
if verbose_level > 1 {
|
||||
println!("== script evaluation start ==");
|
||||
}
|
||||
let value = code
|
||||
.eval::<Value>()
|
||||
.map_err(|e| anyhow!("failed to run script file {}: {e}", script.display()))?;
|
||||
|
||||
if verbose_level > 1 {
|
||||
println!("== script evaluation end ==");
|
||||
println!("== returned data <{}> start ==", value.type_name());
|
||||
}
|
||||
if verbose_level > 0 {
|
||||
match &value {
|
||||
Value::Nil => {}
|
||||
Value::String(s) => {
|
||||
println!("{}", s.to_string_lossy());
|
||||
}
|
||||
Value::Boolean(v) => {
|
||||
println!("{}", *v);
|
||||
}
|
||||
Value::Integer(i) => {
|
||||
println!("{}", *i);
|
||||
}
|
||||
Value::Number(n) => {
|
||||
println!("{}", *n);
|
||||
}
|
||||
Value::Error(e) => {
|
||||
println!("{e}");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if verbose_level > 1 {
|
||||
println!("== returned data <{}> end ==", value.type_name());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
35
g3proxy/utils/lua/src/cmd_version.rs
Normal file
35
g3proxy/utils/lua/src/cmd_version.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright 2023 ByteDance and/or its affiliates.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use anyhow::anyhow;
|
||||
use clap::Command;
|
||||
use mlua::Lua;
|
||||
|
||||
pub const COMMAND: &str = "version";
|
||||
|
||||
pub fn command() -> Command {
|
||||
Command::new(COMMAND)
|
||||
}
|
||||
|
||||
pub fn display(lua: &Lua) -> anyhow::Result<()> {
|
||||
let version = lua
|
||||
.globals()
|
||||
.get::<_, String>("_VERSION")
|
||||
.map_err(|e| anyhow!("failed to get _VERSION variable: {e}"))?;
|
||||
|
||||
println!("lua version: {version}");
|
||||
Ok(())
|
||||
}
|
||||
67
g3proxy/utils/lua/src/main.rs
Normal file
67
g3proxy/utils/lua/src/main.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright 2023 ByteDance and/or its affiliates.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use anyhow::anyhow;
|
||||
use std::io;
|
||||
|
||||
use clap::{value_parser, Arg, Command};
|
||||
use clap_complete::Shell;
|
||||
use mlua::Lua;
|
||||
|
||||
mod cmd_path;
|
||||
mod cmd_run;
|
||||
mod cmd_version;
|
||||
|
||||
const GLOBAL_ARG_COMPLETION: &str = "completion";
|
||||
|
||||
fn build_cli_args() -> Command {
|
||||
Command::new("g3proxy-lua")
|
||||
.arg(
|
||||
Arg::new(GLOBAL_ARG_COMPLETION)
|
||||
.num_args(1)
|
||||
.value_name("SHELL")
|
||||
.long("completion")
|
||||
.value_parser(value_parser!(Shell))
|
||||
.exclusive(true),
|
||||
)
|
||||
.subcommand(cmd_version::command())
|
||||
.subcommand(cmd_path::command())
|
||||
.subcommand(cmd_run::command())
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let args = build_cli_args().get_matches();
|
||||
|
||||
if let Some(target) = args.get_one::<Shell>(GLOBAL_ARG_COMPLETION) {
|
||||
let mut app = build_cli_args();
|
||||
let bin_name = app.get_name().to_string();
|
||||
clap_complete::generate(*target, &mut app, bin_name, &mut io::stdout());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let lua = unsafe { Lua::unsafe_new() };
|
||||
|
||||
if let Some((cmd, args)) = args.subcommand() {
|
||||
match cmd {
|
||||
cmd_version::COMMAND => cmd_version::display(&lua),
|
||||
cmd_path::COMMAND => cmd_path::display(&lua),
|
||||
cmd_run::COMMAND => cmd_run::run(&lua, args),
|
||||
_ => Err(anyhow!("invalid subcommand {cmd}")),
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue