Skip to content

Commit

Permalink
Shell variable interaction (amber-lang#276)
Browse files Browse the repository at this point in the history
* Shell variable interaction

* remove amber_test println

* return type Bool

now shelle_var_set and shell_unset return true or false

* Update src/std/main.ab

Co-authored-by: Phoenix Himself <[email protected]>

* Update src/std/main.ab

Co-authored-by: Phoenix Himself <[email protected]>

* Update src/std/main.ab

Co-authored-by: Phoenix Himself <[email protected]>

* Update src/std/main.ab

Co-authored-by: Phoenix Himself <[email protected]>

* Update src/std/main.ab

Co-authored-by: Phoenix Himself <[email protected]>

* add shell_constant_get test

* define set/get/unset shell_var function as faillabe

---------

Co-authored-by: Phoenix Himself <[email protected]>
  • Loading branch information
CymDeveloppement and Ph0enixKM authored Jul 5, 2024
1 parent 4ec4486 commit 2b96f87
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/std/main.ab
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,30 @@ pub fun get_env_var(var: Text): Text {
pub fun load_env_file(): Null {
unsafe $export "\$(xargs < .env)" > /dev/null$
}

pub fun shell_isset(name: Text): Bool {
$[[ ! -z \$\{!{nameof name}+z} ]]$ failed {
return false
}
return true
}

pub fun shell_constant_set(name: Text, val: Text): Null {
$readonly \${nameof name}="\${nameof val}" 2> /dev/null$?
}

pub fun shell_constant_get(name: Text): Text {
return $echo \$\{!{nameof name}}$?
}

pub fun shell_var_set(name: Text, val: Text): Null {
$declare -g \${nameof name}="\${nameof val}" 2> /dev/null$?
}

pub fun shell_var_get(name: Text): Text {
return $echo \$\{!{nameof name}}$?
}

pub fun shell_unset(name: Text): Null {
$unset {name}$?
}
5 changes: 5 additions & 0 deletions src/tests/stdlib/shell_constant_get.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * from "std"
main {
unsafe shell_constant_set("test_shell_constant_get", "Hello Amber!")
unsafe $echo "\$test_shell_constant_get"$
}
1 change: 1 addition & 0 deletions src/tests/stdlib/shell_constant_get.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Amber!
10 changes: 10 additions & 0 deletions src/tests/stdlib/shell_constant_set.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * from "std"
main {
unsafe shell_constant_set("test_shell_constant_set", "Hello Amber")
let str = ""
str += unsafe $echo "\$test_shell_constant_set"$
shell_constant_set("test_shell_constant_set", "Hello Amber") failed {
str += " Shell Constant!"
}
echo str
}
1 change: 1 addition & 0 deletions src/tests/stdlib/shell_constant_set.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Amber Shell Constant!
11 changes: 11 additions & 0 deletions src/tests/stdlib/shell_isset.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * from "std"
main {
unsafe $test_var_isset="test"$
if shell_isset("test_var_isset") {
echo "test_var_isset"
}

if shell_isset("test_var_isset2") {
echo "test_var_isset2"
}
}
1 change: 1 addition & 0 deletions src/tests/stdlib/shell_isset.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_var_isset
7 changes: 7 additions & 0 deletions src/tests/stdlib/shell_unset.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * from "std"
main {
unsafe shell_var_set("test_shell_unset", "Hello Amber!")
unsafe $echo \$test_shell_unset$
unsafe shell_unset("test_shell_unset")
unsafe $echo \$test_shell_unset$
}
1 change: 1 addition & 0 deletions src/tests/stdlib/shell_unset.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Amber!
5 changes: 5 additions & 0 deletions src/tests/stdlib/shell_var_get.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * from "std"
main {
unsafe $declare -g test_shell_var_get="Hello Amber!"$
echo unsafe shell_var_get("test_shell_var_get")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/shell_var_get.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Amber!
5 changes: 5 additions & 0 deletions src/tests/stdlib/shell_var_set.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * from "std"
main {
unsafe shell_var_set("test_shell_var_set", "Hello Amber!")
unsafe $echo \$test_shell_var_set$
}
1 change: 1 addition & 0 deletions src/tests/stdlib/shell_var_set.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Amber!

0 comments on commit 2b96f87

Please sign in to comment.