mirror of
https://github.com/EnumeratedDev/bpm-utils.git
synced 2026-09-16 11:06:11 +00:00
Add bpm-repo functionality
This commit is contained in:
@@ -1,3 +1,14 @@
|
||||
module git.enumerated.dev/bubble-package-manager/bpm-utils/src/bpm-repo
|
||||
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
bpm-utils-shared v1.0.0
|
||||
github.com/spf13/pflag v1.0.10
|
||||
)
|
||||
|
||||
require (
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace bpm-utils-shared => ../bpm-utils-shared
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
+64
-2
@@ -1,7 +1,69 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
bpmutilsshared "bpm-utils-shared"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
var createRepo = flag.BoolP("create", "c", false, "Create a new BPM repository")
|
||||
var updateDatabases = flag.BoolP("update-databases", "u", false, "Update update source and binary databases in current repository")
|
||||
|
||||
func main() {
|
||||
fmt.Println("bpm-repo")
|
||||
// Setup flags and help
|
||||
bpmutilsshared.SetupHelp("bpm-repo <options>", "Manage BPM repositories and databases")
|
||||
bpmutilsshared.SetupFlags()
|
||||
|
||||
if *createRepo {
|
||||
// Get current database
|
||||
repo := bpmutilsshared.GetRepository()
|
||||
if repo != "" {
|
||||
log.Fatal("Error: this command cannot be run inside a BPM repository")
|
||||
}
|
||||
|
||||
scanner := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Repository name: ")
|
||||
name, err := scanner.ReadString('\n')
|
||||
if err != nil {
|
||||
log.Fatalf("Error: could not read user input: %s", err)
|
||||
}
|
||||
fmt.Print("Repository description: ")
|
||||
desc, err := scanner.ReadString('\n')
|
||||
if err != nil {
|
||||
log.Fatalf("Error: could not read user input: %s", err)
|
||||
}
|
||||
|
||||
createRepository(strings.TrimSpace(name), strings.TrimSpace(desc))
|
||||
} else if *updateDatabases {
|
||||
// Get current database
|
||||
repo := bpmutilsshared.GetRepository()
|
||||
if repo == "" {
|
||||
log.Fatal("Error: this command may only be run inside a BPM repository")
|
||||
}
|
||||
|
||||
bpmutilsshared.UpdateDatabases(repo)
|
||||
} else {
|
||||
bpmutilsshared.ShowHelp()
|
||||
}
|
||||
}
|
||||
|
||||
func createRepository(name, description string) {
|
||||
err := os.Mkdir(name, 0755)
|
||||
if err != nil {
|
||||
log.Fatalf("Error: could not create directory: %s", err)
|
||||
}
|
||||
|
||||
contents := fmt.Sprintf("name: %s\ndescription: %s\n", name, description)
|
||||
err = os.WriteFile(path.Join(name, "bpm-repo.conf"), []byte(contents), 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("Error: could not write to file: %s", err)
|
||||
}
|
||||
|
||||
fmt.Println("Repository created successfully!")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package bpm_utils_shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type BPMDatabase struct {
|
||||
DatabaseVersion int `yaml:"database_version"`
|
||||
Entries map[string]BPMDatabaseEntry `yaml:"entries"`
|
||||
}
|
||||
|
||||
type BPMDatabaseEntry struct {
|
||||
PackageInfo *PackageInfo `yaml:"info"`
|
||||
Filepath string `yaml:"filepath"`
|
||||
DownloadSize int64 `yaml:"download_size"`
|
||||
InstalledSize int64 `yaml:"installed_size"`
|
||||
}
|
||||
|
||||
func ReadDatabase(path string) (*BPMDatabase, error) {
|
||||
// Read database file
|
||||
output, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
database := &BPMDatabase{}
|
||||
|
||||
// Unmarshal yaml
|
||||
err = yaml.Unmarshal(output, &database)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return database, nil
|
||||
}
|
||||
|
||||
func GenerateDatabase(path string) error {
|
||||
database := BPMDatabase{
|
||||
DatabaseVersion: 2,
|
||||
Entries: make(map[string]BPMDatabaseEntry),
|
||||
}
|
||||
|
||||
err := filepath.Walk(path, func(packagePath string, info fs.FileInfo, err error) error {
|
||||
if !strings.HasSuffix(packagePath, ".bpm") {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initialize database entry
|
||||
entry := BPMDatabaseEntry{}
|
||||
entry.DownloadSize = info.Size()
|
||||
entry.Filepath, err = filepath.Rel(path, packagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get package installed size
|
||||
cmd := exec.Command("tar", "-t", "-v", "-f", packagePath, "files.tar.gz")
|
||||
output, err := cmd.Output()
|
||||
if err == nil {
|
||||
entry.InstalledSize, err = strconv.ParseInt(strings.Fields(string(output))[2], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err.(*exec.ExitError).ExitCode() == 2 {
|
||||
entry.InstalledSize = 0
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get package info
|
||||
entry.PackageInfo, err = ReadPacakgeInfo(packagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add entry to database
|
||||
if _, ok := database.Entries[entry.PackageInfo.Name]; ok {
|
||||
return fmt.Errorf("package (%s) has already been added to the database", entry.PackageInfo.Name)
|
||||
}
|
||||
database.Entries[entry.PackageInfo.Name] = entry
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(&database)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.WriteFile(filepath.Join(path, "database.bpmdb"), data, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateDatabases(repo string) {
|
||||
if _, err := os.Stat(path.Join(repo, "source")); err == nil {
|
||||
err = GenerateDatabase(path.Join(repo, "source"))
|
||||
if err != nil {
|
||||
log.Fatalf("Error: could not generate source directory database: %s", err)
|
||||
}
|
||||
fmt.Println("Source directory database was generated successfully!")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path.Join(repo, "binary")); err == nil {
|
||||
err = GenerateDatabase(path.Join(repo, "binary"))
|
||||
if err != nil {
|
||||
log.Fatalf("Error: could not generate binary directory database: %s", err)
|
||||
}
|
||||
fmt.Println("Binary directory database was generated successfully!")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package bpm_utils_shared
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type PackageInfo struct {
|
||||
Name string `yaml:"name,omitempty"`
|
||||
Description string `yaml:"description,omitempty"`
|
||||
Version string `yaml:"version,omitempty"`
|
||||
Revision int `yaml:"revision,omitempty"`
|
||||
Url string `yaml:"url,omitempty"`
|
||||
License string `yaml:"license,omitempty"`
|
||||
Arch string `yaml:"architecture,omitempty"`
|
||||
Type string `yaml:"type,omitempty"`
|
||||
Keep []string `yaml:"keep,omitempty"`
|
||||
Depends []string `yaml:"depends,omitempty"`
|
||||
MakeDepends []string `yaml:"make_depends,omitempty"`
|
||||
OptionalDepends []string `yaml:"optional_depends,omitempty"`
|
||||
Conflicts []string `yaml:"conflicts,omitempty"`
|
||||
Replaces []string `yaml:"replaces,omitempty"`
|
||||
Provides []string `yaml:"provides,omitempty"`
|
||||
SplitPackages []*PackageInfo `yaml:"split_packages,omitempty"`
|
||||
}
|
||||
|
||||
func ReadPacakgeInfo(path string) (*PackageInfo, error) {
|
||||
// Extract package info using tar
|
||||
cmd := exec.Command("tar", "-x", "-f", path, "pkg.info", "-O")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pkgInfo := &PackageInfo{
|
||||
Name: "",
|
||||
Description: "",
|
||||
Version: "",
|
||||
Revision: 1,
|
||||
Url: "",
|
||||
License: "",
|
||||
Arch: "",
|
||||
Type: "",
|
||||
Keep: make([]string, 0),
|
||||
Depends: make([]string, 0),
|
||||
MakeDepends: make([]string, 0),
|
||||
OptionalDepends: make([]string, 0),
|
||||
Conflicts: make([]string, 0),
|
||||
Replaces: make([]string, 0),
|
||||
Provides: make([]string, 0),
|
||||
SplitPackages: make([]*PackageInfo, 0),
|
||||
}
|
||||
|
||||
// Unmarshal yaml
|
||||
err = yaml.Unmarshal(output, pkgInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pkgInfo, nil
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package bpm_utils_shared
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
func GetRepository() string {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for dir != "/" {
|
||||
if _, err := os.Stat(path.Join(dir, "bpm-repo.conf")); err == nil {
|
||||
return dir
|
||||
}
|
||||
dir = path.Dir(dir)
|
||||
}
|
||||
|
||||
if dir == "/" {
|
||||
return ""
|
||||
}
|
||||
return dir
|
||||
}
|
||||
Reference in New Issue
Block a user