Radish alpha
r
rad:z39mP9rQAaGmERfUMPULfPUi473tY
Radicle terminal user interface
Radicle
Git
simplify proxy-script
Archived did:key:z6MkppTx...sU13 opened 1 year ago

The proxy script assumes that passed arguments are safe from triggering carious forms of shell expansion, and uses needless bash constructs.

This patch simplifies the shell code to always quote variables, and to use only plain shell constructs - and the result is arguably easier to read as an added bonus.

1 file changed +36 -39 7303bd6c 437599f4
modified rad.sh
@@ -1,45 +1,42 @@
-
#!/bin/bash
+
#!/bin/sh
extract_operation() {
-
    local op=$(echo $1 | jq '.operation')
-
    op=${op//\"/""}
-
    
-
    echo "$op"
+
    echo "$1" | jq --raw-output '.operation'
}

extract_id() {
-
    local ids=$(echo $1 | jq '.ids')
-
    local id=$(echo $ids | jq '.[0]')
-
    id=${id//\"/""}
-
    
-
    echo "$id"
+
    echo "$1" | jq --raw-output '.ids[0]'
}

-
if [[ "$1" == "patch" ]] || [[ "$1" == "issue" ]] || [[ "$1" == "inbox" ]]; then
-
    if [[ -n "$2" ]]; then
-
        if [[ "$2" == "--tui" ]]; then
-
            # Run TUI
-
            { out=$(rad-tui $1 select 2>&1 >&3 3>&-); } 3>&1
-
            if [[ "$out" == "" ]]; then
-
                exit 1
-
            fi
-
            
-
            op=$(extract_operation $out)
-
            id=$(extract_id $out)
-
            
-
            rad $1 $op $id
-
        else
-
            # Run TUI
-
            args="--mode id"
-
            { out=$(rad-tui $1 select $args 2>&1 >&3 3>&-); } 3>&1
-
            id=$(extract_id $out)
-
            
-
            args=("$@")
-
 
-
            rad $1 $2 $id ${args[@]:2}
-
        fi
-
    else
-
        rad $@
-
    fi
-
else
-
    rad $@
-
fi

\ No newline at end of file
+
case "$1" in
+
    patch|issue|inbox)
+
        case "$2" in
+
            '')
+
                rad "$@"
+
                ;;
+
            --tui)
+
                # Run TUI
+
                { out=$(rad-tui "$1" select 2>&1 >&3 3>&-); } 3>&1
+
                if [ -z "$out" ]; then
+
                    exit 1
+
                fi
+

+
                op=$(extract_operation "$out")
+
                id=$(extract_id "$out")
+

+
                rad "$1" "$op" "$id"
+
                ;;
+
            *)
+
                # Run TUI
+
                { out=$(rad-tui "$1" select --mode id 2>&1 >&3 3>&-); } 3>&1
+
                id=$(extract_id "$out")
+

+
                shift 2
+

+
                rad "$1" "$2" "$id" "$@"
+
                ;;
+
        esac
+
        ;;
+
    *)
+
        rad "$@"
+
        ;;
+
esac