Skip to content

Commit

Permalink
add tar.xz & handle multi program
Browse files Browse the repository at this point in the history
  • Loading branch information
cxjava committed Oct 25, 2024
1 parent 318ab29 commit 612d3a8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 101 deletions.
4 changes: 4 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// multi program, such: mp=ss_server,ss_client
if len(r.URL.Query().Get("mp")) != 0 {
result.Program = r.URL.Query().Get("mp")
}
switch qtype {
case "json":
w.Header().Set("Content-Type", "application/json")
Expand Down
2 changes: 1 addition & 1 deletion handler/handler_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (h *Handler) getAssetsNoCache(q Query) (string, Assets, error) {
fext = ".bin" // +1MB binary
}
switch fext {
case ".bin", ".zip", ".tar.bz", ".tar.bz2", ".bz2", ".gz", ".tar.gz", ".tgz":
case ".bin", ".zip", ".tar.bz", ".tar.bz2", ".tar.xz", ".bz2", ".gz", ".tar.gz", ".tgz":
// valid
default:
log.Printf("fetched asset has unsupported file type: %s (ext '%s')", ga.Name, fext)
Expand Down
63 changes: 28 additions & 35 deletions handler/install.sh.qtpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function fail {
function install {
#settings
USER="{%s r.User %}"
PROG="{%s r.Program %}"
IFS=',' read -r -a PROG_LIST <<< "{%s r.Program %}"
ASPROG="{% if len(r.AsProgram)>0 %} {%s r.AsProgram %} {% endif %}"
MOVE="{%v r.MoveToPath %}"
RELEASE="{%s r.Release %}" # {%s r.ResolvedRelease %}
Expand Down Expand Up @@ -93,7 +93,7 @@ function install {
esac
#got URL! download it...
echo -n "{% if r.MoveToPath %}Installing{% else %}Downloading{% endif %}"
echo -n " $USER/$PROG"
echo -n " $USER/${PROG_LIST[*]}"
if [ ! -z "$RELEASE" ]; then
echo -n " $RELEASE"
fi
Expand All @@ -116,10 +116,10 @@ function install {
cd $TMP_DIR
if [[ $FTYPE = ".gz" ]]; then
which gzip > /dev/null || fail "gzip is not installed"
bash -c "$GET $URL" | gzip -d - > $PROG || fail "download failed"
bash -c "$GET $URL" | gzip -d - > "${PROG_LIST[0]}" || fail "download failed"
elif [[ $FTYPE = ".bz2" ]]; then
which bzip2 > /dev/null || fail "bzip2 is not installed"
bash -c "$GET $URL" | bzip2 -d - > $PROG || fail "download failed"
bash -c "$GET $URL" | bzip2 -d - > "${PROG_LIST[0]}" || fail "download failed"
elif [[ $FTYPE = ".tar.bz" ]] || [[ $FTYPE = ".tar.bz2" ]]; then
which tar > /dev/null || fail "tar is not installed"
which bzip2 > /dev/null || fail "bzip2 is not installed"
Expand All @@ -128,46 +128,39 @@ function install {
which tar > /dev/null || fail "tar is not installed"
which gzip > /dev/null || fail "gzip is not installed"
bash -c "$GET $URL" | tar zxf - || fail "download failed"
elif [[ $FTYPE = ".tar.xz" ]] || [[ $FTYPE = ".tgz" ]]; then
which tar > /dev/null || fail "tar is not installed"
which xz > /dev/null || fail "xz is not installed"
bash -c "$GET $URL" | tar Jxf - || fail "download failed"
elif [[ $FTYPE = ".zip" ]]; then
which unzip > /dev/null || fail "unzip is not installed"
bash -c "$GET $URL" > tmp.zip || fail "download failed"
unzip -o -qq tmp.zip || fail "unzip failed"
rm tmp.zip || fail "cleanup failed"
elif [[ $FTYPE = ".bin" ]]; then
bash -c "$GET $URL" > "{%s r.Program %}_${OS}_${ARCH}" || fail "download failed"
bash -c "$GET $URL" > "${PROG_LIST[0]}_${OS}_${ARCH}" || fail "download failed"
else
fail "unknown file type: $FTYPE"
fi
#search subtree largest file (bin)
TMP_BIN=$(find . -type f | xargs du | sort -n | tail -n 1 | cut -f 2)
if [ ! -f "$TMP_BIN" ]; then
fail "could not find find binary (largest file)"
fi
#ensure its larger than 1MB
#TODO linux=elf/darwin=macho file detection?
if [[ $(du -m $TMP_BIN | cut -f1) -lt 1 ]]; then
fail "no binary found ($TMP_BIN is not larger than 1MB)"
fi
#move into PATH or cwd
chmod +x $TMP_BIN || fail "chmod +x failed"
DEST="$OUT_DIR/$PROG"
if [ ! -z "$ASPROG" ]; then
DEST="$OUT_DIR/$ASPROG"
fi
#move without sudo
OUT=$(mv $TMP_BIN $DEST 2>&1)
STATUS=$?
# failed and string contains "Permission denied"
if [ $STATUS -ne 0 ]; then
if [[ $OUT =~ "Permission denied" ]]; then
echo "mv with sudo..."
sudo mv $TMP_BIN $DEST || fail "sudo mv failed"
else
fail "mv failed ($OUT)"
fi
fi
echo "{% if r.MoveToPath %}Installed at{% else %}Downloaded to{% endif %} $DEST"
#done
for PROG in "${PROG_LIST[@]}"; do
BIN_PATH=$(find . -type f | grep -i "$PROG" | head -n 1)
[[ -z "$BIN_PATH" ]] && fail "Binary $PROG not found"

chmod +x "$BIN_PATH" || fail "chmod +x failed on $BIN_PATH"
DEST="$OUT_DIR/$PROG"

OUT=$(mv "$BIN_PATH" "$DEST" 2>&1)
STATUS=$?
if [ $STATUS -ne 0 ]; then
if [[ $OUT =~ "Permission denied" ]]; then
echo "mv with sudo..."
sudo mv "$BIN_PATH" "$DEST" || fail "sudo mv failed for $BIN_PATH"
else
fail "mv failed for $BIN_PATH ($OUT)"
fi
fi
echo "Moved $PROG to $DEST"
done
cleanup
}
install
Expand Down
107 changes: 42 additions & 65 deletions handler/install.sh.qtpl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 612d3a8

Please sign in to comment.