Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rust-shell-script/rust-shell-script
Rustlike shell scripting language; Resilient & robust shell script, compiling to rust code/bash script
https://github.com/rust-shell-script/rust-shell-script
command-line rust shell-script
Last synced: 6 days ago
JSON representation
Rustlike shell scripting language; Resilient & robust shell script, compiling to rust code/bash script
- Host: GitHub
- URL: https://github.com/rust-shell-script/rust-shell-script
- Owner: rust-shell-script
- License: apache-2.0
- Created: 2019-09-07T17:41:50.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-22T05:07:21.000Z (about 3 years ago)
- Last Synced: 2024-10-31T19:42:18.782Z (13 days ago)
- Topics: command-line, rust, shell-script
- Language: Rust
- Homepage:
- Size: 30.3 KB
- Stars: 58
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rust-shell-script
~~Rustlike shell scripting language (WIP)~~ -> use [rust_cmd_lib](https://github.com/rust-shell-script/rust_cmd_lib) instead.Resilient & robust shell script, compiling to rust code/bash script
# Build & Install
```bash
$ cargo build
```# Example
## Source file
```bash
rust-shell-script$:~/rust-shell-script$ cat examples/hello.rss
cmd error_command() {
do_something_failed
}fun bad_greeting(name) {
info "Running error_command ..."
error_command
output "hello, ${name}!"
}fun good_greeting(name) {
info "Running good_command ..."
output "hello, ${name}!"
}cmd main() {
let bad_ans = $(bad_greeting "rust-shell-script")
info "${bad_ans}"
let good_ans = $(good_greeting "rust-shell-script")
info "${good_ans}"
return 0
}
```## Compiling to bash
### compiling
```bash
rust-shell-script:~/rust-shell-script$ target/debug/rust-shell-script -t bash examples/hello.rss
Generating bash script to examples/hello.sh ...
```### passing shellcheck
```bash
rust-shell-script:~/rust-shell-script$ shellcheck examples/hello.sh
rust-shell-script:~/rust-shell-script$ echo $?
0
```### generated bash script
```bash
#!/bin/bash
#
# Generated by rust-shell-script
#
. "${RUNTIME:-.}/cmd_lib.sh"error_command() {
do_something_failed
}function bad_greeting() {
local name="$1"info "Running error_command ..."
error_command
output "hello, ${name}!"
}function good_greeting() {
local name="$1"info "Running good_command ..."
output "hello, ${name}!"
}main() {
local bad_ans
bad_ans=$(_call bad_greeting "rust-shell-script")
info "${bad_ans}"
local good_ans
good_ans=$(_call good_greeting "rust-shell-script")
info "${good_ans}"
return 0
}main "$@"
```### running script
All command errors will be captured automatically, even within bash function:
```bash
rust-shell-script:~/rust-shell-script/examples$ ./hello.sh
Running error_command ...
/bin/bash: line 219: do_something_failed: command not found
FATAL: Running command (bad_ans=$(_call bad_greeting "rust-shell-script")) near script hello.sh:main():1 failed (ret=127)
```## Compiling to rust
### compiling
```bash
tao@sophia:~/rust-shell-script$ target/debug/rust-shell-script -t rust examples/hello.rss
Generating rust code to examples/hello.rs ...
```### generated rust code
```rust
// Generated by rust-shell-script
mod cmd_lib;
use crate::cmd_lib::{CmdResult, FunResult};fn error_command() -> CmdResult {
run_cmd!("do_something_failed")
}fn bad_greeting(name: &str) -> FunResult {
info!("Running error_command ...");
error_command()?;
output!("hello, {}!", name)
}fn good_greeting(name: &str) -> FunResult {
info!("Running good_command ...");
output!("hello, {}!", name)
}fn main() -> CmdResult {
let bad_ans = bad_greeting("rust-shell-script")?;;
info!("{}", bad_ans);
let good_ans = good_greeting("rust-shell-script")?;;
info!("{}", good_ans);
return Ok(())
}
```### running rust code
```bash
rust-shell-script:~/rust-shell-script/examples$ rustc hello.rs
rust-shell-script:~/rust-shell-script/examples$ ./hello
Running error_command ...
Running ["do_something_failed"] ...
Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
```