repos / dbin

📦 Poor man's package manager.
git clone https://github.com/xplshn/dbin.git

dbin / misc / cmd / AM-support
xplshn  ·  2025-08-13

meta-to-am.go

Go
  1package main
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"io"
  7	"net/http"
  8	"os"
  9	"strings"
 10)
 11
 12const pipeRepl = "ǀ" // Replacement for `|` to avoid breaking the MD table
 13
 14type Metadata struct {
 15	Bin  []Package `json:"bin"`
 16	Pkg  []Package `json:"pkg"`
 17	Base []Package `json:"base"`
 18}
 19
 20type Package struct {
 21	Pkg         string `json:"pkg"`
 22	PkgName     string `json:"pkg_name"`
 23	Description string `json:"description"`
 24	SrcURL      string `json:"src_url"`
 25	Homepage    string `json:"homepage"`
 26	DownloadURL string `json:"download_url"`
 27	Bsum        string `json:"bsum"`
 28}
 29
 30// Utility function to replace `|` in string fields
 31func replacePipeFields(pkg *Package) {
 32	pkg.PkgName = strings.ReplaceAll(pkg.PkgName, "|", pipeRepl)
 33	pkg.Description = strings.ReplaceAll(pkg.Description, "|", pipeRepl)
 34	pkg.SrcURL = strings.ReplaceAll(pkg.SrcURL, "|", pipeRepl)
 35	pkg.Homepage = strings.ReplaceAll(pkg.Homepage, "|", pipeRepl)
 36	pkg.DownloadURL = strings.ReplaceAll(pkg.DownloadURL, "|", pipeRepl)
 37}
 38
 39// Utility function to replace empty strings with "nil"
 40func replaceEmptyWithNil(value string) string {
 41	if value == "" {
 42		return "nil"
 43	}
 44	return value
 45}
 46
 47// Utility function to process PkgName as required
 48func processPkgName(pkgName string) string {
 49	// Step 1: Convert to lowercase
 50	pkgName = strings.ToLower(pkgName)
 51	// Step 2: Replace spaces with hyphens
 52	pkgName = strings.ReplaceAll(pkgName, " ", "-")
 53	// Step 3: Append `.appbundle` if not already present
 54	if !strings.HasSuffix(pkgName, ".appbundle") {
 55		pkgName += ".dwfs.appbundle"
 56	}
 57	return pkgName
 58}
 59
 60func main() {
 61	url := "https://github.com/xplshn/AppBundleHUB/releases/download/latest_metadata/metadata_x86_64-Linux.json"
 62
 63	resp, err := http.Get(url)
 64	if err != nil {
 65		fmt.Println("Error fetching JSON data:", err)
 66		return
 67	}
 68	defer resp.Body.Close()
 69
 70	body, err := io.ReadAll(resp.Body)
 71	if err != nil {
 72		fmt.Println("Error reading JSON data:", err)
 73		return
 74	}
 75
 76	var metadata Metadata
 77	err = json.Unmarshal(body, &metadata)
 78	if err != nil {
 79		fmt.Println("Error parsing JSON data:", err)
 80		return
 81	}
 82
 83	file, err := os.Create("AM.txt")
 84	if err != nil {
 85		fmt.Println("Error creating output file:", err)
 86		return
 87	}
 88	defer file.Close()
 89
 90	// Process each package and write to the file
 91	for _, pkg := range append(append(metadata.Bin, metadata.Pkg...), metadata.Base...) {
 92		// Replace empty fields with "nil"
 93		pkg.PkgName = replaceEmptyWithNil(pkg.PkgName)
 94		pkg.Description = replaceEmptyWithNil(pkg.Description)
 95		pkg.SrcURL = replaceEmptyWithNil(pkg.SrcURL)
 96		pkg.Homepage = replaceEmptyWithNil(pkg.Homepage)
 97		pkg.DownloadURL = replaceEmptyWithNil(pkg.DownloadURL)
 98
 99		// If both SrcURL and Homepage are empty, fall back to "nil"
100		webURL := pkg.SrcURL
101		if webURL == "nil" {
102			webURL = pkg.Homepage
103		}
104		if webURL == "nil" {
105			webURL = pkg.DownloadURL
106		}
107
108		// Replace `|` in fields
109		replacePipeFields(&pkg)
110
111		// Process PkgName
112		pkgName := processPkgName(pkg.PkgName)
113
114		// Handle bsum
115		bsum := pkg.Bsum
116		if len(bsum) > 12 {
117			bsum = bsum[:12]
118		} else {
119			bsum = "nil"
120		}
121
122		// Write formatted data to file
123		file.WriteString(fmt.Sprintf("| %s | %s | %s | %s | %s |\n",
124			pkgName, pkg.Description, webURL, pkg.DownloadURL, bsum))
125	}
126
127	fmt.Println("Data has been written to AM.txt")
128}