Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Extract compressed files bash function #449

Open
Mte90 opened this issue Jun 19, 2024 · 8 comments
Open

[Feature] Extract compressed files bash function #449

Mte90 opened this issue Jun 19, 2024 · 8 comments
Assignees
Labels
enhancement New feature or request stdlib

Comments

@Mte90
Copy link
Member

Mte90 commented Jun 19, 2024

We can integrate https://github.com/xvoland/Extract/blob/master/extract.sh as extract for tons of formats (or rewrite in amber).

Or also this one https://github.com/Bash-it/bash-it/blob/master/plugins/available/extract.plugin.bash

@Mte90 Mte90 changed the title Extract bash function Extract compressed files bash function Jun 19, 2024
@Ph0enixKM
Copy link
Member

This is a great idea!

@b1ek
Copy link
Member

b1ek commented Jun 20, 2024

it can be added to stdlib (but it will be needed to be rewritten in amber)

@Mte90 Mte90 added the enhancement New feature or request label Jun 24, 2024
@Mte90
Copy link
Member Author

Mte90 commented Jun 27, 2024

@rbtylee shared to me his version of extract written in Amber:

// extract.ab: An amber extraction library function
//
// Copyright 2024 Robert Wiley <ylee@bodhilinux.com>
//
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

// Amber version of Bash-it extract function (simplified somewhat)
// Added mkdir repects verbosity
//       Only process if file exist

// This is a work in progress, not everything may work right
// not well tested and no tests

import * from "std"

pub fun glob(path:Text, pattern:[Text]): Bool
{
    loop p in pattern {
        $[[ {path} =~ {p}\$ ]]$ failed {
            continue}
        return true
        
    }
    return false
}

pub fun extract(path: Text, verbose: Bool): Null{
    if file_exist(path) {
        let filename = unsafe $basename -- {path}$
        let dirname  = unsafe $dirname -- {path}$
        if verbose : let v = "v"
        else: let v = ""

        let target = unsafe $sed 's/\(\.tar\.bz2\$\|\.tbz\$\|\.tbz2\$\|\.tar\.gz\$\|\.tgz\$\|\.tar\$\|\.tar\.xz\$\|\.txz\$\|\.tar\.Z\$\|\.7z\$\|\.nupkg\$\|\.zip\$\|\.war\$\|\.jar\$\)//g' <<< {filename}$
        
        if {
            filename == target: target = ""
            verbose: unsafe  $mkdir -{v} {dirname}/{target}$
            else: unsafe  $mkdir {dirname}/{target}$
        }
        
        if {
            glob(path, [".tar.bz2", ".tbz", ".tbz2"]): unsafe $tar "x{v}jf" "{path}" -C "{dirname}/{target}"$
            glob(path, [".tar.gz", ".tgz"]): unsafe $tar "x{v}zf" {path} -C {dirname}/{target}$
            glob(path, [".tar.xz", ".txz"]): unsafe $tar "x{v}Jf" {path} -C {dirname}/{target}$
            glob(path, [".tar.Z"]): unsafe $tar "x{v}Zf" {path} -C {dirname}/{target}$
            glob(path, [".bz2"]): unsafe $bunzip2 {path}$
            glob(path, [".deb"]): unsafe $dpkg-deb -x{v} {path} "\$\{1:0:-4}"$
            glob(path, [".pax.gz"]): unsafe $gunzip {path}; set -- "\$@" "\$\{1:0:-3}"$
            glob(path, [".gz"]): unsafe $gunzip {path}$
            glob(path, [".pax"]): unsafe $pax -r -f {path}$
            glob(path, [".pkg"]): unsafe $pkgutil --expand {path} "\$\{1:0:-4}"$
            glob(path, [".rar"]): unsafe $unrar x {path}$
            glob(path, [".rpm"]): unsafe $rpm2cpio {path} | cpio -idm{v}$
            glob(path, [".tar"]): unsafe $tar "x{v}f" {path} -C {dirname}/{target}$
            glob(path, [".xz"]): unsafe $xz --decompress {path}$
            glob(path, [".zip", ".war", ".jar", ".nupkg"]): unsafe $unzip {path} -d {dirname}/{target}$
            glob(path, [".Z"]): unsafe $uncompress {path}$
            else: echo "Error: Unsupported file type"
        }    
    }
    else: echo "Error: File not found"
}

I think that is perfect, we need just to remove the .z/pax files as they are very uncommon and add a check with is_command for the various cases.

@b1ek
Copy link
Member

b1ek commented Jun 28, 2024

this would create confusion with the runtime dependency checker feature, since it will check for every function from there

@Mte90
Copy link
Member Author

Mte90 commented Jun 28, 2024

That's true so maybe the stdlib should not this kind of check for command check.

@rbtylee
Copy link

rbtylee commented Jun 28, 2024

Perhaps amber can include more than the std library and if you want extract include it in another library like util for example. Do not run the runtime dependency checker on libs found in util. (I am assuming the runtime dependency checker is something that lives in the rust code).

@Ph0enixKM
Copy link
Member

@b1ek I think that we need to disable RDC for unsafe commands

@Mte90 Mte90 self-assigned this Jul 9, 2024
@Mte90
Copy link
Member Author

Mte90 commented Jul 16, 2024

To proceed with this one I think that we need to define and implement the RDC as it is a very complex case.

@Mte90 Mte90 transferred this issue from amber-lang/amber Sep 3, 2024
@Ph0enixKM Ph0enixKM transferred this issue from another repository Sep 3, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Sep 3, 2024
@Mte90 Mte90 added the stdlib label Sep 5, 2024
@Ph0enixKM Ph0enixKM changed the title Extract compressed files bash function [Feature] Extract compressed files bash function Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stdlib
Projects
None yet
Development

No branches or pull requests

4 participants