From a01e80b6fff30523c1ef6929a1962d43744f260a Mon Sep 17 00:00:00 2001 From: Andrew Voynov <37143421+Andrew15-5@users.noreply.github.com> Date: Sat, 26 Jul 2025 17:00:03 +0500 Subject: [PATCH] Improve gen shell scripts (#2064) * Improve gen shell scripts * Remove unused gen/doc.sh * Fix generated version in docs * Make OCI program usage transparent podman/docker/* as well as root(less) mode is detected automatically. Formatted with 4 space indentation, added Vim modeline for convenience. * Rename gen/doc{-with-docker,}.sh Since the script is no longer docker-specific, it's better to drop the more specific name. * Add Vim modeline to build.sh and xbuild.sh --- gen/build.sh | 6 +++- gen/doc-with-docker.sh | 32 ------------------ gen/doc.sh | 76 +++++++++++++++++++++++++++++++++--------- gen/xbuild.sh | 6 ++-- 4 files changed, 70 insertions(+), 50 deletions(-) delete mode 100755 gen/doc-with-docker.sh diff --git a/gen/build.sh b/gen/build.sh index 26897307..c060b64d 100755 --- a/gen/build.sh +++ b/gen/build.sh @@ -6,6 +6,10 @@ # given to the go compiler to strip the binary. Current git tag is passed to # the compiler by default to be used as the version in the binary. -[ -z $version ] && version=$(git describe --tags) +set -o errexit -o nounset + +[ -z "${version:-}" ] && version=$(git describe --tags --abbrev=0) CGO_ENABLED=0 go build -ldflags="-s -w -X main.gVersion=$version" "$@" + +# vim: tabstop=4 shiftwidth=4 textwidth=80 colorcolumn=80 diff --git a/gen/doc-with-docker.sh b/gen/doc-with-docker.sh deleted file mode 100755 index 8b12218b..00000000 --- a/gen/doc-with-docker.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/sh -# Generates `lf.1` and `doc.txt` from the `doc.md` file. -# -# This script is used to generate a man page and a plain text conversion of the -# markdown documentation using pandoc (https://pandoc.org/). GitHub Flavored -# Markdown (GFM) (https://github.github.com/gfm/) is used for the markdown -# input format. The markdown file is automatically rendered in the GitHub -# repository (https://github.com/gokcehan/lf/blob/master/doc.md). The man page -# file `lf.1` is meant to be used for installations on Unix systems. The plain -# text file `doc.txt` is embedded in the binary to be displayed on request with -# the `-doc` command line flag. Thus the same documentation is used for online -# and terminal display. - -[ -z $version ] && version=$(git describe --tags) -[ -z $date ] && date=$(date +%F) - -PANDOC_IMAGE=pandoc/minimal:3.7 - -docker run --rm -v "$PWD:/data" --user "$(id -u):$(id -g)" $PANDOC_IMAGE \ - --standalone \ - --from gfm --to man \ - --metadata=title:"LF" \ - --metadata=section:"1" \ - --metadata=date:"$date" \ - --metadata=footer:"$version" \ - --metadata=header:"DOCUMENTATION" \ - doc.md -o lf.1 - -docker run --rm -v "$PWD:/data" --user "$(id -u):$(id -g)" $PANDOC_IMAGE \ - --standalone \ - --from gfm --to plain \ - doc.md -o doc.txt diff --git a/gen/doc.sh b/gen/doc.sh index 7f2df743..069535fd 100755 --- a/gen/doc.sh +++ b/gen/doc.sh @@ -11,20 +11,66 @@ # the `-doc` command line flag. Thus the same documentation is used for online # and terminal display. -[ -z $version ] && version=$(git describe --tags) -[ -z $date ] && date=$(date +%F) +set -o errexit -o nounset -pandoc \ - --standalone \ - --from gfm --to man \ - --metadata=title:"LF" \ - --metadata=section:"1" \ - --metadata=date:"$date" \ - --metadata=footer:"$version" \ - --metadata=header:"DOCUMENTATION" \ - doc.md -o lf.1 +[ -z "${version:-}" ] && version=$(git describe --tags --abbrev=0) +[ -z "${date:-}" ] && date=$(date +%F) -pandoc \ - --standalone \ - --from gfm --to plain \ - doc.md -o doc.txt +PANDOC_IMAGE=pandoc/minimal:3.7 + +generate_man_page() { + "${OCI_PROGRAM?}" run \ + --rm \ + --volume "$PWD:/data" \ + "$@" "$PANDOC_IMAGE" \ + --standalone \ + --from gfm --to man \ + --metadata=title:"LF" \ + --metadata=section:"1" \ + --metadata=date:"$date" \ + --metadata=footer:"$version" \ + --metadata=header:"DOCUMENTATION" \ + doc.md -o lf.1 +} + +generate_plain_text() { + "${OCI_PROGRAM?}" run \ + --rm \ + --volume "$PWD:/data" \ + "$@" "$PANDOC_IMAGE" \ + --standalone \ + --from gfm --to plain \ + doc.md -o doc.txt +} + +is_rootless() { + case "$OCI_PROGRAM" in + podman) podman info -f '{{.Host.Security.Rootless}}' | grep -q true ;; + docker) docker info -f '{{.SecurityOptions}}' | grep -q rootless ;; + *) echo >&2 \ + "Unknown OCI program \"$OCI_PROGRAM\", assuming rootless mode" ;; + esac +} + +# You can set your own OCI_PROGRAM, which assumes it runs in rootless mode. +if [ -z "${OCI_PROGRAM:-}" ]; then + if command -v podman > /dev/null; then + OCI_PROGRAM=podman + elif command -v docker > /dev/null; then + OCI_PROGRAM=docker + fi +fi + +if is_rootless; then + generate_man_page +else + generate_man_page --user "$(id -u):$(id -g)" +fi + +if is_rootless; then + generate_plain_text +else + generate_plain_text --user "$(id -u):$(id -g)" +fi + +# vim: tabstop=4 shiftwidth=4 textwidth=80 colorcolumn=80 diff --git a/gen/xbuild.sh b/gen/xbuild.sh index 0dc423f2..4a58386a 100755 --- a/gen/xbuild.sh +++ b/gen/xbuild.sh @@ -8,7 +8,7 @@ # compressed in an archive form (`.zip` for windows and `.tar.gz` for the rest) # within a folder named `dist`. -[ -z "$version" ] && version=$(git describe --tags) +[ -z "${version:-}" ] && version=$(git describe --tags --abbrev=0) test -d dist && { echo "gen/xbuild.sh: WARNING: removing preexisting subdir 'dist'" @@ -31,7 +31,7 @@ build() { } package() ( - cd dist || return 1 + cd dist || return 1 # Since the function is surrounded by (), the cd only affects a subshell OUTFILE= case "$1" in @@ -90,3 +90,5 @@ if test -n "$ERRORS"; then printf "\ngen/xbuild.sh: some targets failed to compile.\n" exit 1 fi + +# vim: tabstop=4 shiftwidth=4 textwidth=80 colorcolumn=80