Ph0enixKM / Amber

💎 Amber the programming language compiled to bash
https://amber-lang.com
GNU General Public License v3.0
3.51k stars 67 forks source link

feat(std): more commands #185

Open Mte90 opened 2 weeks ago

Mte90 commented 2 weeks ago

So I added more commands:

In many of those cases there is a check if the file/folder exist, in case return a different boolean value and print the error. In 2 cases we need the OR condition so we can check if it is file and a folder.

b1ek commented 2 weeks ago

cd could (and probably should) be added as a bulit in command to amber, like echo

Ph0enixKM commented 2 weeks ago

@b1ek all the non-failable GNU commands that are parto of POSIX should be built-in.

b1ek commented 2 weeks ago

@b1ek all the non-failable GNU commands that are parto of POSIX should be built-in.

and most of these

Ph0enixKM commented 2 weeks ago

Actually... perhaps built-ins could be failable as well:

mv "path/to/file.txt" "other/path/to/file.txt" failed {
    echo "Couldn't move the file"
}
Mte90 commented 2 weeks ago

Let's do a list of commands that need a native support and what needs a function. Maybe we can support them natively and create functions like I did that improve the usage like a normal scripting language.

s3than commented 1 week ago

This function should have the following changes

pub fun make_executable(path: Text): Null {
    if file_exist(origin) {
        unsafe $chmod +x "{path}"$
        return true
    }

    echo "The file {$path} doesn't exist!"
    return false
}

To

pub fun make_executable(path: Text): Bool {
    if file_exist(path) {
        unsafe $chmod +x "{path}"$
        return true
    }

    echo "The file {path} doesn't exist!"
    return false
}
Mte90 commented 1 week ago

So I have to removed the non-failable commands from this PR.

About make_symbolic_link I think that is better to rename it as create_symbolic_link to keep the consistency.

Mte90 commented 1 week ago

Probably is missing a function to extract compressed files, download files and to search inside file content. I will work on those the next days.

Mte90 commented 1 week ago

I did some changes and added the download command but I left a note for that.

Mte90 commented 1 week ago

every stdlib command should be covered by a test. the tests are in src/tests/stdlib.rs

I never developed with Rust but I guess that is enough to generate the bash and check that is always the same right? Maybe this can be simplified instead of editing a rust file maybe a pure text file is more simplier?


I added also is_root and is_command and improved the download function.

Ph0enixKM commented 1 week ago

@Mte90 if you want to write a standard function to download file, then maybe write one that detects available solution to download it like curl, wget, python and then use the strategy to do it:

if {
    not has_failed("curl") {
        // ... run curl command
    }
    not has_failed("wget") {
        // ... run wget command
    }
    not has_failed("python") {
        // ... run python command (and detect python version)
    }
Mte90 commented 1 week ago
not has_failed("wget") {

I think that is better a command that check if exist instead to run it to see what is the error. In this way we can avoid issues more easily, that is why I added the is_command function.

Ph0enixKM commented 1 week ago

I think that is better a command that check if exist instead to run it to see what is the error. In this way we can avoid issues more easily, that is way I added the is_command function.

Sure! I just wanted to share an example of potential implementation

Mte90 commented 1 week ago

So I think that another round of review for the function so I can try to do the unit tests :-)

Mte90 commented 1 week ago

I did the various tests, I have to fix the download one that returns:

thread 'tests::stdlib::download' panicked at src/tests/stdlib.rs:510:5:
assertion `left == right` failed
  left: ""
 right: "yes"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

If you have suggestions they are accepted :-)

Mte90 commented 1 week ago

Fixed also the last test :-)

Mte90 commented 1 week ago

There are some conflicts with cargo.lock/toml

b1ek commented 1 week ago

There are some conflicts with cargo.lock/toml

ftfy <3

Mte90 commented 3 days ago

So I added 3 new commands:

I am fighting with the compilation as we need $(echo "${!var}") to read the variable exported otherwise doesn't work and I am trying to understand how to generate that line in this way. So right now 2 tests fails.