Skip to content

Commit 7ce86bf

Browse files
committed
a demo of running command
1 parent acd21b4 commit 7ce86bf

File tree

5 files changed

+90
-56
lines changed

5 files changed

+90
-56
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
2-
name = "calcit_dylib"
2+
name = "calcit_command"
33
version = "0.0.1"
44
authors = ["jiyinyiyong <[email protected]>"]
55
edition = "2021"
66

77
[lib]
8-
name = "calcit_std"
8+
name = "calcit_command"
99
path = "src/lib.rs"
1010
crate-type = ["dylib"] # Creates dynamic lib
1111

calcit.cirru

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compact.cirru

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11

2-
{} (:package |lib)
3-
:configs $ {} (:init-fn |lib.test/main!) (:reload-fn |lib.test/reload!) (:version |0.0.1)
2+
{} (:package |command)
3+
:configs $ {} (:init-fn |command.test/main!) (:reload-fn |command.test/reload!) (:version |0.0.1)
44
:modules $ []
55
:entries $ {}
66
:files $ {}
7-
|lib.core $ %{} :FileEntry
7+
|command.core $ %{} :FileEntry
88
:defs $ {}
9-
|path-exists? $ %{} :CodeEntry (:doc |)
9+
|run-command $ %{} :CodeEntry (:doc |)
1010
:code $ quote
11-
defn path-exists? (name)
12-
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"path_exists" name
11+
defn run-command (name & args)
12+
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_command") "\"run_command" name & args
1313
:ns $ %{} :CodeEntry (:doc |)
1414
:code $ quote
15-
ns lib.core $ :require
16-
lib.$meta :refer $ calcit-dirname
17-
lib.util :refer $ get-dylib-path
18-
|lib.test $ %{} :FileEntry
15+
ns command.core $ :require
16+
command.$meta :refer $ calcit-dirname
17+
command.util :refer $ get-dylib-path
18+
|command.test $ %{} :FileEntry
1919
:defs $ {}
2020
|main! $ %{} :CodeEntry (:doc |)
2121
:code $ quote
@@ -26,13 +26,13 @@
2626
|run-tests $ %{} :CodeEntry (:doc |)
2727
:code $ quote
2828
defn run-tests () (println "\"%%%% test for lib") (println calcit-filename calcit-dirname)
29-
println (path-exists? "\"README.md") (path-exists? "\"build.js")
29+
println $ run-command "\"ls"
3030
:ns $ %{} :CodeEntry (:doc |)
3131
:code $ quote
32-
ns lib.test $ :require
33-
lib.core :refer $ path-exists?
34-
lib.$meta :refer $ calcit-dirname calcit-filename
35-
|lib.util $ %{} :FileEntry
32+
ns command.test $ :require
33+
command.core :refer $ run-command
34+
command.$meta :refer $ calcit-dirname calcit-filename
35+
|command.util $ %{} :FileEntry
3636
:defs $ {}
3737
|get-dylib-ext $ %{} :CodeEntry (:doc |)
3838
:code $ quote
@@ -47,5 +47,5 @@
4747
if (blank? p) "\"." p
4848
:ns $ %{} :CodeEntry (:doc |)
4949
:code $ quote
50-
ns lib.util $ :require
51-
lib.$meta :refer $ calcit-dirname calcit-filename
50+
ns command.util $ :require
51+
command.$meta :refer $ calcit-dirname calcit-filename

src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cirru_edn::Edn;
22
use std::path::Path;
3+
use std::process::Command;
34

45
#[no_mangle]
56
pub fn abi_version() -> String {
@@ -18,3 +19,36 @@ pub fn path_exists(args: Vec<Edn>) -> Result<Edn, String> {
1819
Err(format!("path-exists? expected 1 arg, got {:?}", args))
1920
}
2021
}
22+
23+
24+
/// simple command to run a command, without options
25+
#[no_mangle]
26+
pub fn run_command(args: Vec<Edn>) -> Result<Edn, String> {
27+
let mut xs = vec![];
28+
for arg in args.iter() {
29+
if let Edn::Str(name) = arg {
30+
xs.push((*name).to_owned());
31+
} else {
32+
return Err(format!("run-command expected 1 filename, got {:?}", args))
33+
}
34+
}
35+
if xs.is_empty() {
36+
Err(format!("run-command expected at least 1 arg, got {:?}", args))
37+
} else {
38+
let name = xs.remove(0);
39+
let mut command = Command::new(&(*name));
40+
for arg in xs.iter() {
41+
command.arg(&*(*arg).to_owned());
42+
}
43+
44+
let output = command.output()
45+
.expect("failed to execute process");
46+
if output.status.success() {
47+
48+
Ok(Edn::str(String::from_utf8_lossy(&output.stdout).to_string()))
49+
} else {
50+
Err(format!("run-command failed: {}", String::from_utf8_lossy(&output.stderr)))
51+
}
52+
}
53+
}
54+

0 commit comments

Comments
 (0)