浏览代码

add config files

Tobias Simetsreiter 4 年之前
父节点
当前提交
8d2fb0c1b1
共有 9 个文件被更改,包括 91 次插入35 次删除
  1. 24 0
      asd.sh
  2. 55 14
      sharxz
  3. 0 1
      test/.sharignore
  4. 4 0
      test/.sharxz/env
  5. 7 0
      test/.sharxz/exclude
  6. 0 0
      test/excluded_directory/excluded
  7. 0 0
      test/excluded_file
  8. 1 1
      test/main.sh
  9. 0 19
      test/unsharxz.sh

+ 24 - 0
asd.sh

@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+set -e
+set +x
+
+FIFO_NAME=$(mktemp -u)
+mkfifo "$FIFO_NAME"
+TMPDIR=$(mktemp -d)
+
+cat $FIFO_NAME|base64 -d |xz -d -T 0 |tar -xC $TMPDIR &
+
+cat <<SAFEPAYLOADEOF > "$FIFO_NAME"
+/Td6WFoAAATm1rRGBMCKA4BQIQEUAAAAAAAAABskFAfgJ/8Bgl0AFwu8HH0BlcAdP4J2VSOao4m0q/0x5cxslicl1WdGudDNGiAUt8zyrZMKWltnzbcleF69AxylHySwAvUZDOOfhLao2v4vpAb0TpzrWegRbq/qVbhUiArQN52s3O69Sp6nHuAjHuntsGuaTzOmMSQj16KFoBGOxioAVRlm20lNRJR/wfjAmDfrfQaUwkX7SIOOjDBVOGes/kcxPWP3fmh8PKj/6hEAu94h2nt3jG6KKj2ew5EQzr4FTPPEcoTcr8xAiLxAsGQKnxe1GeYDdWDOu4CNMiOdvGjrfMP5qk0PtgJtExXPCWR2i08FZeIz0MRR6slzVV0uzh/Ob+moRE5k0xwVslN/CE9VZpSg6sxSp1D12CvlWd4yhvCfdoXXUvVg9JAD0IpsGyxJaE7KHdIjyhaHtXTur//3NyH0xFbr1+JCaSSUmsQN+WVgszIdF0eNp9Z7MhEzhEqiRykmJMDfczwg4LHgT8HWIcHD6ZVnv5R2ynm/+N6jgvfRL9hftvPf3t4AAACj1/AzHBUIBQABpgOAUAAAdcRVyrHEZ/sCAAAAAARZWg==
+SAFEPAYLOADEOF
+
+wait
+rm "$FIFO_NAME"
+cd $TMPDIR
+set +e
+./main.sh $@
+
+EXITCODE=$?
+rm -r $TMPDIR
+exit $EXITCODE

+ 55 - 14
sharxz

@@ -1,14 +1,19 @@
 #!/usr/bin/env bash
 
 sharxz(){
-    TAR_OPTIONS=""
+    TAR_OPTIONS=()
     CHDIR=""
+    CMD=""
     OUTPUT="-"
-    TAR_EXCLUDE=()
-    while getopts ":T:S:C:o:" opt; do
-        case ${opt} in
+    DEBUG=false
+    NO_PARSE=false
+    while getopts ":dT:o:C:x:n" opt; do
+        case $opt in
+            d )
+                DEBUG=true
+              ;;
             T )
-                TAR_OPTIONS="$OPTARG"
+                TAR_OPTIONS+=( $OPTARG )
               ;;
             o )
                 OUTPUT="$OPTARG"
@@ -16,47 +21,83 @@ sharxz(){
             C )
                 CHDIR="$OPTARG"
               ;;
+            x )
+                CMD="$OPTARG"
+              ;;
+            n )
+                NO_PARSE=true
+              ;;
             \? ) echo "Usage: sharxz [-T TAR_OPTIONS] [-C chdir]" 1>&2
               ;;
             : )
             echo "Invalid option: $OPTARG requires an argument" 1>&2
-      ;;
+              ;;
         esac
     done
     shift $((OPTIND -1))
     [ "$OUTPUT" == "-" ] && OUTPUT="/dev/stdout"
     INFILE=$1
 
-    if [ "$CHDIR" == "" ]; then
+    [ "$CHDIR" == "" ] &&
         CHDIR="$(dirname $INFILE)"
-    fi
 
-    [ ! -f "$INFILE" ] && echo "Error: $INFILE is not a file" 1>&2 && exit 1
+    [ ! -f "$INFILE" ] &&
+        [ ! -d "$CHDIR"] && echo "Error: $INFILE is not a file, and $CHDIR is not a directory" 1>&2 && exit 1
+
+    [ -z "$CMD" ] &&
+        CMD="./$(realpath "--relative-to=$CHDIR" "$INFILE")"
 
-    INFILE="$(realpath "--relative-to=$CHDIR" "$INFILE")"
+    $DEBUG && echo "CMD: $CMD" 1>&2
 
     cd "$CHDIR"
 
+    $NO_PARSE || parse_options
+
     print_shar "$INFILE" > "$OUTPUT"
 
 }
+
+parse_options(){
+    [ -f .sharxz/env ] && source .sharxz/env
+    [ -f .sharxz/exclude ] && while read -r line; do
+        local OPTION="$(trim "$line")"
+        [ -z "$OPTION" ] && continue
+        [[ $OPTION == \#* ]] && continue
+        $DEBUG && echo "Excluding: $line" 1>&2
+        TAR_OPTIONS+=( "--exclude=$OPTION" )
+    done < .sharxz/exclude
+}
+
 print_shar(){
+    printf -- "#!/usr/bin/env bash\n\nDEBUG=$DEBUG\n"
     printf -- "$UNSHAR\n"
-    tar -c $TAR_OPTIONS .| xz -c -3 -T 0 | base64 -w 0
+    $DEBUG && pwd 1>&2
+    $DEBUG && ls ** 1>&2
+    $DEBUG && set -x
+    tar -c ${TAR_OPTIONS[@]} ./| xz -c -3 -T 0 | base64 -w 0
+    $DEBUG && set +x
     printf -- "\n$SETUPSHAR\n"
 
-    printf -- "./$INFILE "'$@'"\n"
+    printf -- "$CMD"' $@'"\n"
 
     printf -- "\n$CLEANUPSHAR\n"
 
 }
 
+trim() {
+    local var="$*"
+    # remove leading whitespace characters
+    var="${var#"${var%%[![:space:]]*}"}"
+    # remove trailing whitespace characters
+    var="${var%"${var##*[![:space:]]}"}"   
+    printf '%s' "$var"
+}
+
 
 read -r -d '' UNSHAR <<'UNSHAREOF'
-#!/usr/bin/env bash
 
 set -e
-set +x
+$DEBUG && set -x
 
 FIFO_NAME=$(mktemp -u)
 mkfifo "$FIFO_NAME"

+ 0 - 1
test/.sharignore

@@ -1 +0,0 @@
-unsharxz.sh

+ 4 - 0
test/.sharxz/env

@@ -0,0 +1,4 @@
+
+
+# dereference symlinks
+TAR_OPTIONS+=( "-h" )

+ 7 - 0
test/.sharxz/exclude

@@ -0,0 +1,7 @@
+
+# comment
+excluded_file
+excluded_directory
+
+
+

+ 0 - 0
test/excluded_directory/excluded


+ 0 - 0
test/excluded_file


+ 1 - 1
test/main.sh

@@ -3,4 +3,4 @@
 echo "Hey i ran via ssh in $(realpath $0) on host $(hostname)"
 echo "I was executed like so: $0 $*"
 echo "These Files are included:"
-ls -alsh **
+find .

+ 0 - 19
test/unsharxz.sh

@@ -1,19 +0,0 @@
-#!/usr/bin/env bash
-
-set -e
-set +x
-
-TMPDIR=$(mktemp -d)
-
-dd if=$0 bs=1024 skip=1 2>/dev/null | base64 -d |xz -d -T 0 |tar -xC $TMPDIR
-
-export UNSHARXZ_INSTALLER="$(realpath $0)"
-export UNSHARXZ_ORIG_DIR="$(pwd)"
-cd $TMPDIR
-set +e
-./main.py $@
-EXITCODE=$?
-rm -r $TMPDIR
-exit $EXITCODE
-
-cat <<SAFEPAYLOADEOF > /dev/null