changed project name to manweb made tldr pages work a bit better
@@ -4,7 +4,7 @@ import (
"context" "errors" "git.sophuwu.com/gophuwu/flags" - "git.sophuwu.com/manhttpd/logs" + "git.sophuwu.com/manweb/logs" "golang.org/x/sys/unix" "net/http" "os"@@ -21,12 +21,12 @@ Hostname string = ""
Port string = "8082" Addr string = "0.0.0.0" RequireAuth bool = false - PasswdFile string = "/var/lib/manhttpd/authuwu" + PasswdFile string = "/var/lib/manweb/authuwu.db" TldrPages bool = false - TldrDir string = "/var/lib/manhttpd/tldr" + TldrDir string = "/var/lib/manweb/tldr" TldrGitSrc string = "https://github.com/tldr-pages/tldr.git" EnableStats bool = false - StatisticDB string = "/var/lib/manhttpd/manhttpd.db" + StatisticDB string = "/var/lib/manweb/manweb.db" UseTLS bool = false TLSCertFile string = "" TLSKeyFile string = ""
@@ -3,12 +3,12 @@
import ( "errors" "fmt" - "git.sophuwu.com/manhttpd/logs" + "git.sophuwu.com/manweb/logs" "os" "strings" ) -var ConfFile = "/etc/manhttpd/manhttpd.conf" +var ConfFile = "/etc/manweb/manweb.conf" var NoConfError = errors.New("no configuration file found")
@@ -1,19 +1,23 @@
default: build + build: - go build -ldflags="-w -s" -trimpath -o build/manhttpd + go build -ldflags="-w -s" -trimpath -o build/manweb -build_deb: ./build/manhttpd - cd extra && nfpm pkg --packager deb --config nfpm.yaml --target ../build/manhttpd.deb +clean: + rm -rf build -install_deb: ./build/manhttpd.deb - sudo apt-get install -y ./build/manhttpd.deb +build_deb: + cd extra && nfpm pkg --packager deb --config nfpm.yaml --target ../build/manweb.deb -install_bin: ./build/manhttpd +install_deb: ./build/manweb.deb + sudo apt-get install -y ./build/manweb.deb + +install_bin: ./build/manweb sudo apt-get install -y mandoc sudo sh ./extras/preinst.sh - sudo install ./build/manhttpd /usr/bin/manhttpd - sudo install ./extras/manhttpd-passwd /usr/bin/manhttpd-passwd - sudo install ./extras/manhttpd.service /etc/manhttpd/manhttpd.service - sudo install ./extras/manhttpd.conf /etc/manhttpd/manhttpd.conf + sudo install ./build/manweb /usr/bin/manweb + sudo install ./extras/manweb-passwd /usr/bin/manweb-passwd + sudo install ./extras/manweb.service /etc/manweb/manweb.service + sudo install ./extras/manweb.conf /etc/manweb/manweb.conf sudo sh ./extras/postinstall.sh
@@ -1,6 +1,11 @@
-# Go Man Page Web Server +# Man Web +A Service to show man pages on the web. +It allows for easy viewing and advanced searching of man pages in a web browser. +It uses the current system's manpages, so you will always have the correct documentation +to all the commands and libraries installed on your system. -This Go application serves man pages over HTTP. It allows users to view, search, and browse man pages directly from a web browser. The server dynamically integrates the hostname into the pages and provides static file support (CSS and favicon). +Written in GoLang, so the only dependencies are `man` and `mandoc` packages. +To use tldr, `git` will be required to download the tldr pages. ## Features@@ -28,7 +33,7 @@ This isn't common on most systems, so the default configuration should work out of the box in most cases.
```bash curl https://cdn.sophuwu.com/deb/addrepo.sh | sudo sh -sudo apt install manhttpd +sudo apt install manweb ``` # Compiling From Source@@ -44,8 +49,8 @@
## Compiling The Binary ```bash -git clone "https://git.sophuwu.com/manhttpd" -cd manhttpd +git clone "https://git.sophuwu.com/manweb" +cd manweb # build binary make build@@ -73,33 +78,33 @@
### System Variables: `User`: Reccomended to use your login user so the service can access your ~/.local man pages. But not required.\ -`ExecStart`: The path to the manhttpd binary. If you installed it to /usr/local/bin, you can leave it as is.\ +`ExecStart`: The path to the manweb binary. If you installed it to /usr/local/bin, you can leave it as is.\ `WorkingDirectory`: This should be /tmp since the server doesn't need to write to disk.\ ```sh # to edit paths, users, and environment variables if needed -nano manhttpd.service +nano manweb.service # install the service file to systemd and load it -sudo install manhttpd.service /etc/systemd/system/manhttpd.service +sudo install manweb.service /etc/systemd/system/manweb.service sudo systemctl daemon-reload # start the service and check its status -sudo systemctl start manhttpd.service -sudo systemctl status manhttpd.service +sudo systemctl start manweb.service +sudo systemctl status manweb.service # to keep the service running after a reboot -sudo systemctl enable manhttpd.service +sudo systemctl enable manweb.service # to stop the service and disable it from restarting -sudo systemctl stop manhttpd.service -sudo systemctl disable manhttpd.service +sudo systemctl stop manweb.service +sudo systemctl disable manweb.service # to edit the server configuration after installation -sudo systemctl edit manhttpd.service +sudo systemctl edit manweb.service sudo systemctl daemon-reload -sudo systemctl reload-or-restart manhttpd.service -sudo systemctl status manhttpd.service +sudo systemctl reload-or-restart manweb.service +sudo systemctl status manweb.service ``` # Accessing the Web Interface
@@ -4,9 +4,9 @@ import (
"embed" _ "embed" "fmt" - "git.sophuwu.com/manhttpd/CFG" - "git.sophuwu.com/manhttpd/logs" - "git.sophuwu.com/manhttpd/neterr" + "git.sophuwu.com/manweb/CFG" + "git.sophuwu.com/manweb/logs" + "git.sophuwu.com/manweb/neterr" "html/template" "io/fs" "net/http"
@@ -1,63 +0,0 @@
-#!/bin/bash - -# This script is used change the users and passwords for manhttpd -# This script must be run as root or with sudo -if [[ $EUID -ne 0 ]]; then - echo "This script must be run as root or with sudo" 1>&2 - exit 1 -fi - -helpMenu() { - echo 'usage: manhttpd-passwd [-c /path/to/manhttpd.conf] <username>' - echo ' -c: specify the path to the manhttpd.conf file, otherwise the default' - echo ' will be used (/etc/manhttpd/manhttpd.conf)' - echo - echo ' if the username already exists, the password for that user will be changed' - echo ' if the username does not exist, a new user will be created' - echo ' if a username exists and no password is given, the user will be deleted' -} - -if [[ "$1" == "" || "$#" == "0" ]] ]]; then - helpMenu - exit 1 -fi -if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "-?" || "$1" == "help" ]]; then - helpMenu - exit 0 -fi - -manhttpd_running() { - echo "manhttpd is running" - echo "Please stop all instances of manhttpd before running this script." -} - -echo "Checking if manhttpd is running..." -systemctl is-active manhttpd >/dev/null 2>&1 -if [[ "$?" == "0" ]]; then - manhttpd_running - exit 1 -fi -ps x | grep -E '/.*bin.*/manhttpd$' > /dev/null 2>&1 -if [[ "$?" == "0" ]]; then - manhttpd_running - exit 1 -fi - -confFile="/etc/manhttpd/manhttpd.conf" -if [[ "$#" == "3" && "$1" == "-c" ]]; then - if [[ ! "$2" == *".conf" || ! -f "$2" ]]; then - echo "File $2 does not exist or is not a conf file." - exit 1 - fi - confFile="$2" - username="$3" -elif [[ "$#" == "1" ]]; then - username="$1" -else - helpMenu - exit 1 -fi - -sudo -u manhttpd /usr/bin/manhttpd -c "$confFile" -pu "$username" - -
@@ -1,9 +1,9 @@
-## manhttpd.conf - Configuration file for the manhttpd server +## manweb.conf - Configuration file for the manweb server -## manhttpd will read this file to configure its behavior -## The default location for this file is /etc/manhttpd/manhttpd.conf +## manweb will read this file to configure its behavior +## The default location for this file is /etc/manweb/manweb.conf ## You can also specify a custom configuration file by using the -c option -## Example: manhttpd -c /path/to/custom/manhttpd.conf +## Example: manweb -c /path/to/custom/manweb.conf ## By default, the hostname in set from /etc/hostname@@ -20,28 +20,28 @@ #addr = 0.0.0.0
## By default, tldr_pages is set to no -## and tldr_dir is set to /var/lib/manhttpd/tldr +## and tldr_dir is set to /var/lib/manweb/tldr ## Uncomment the line below to enable tldr_pages #tldr_pages = yes -#tldr_dir = /var/lib/manhttpd/tldr +#tldr_dir = /var/lib/manweb/tldr #tldr_git_src = https://github.com/tldr-pages/tldr.git -## manhttpd supports basic authentication. to edit auth credentials: -## sudo manhttpd-passwd <username> +## manweb supports basic authentication. to edit auth credentials: +## sudo manweb-passwd <username> ## this will prompt you to enter a password for the user ## By default, require_auth is set to no, this disables authentication -## the default passwd_file is set to /var/lib/manhttpd/authuwu.db +## the default passwd_file is set to /var/lib/manweb/authuwu.db ## Uncomment the line below to enable tldr_pages #require_auth = yes -#passwd_file = /var/lib/manhttpd/authuwu.db +#passwd_file = /var/lib/manweb/authuwu.db ## By default, enable_stats is set to no -## and statistic_db is set to /var/lib/manhttpd/stats.db +## and statistic_db is set to /var/lib/manweb/stats.db ## Uncomment the line below to enable statistics collection #enable_stats = yes -#statistic_db = /var/lib/manhttpd/stats.db +#statistic_db = /var/lib/manweb/stats.db ## By default, use_tls is set to no
@@ -1,15 +0,0 @@
-[Unit] -Description=manhttpd - HTTP server for serving manual pages -After=network.target - -[Service] -ExecStart=/usr/bin/manhttpd - -WorkingDirectory=/var/lib/manhttpd -Type=simple - -User=manhttpd -Group=manhttpd - -[Install] -WantedBy=multi-user.target
@@ -0,0 +1,63 @@
+#!/bin/bash + +# This script is used change the users and passwords for manweb +# This script must be run as root or with sudo +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root or with sudo" 1>&2 + exit 1 +fi + +helpMenu() { + echo 'usage: manweb-passwd [-c /path/to/manweb.conf] <username>' + echo ' -c: specify the path to the manweb.conf file, otherwise the default' + echo ' will be used (/etc/manweb/manweb.conf)' + echo + echo ' if the username already exists, the password for that user will be changed' + echo ' if the username does not exist, a new user will be created' + echo ' if a username exists and no password is given, the user will be deleted' +} + +if [[ "$1" == "" || "$#" == "0" ]]; then + helpMenu + exit 1 +fi +if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "-?" || "$1" == "help" ]]; then + helpMenu + exit 0 +fi + +manweb_running() { + echo "manweb is running" + echo "Please stop all instances of manweb before running this script." +} + +echo "Checking if manweb is running..." +systemctl is-active manweb >/dev/null 2>&1 +if [[ "$?" == "0" ]]; then + manweb_running + exit 1 +fi +ps x | grep -E '/.*bin.*/manweb$' > /dev/null 2>&1 +if [[ "$?" == "0" ]]; then + manweb_running + exit 1 +fi + +confFile="/etc/manweb/manweb.conf" +if [[ "$#" == "3" && "$1" == "-c" ]]; then + if [[ ! "$2" == *".conf" || ! -f "$2" ]]; then + echo "File $2 does not exist or is not a conf file." + exit 1 + fi + confFile="$2" + username="$3" +elif [[ "$#" == "1" ]]; then + username="$1" +else + helpMenu + exit 1 +fi + +sudo -u manweb /usr/bin/manweb -c "$confFile" -pu "$username" + +
@@ -0,0 +1,15 @@
+[Unit] +Description=manweb - HTTP server for serving manual pages +After=network.target + +[Service] +ExecStart=/usr/bin/manweb + +WorkingDirectory=/var/lib/manweb +Type=simple + +User=manweb +Group=manweb + +[Install] +WantedBy=multi-user.target
@@ -2,7 +2,7 @@ # nfpm example configuration file
# # check https://nfpm.goreleaser.com/configuration for detailed usage # -name: "manhttpd" +name: "manweb" arch: "amd64" platform: "linux" version: "1.4.0"@@ -12,12 +12,13 @@ replaces:
provides: depends: - mandoc +- git recommends: suggests: conflicts: maintainer: "sophuwu <sophie@sophuwu.com>" description: | - manhttpd is a frontend for the linux man pages. + manweb is a frontend for the linux man pages. Offering a functional, minimalistic interface for viewing and finding pages. - Search directly by page name - Search by keyword/wildcard@@ -26,19 +27,19 @@ - Full regex name and description search
It is useful for serving manpages over a network, or for browsing them in a web browser. vendor: "sophuwu.com" -homepage: "https://git.sophuwu.com/manhttpd" +homepage: "https://git.sophuwu.com/manweb" license: "MIT" changelog: "" contents: -- src: ../build/manhttpd - dst: /usr/bin/manhttpd -- src: ./manhttpd-passwd - dst: /usr/bin/manhttpd-passwd -- src: ./manhttpd.service - dst: /etc/manhttpd/manhttpd.service +- src: ../build/manweb + dst: /usr/bin/manweb +- src: ./manweb-passwd + dst: /usr/bin/manweb-passwd +- src: ./manweb.service + dst: /etc/manweb/manweb.service type: config -- src: ./manhttpd.conf - dst: /etc/manhttpd/manhttpd.conf +- src: ./manweb.conf + dst: /etc/manweb/manweb.conf type: config overrides: deb:
@@ -1,18 +1,18 @@
#!/bin/bash -if [ -f /etc/manhttpd/manhttpd.service ]; then - ln -sf /etc/manhttpd/manhttpd.service /etc/systemd/system/manhttpd.service +if [ -f /etc/manweb/manweb.service ]; then + ln -sf /etc/manweb/manweb.service /etc/systemd/system/manweb.service fi -if [ ! -d /var/lib/manhttpd ]; then - mkdir -p /var/lib/manhttpd +if [ ! -d /var/lib/manweb ]; then + mkdir -p /var/lib/manweb fi -if [ -d /var/lib/manhttpd ]; then - chown manhttpd:manhttpd /var/lib/manhttpd - chmod 755 /var/lib/manhttpd +if [ -d /var/lib/manweb ]; then + chown manweb:manweb /var/lib/manweb + chmod 755 /var/lib/manweb fi systemctl daemon-reload -systemctl enable manhttpd -systemctl start manhttpd +systemctl enable manweb +systemctl start manweb
@@ -1,27 +1,32 @@
#!/bin/bash -getent group manhttpd > /dev/null +getent group manweb > /dev/null if [ $? -ne 0 ]; then - addgroup --system manhttpd + addgroup --system manweb fi -getent passwd manhttpd > /dev/null +getent passwd manweb > /dev/null if [ $? -ne 0 ]; then - adduser --system manhttpd - usermod -aG manhttpd manhttpd - usermod --shell /bin/false manhttpd - usermod --home /var/lib/manhttpd manhttpd + adduser --system manweb + usermod -aG manweb manweb + usermod --shell /bin/false manweb + usermod --home /var/lib/manweb manweb +fi + +if [ ! -d /var/lib/manweb ]; then + mkdir -p /var/lib/manweb fi -if [ ! -d /var/lib/manhttpd ]; then - mkdir -p /var/lib/manhttpd +if [ -d /var/lib/manweb ]; then + chown manweb:manweb /var/lib/manweb + chmod 0775 /var/lib/manweb fi -if [ -d /var/lib/manhttpd ]; then - chown manhttpd:manhttpd /var/lib/manhttpd - chmod 0775 /var/lib/manhttpd +if [ ! -d /etc/manweb ]; then + mkdir -p /etc/manweb fi -if [ ! -d /etc/manhttpd ]; then - mkdir -p /etc/manhttpd +if [ -d /etc/systemd/system/manhttpd.service ]; then + systemctl disable manhttpd fi +
@@ -1,9 +1,9 @@
-module git.sophuwu.com/manhttpd +module git.sophuwu.com/manweb go 1.24.4 require ( - git.sophuwu.com/authuwu v0.0.0-20250716222415-8755549e8fed + git.sophuwu.com/authuwu v0.0.0-20250727204702-f3a53ad3d5bf git.sophuwu.com/gophuwu v0.0.0-20250716231858-26e29b0c923a golang.org/x/sys v0.34.0 golang.org/x/term v0.33.0
@@ -1,5 +1,5 @@
-git.sophuwu.com/authuwu v0.0.0-20250716222415-8755549e8fed h1:ixaJKErKg3l5+1i5ArQtzEPemqMLPRybwpgOQ8ZWu6A= -git.sophuwu.com/authuwu v0.0.0-20250716222415-8755549e8fed/go.mod h1:8Iwgnydo+Zud59xPW1HZKflqjVfXslLr0isASGRhj+M= +git.sophuwu.com/authuwu v0.0.0-20250727204702-f3a53ad3d5bf h1:dynoakGl6r7KDIimBIrQBFpn1Wj/oxylZ1qAqgRyuWE= +git.sophuwu.com/authuwu v0.0.0-20250727204702-f3a53ad3d5bf/go.mod h1:8Iwgnydo+Zud59xPW1HZKflqjVfXslLr0isASGRhj+M= git.sophuwu.com/gophuwu v0.0.0-20250716231858-26e29b0c923a h1:eh4yOMWfnwuv7sJ35tZD/fNzeSfs0v2gHnYqiHtpm2M= git.sophuwu.com/gophuwu v0.0.0-20250716231858-26e29b0c923a/go.mod h1:2j1SAWD5STcFV5oKUm4vChACQ1peXCKpfJLbgE/sD00= github.com/DataDog/zstd v1.4.1 h1:3oxKN3wbHibqx897utPC2LTQU4J+IHWWJO+glkAkpFM=
@@ -5,12 +5,12 @@ "fmt"
"git.sophuwu.com/authuwu" "git.sophuwu.com/authuwu/userpass" "git.sophuwu.com/gophuwu/flags" - "git.sophuwu.com/manhttpd/CFG" - "git.sophuwu.com/manhttpd/embeds" - "git.sophuwu.com/manhttpd/logs" - "git.sophuwu.com/manhttpd/manpage" - "git.sophuwu.com/manhttpd/neterr" - "git.sophuwu.com/manhttpd/tldr" + "git.sophuwu.com/manweb/CFG" + "git.sophuwu.com/manweb/embeds" + "git.sophuwu.com/manweb/logs" + "git.sophuwu.com/manweb/manpage" + "git.sophuwu.com/manweb/neterr" + "git.sophuwu.com/manweb/tldr" "golang.org/x/term" "net/http" "os"@@ -21,7 +21,7 @@ "time"
) func init() { - err := flags.NewFlag("conf", "c", "configuration file to use", "/etc/manhttpd/manhttpd.conf") + err := flags.NewFlag("conf", "c", "configuration file to use", "/etc/manweb/manweb.conf") logs.CheckFatal("creating conf flag", err) err = flags.NewFlag("passwd", "p", "open the program in password edit mode", false) logs.CheckFatal("creating passwd flag", err)
@@ -2,9 +2,9 @@ package manpage
import ( "fmt" - "git.sophuwu.com/manhttpd/CFG" - "git.sophuwu.com/manhttpd/embeds" - "git.sophuwu.com/manhttpd/neterr" + "git.sophuwu.com/manweb/CFG" + "git.sophuwu.com/manweb/embeds" + "git.sophuwu.com/manweb/neterr" "net/http" "os/exec" "path/filepath"
@@ -7,21 +7,20 @@
var ( Err400 = HTCode(400, "Bad Request", "Your request cannot be understood by the server.", - "Check that you are using a release version of manhttpd.", + "Check that you are using a release version of manweb.", "Please check spelling and try again.", - "Otherwise browser extensions or proxies may be hijacking your requests.", ) Err404 = HTCode(404, "Not Found", "The requested does match any known page names. Please check your spelling and try again.", `If you cannot find the page using your system's man command, then you may need to update your manDB or apt-get <b><package>-doc</b>.`, - "If you can open a page using the cli but not in manhttpd, your service is misconfigured. For best results set user and group to your login user:", - `You can edit <<b>/etc/systemd/system/manhttpd.service</b>> and set "<b>User</b>=<<b>your-user</b>>" and "<b>Group</b>=<<b>your-group</b>>".`, + "If you can open a page using the cli but not in manweb, your service is misconfigured. For best results set user and group to your login user:", + `You can edit <<b>/etc/systemd/system/manweb.service</b>> and set "<b>User</b>=<<b>your-user</b>>" and "<b>Group</b>=<<b>your-group</b>>".`, `Usually root user will work just fine, however root does not index user pages. If manuals are installed without superuser, they are saved to <<b>$HOME/.local/share/man/</b>>.`, - `If you want user pages you have to run manhttpd as your login user. If you really want to run the service as root with user directories, at your own risk: adding users' homes into the global path <<b>/etc/manpath.config</b>> is usually safe but may cause catastrophic failure on some systems.`, + `If you want user pages you have to run manweb as your login user. If you really want to run the service as root with user directories, at your own risk: adding users' homes into the global path <<b>/etc/manpath.config</b>> is usually safe but may cause catastrophic failure on some systems.`, ) Err500 = HTCode(500, "Internal Server Error", "The server encountered an error and could not complete your request.", - "Make sure you are using a release version of manhttpd.", + "Make sure you are using a release version of manweb.", ) )
@@ -2,11 +2,10 @@ package tldr
import ( "errors" - "fmt" - "git.sophuwu.com/manhttpd/CFG" - "git.sophuwu.com/manhttpd/embeds" - "git.sophuwu.com/manhttpd/logs" - "git.sophuwu.com/manhttpd/neterr" + "git.sophuwu.com/manweb/CFG" + "git.sophuwu.com/manweb/embeds" + "git.sophuwu.com/manweb/logs" + "git.sophuwu.com/manweb/neterr" "net/http" "os" "strings"@@ -18,100 +17,49 @@ )
func GitDir() string { return filepath.Join(CFG.TldrDir, "tldr.git") } -func getGitList(path string, mp *map[string]string) error { - cmd := exec.Command("/bin/git", "--git-dir", GitDir(), "--no-color", "show", "main:"+path) - cmd.Dir = CFG.TldrDir - b, err := cmd.Output() - if err != nil { - return err - } - var k, v string - for _, v = range strings.Split(string(b), "\n") { - if strings.HasSuffix(v, ".md") { - v = strings.TrimSpace(v) - k = strings.TrimSuffix(v, ".md") - v = filepath.Join(path, v) - (*mp)[k] = v - } - } - return nil -} - -var shspt = `#!/bin/bash - -cd '{{ .TldrDir }}' -if [[ -d '{{ .GitDir }}' ]]; then - rm -rf '{{ .GitDir }}' -fi -set -e - -git clone --filter=blob:none --no-checkout '{{ .TldrGitSrc }}' '{{ .GitDir }}' -cd '{{ .GitDir }}' -git sparse-checkout init -git sparse-checkout set pages/linux/*.md pages/common/*.md -git checkout main -mkdir -p '{{ .TldrDir }}/pages' -find "{{ .GitDir }}/pages/common" -type f -name "*.md" -exec cp "{}" '{{ .TldrDir }}/pages/' \; -find "{{ .GitDir }}/pages/linux" -type f -name "*.md" -exec cp "{}" '{{ .TldrDir }}/pages/' \; -` - var TldrPagesMap = make(map[string]string) - -var PageDir string - -func updateTldrPages() error { - sh := strings.ReplaceAll(shspt, "{{ .TldrDir }}", CFG.TldrDir) - sh = strings.ReplaceAll(sh, "{{ .GitDir }}", GitDir()) - sh = strings.ReplaceAll(sh, "{{ .TldrGitSrc }}", CFG.TldrGitSrc) - upcmd := filepath.Join(CFG.TldrDir, "update.sh") - _ = os.Remove(upcmd) - err := os.WriteFile(upcmd, []byte(sh), 0755) - if err != nil { - return fmt.Errorf("failed to write update script: %w", err) - } - cmd := exec.Command(upcmd) - cmd.Env = os.Environ() - cmd.Dir = CFG.TldrDir - _, err = cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("failed to run update script: %w", err) - } - return nil -} func Open() { if !CFG.TldrPages { return } - PageDir = filepath.Join(CFG.TldrDir, "pages") - update := false - st, err := os.Stat(PageDir) + _ = os.MkdirAll(CFG.TldrDir, 0755) + var cmd *exec.Cmd + st, err := os.Stat(GitDir()) if err != nil && os.IsNotExist(err) { - err = os.MkdirAll(PageDir, 0755) - logs.CheckFatal("unable to create tldr pages directory", err) - update = true - } else if time.Now().After(st.ModTime().AddDate(0, 0, 14)) { - update = true + cmd = exec.Command("/bin/git", "clone", "--bare", CFG.TldrGitSrc, GitDir()) + } else if err != nil { + logs.CheckFatal("unable to access tldr pages repository", err) + } else if !st.IsDir() { + logs.Fatalf("tldr pages repository is not a directory: %s", GitDir()) + } else if st.ModTime().Before(time.Now().AddDate(0, 0, -14)) { + cmd = exec.Command("/bin/git", "--git-dir", GitDir(), "fetch", "--all") } - if update { - logs.Log("Updating tldr pages...") - logs.CheckFatal("unable to update tldr pages", err) - logs.Log("Tldr pages updated successfully.") + if cmd != nil { + cmd.Dir = CFG.TldrDir + err = cmd.Run() + logs.CheckFatal("unable to clone tldr pages repository", err) } TldrPagesMap = make(map[string]string) - var de []os.DirEntry - de, err = os.ReadDir(PageDir) - logs.CheckFatal("unable to read tldr pages directory", err) - var name string - for _, d := range de { - if d.IsDir() { - continue + fn := func(path string) { + cmd = exec.Command("/bin/git", "--no-pager", "--git-dir", GitDir(), "ls-tree", "--name-only", "main:pages/"+path) + cmd.Dir = CFG.TldrDir + var b []byte + b, err = cmd.Output() + logs.CheckFatal("unable to list tldr pages", err) + for _, line := range strings.Split(string(b), "\n") { + line = strings.TrimSpace(line) + if len(line) == 0 || !strings.HasSuffix(line, ".md") { + continue + } + name := strings.TrimSuffix(line, ".md") + TldrPagesMap[name] = filepath.Join(path, line) } - name = strings.TrimSuffix(d.Name(), ".md") - TldrPagesMap[name] = filepath.Join(PageDir, d.Name()) } + fn("common") + fn("linux") } type TldrPage struct {@@ -137,12 +85,17 @@ err := p.findPath()
if err != nil { return err } - // cmd := exec.Command("/bin/git", "--git-dir", GitDir(), "--no-color", "show", "main:"+p.Path) - // cmd.Dir = CFG.TldrDir - // b, err := cmd.Output() - b, err := os.ReadFile(p.Path) - p.Content = string(b) - return err + cmd := exec.Command("/bin/git", "--git-dir", GitDir(), "show", "main:pages/"+p.Path) + cmd.Dir = CFG.TldrDir + b, err := cmd.Output() + if err != nil { + return err + } + if len(b) == 0 { + return errors.New("Page not found: " + p.Name) + } + p.Content = strings.TrimSpace(string(b)) + return nil } /*