Switch to yaml and preparation for repository functionality #3

Merged
EnumDev merged 5 commits from develop into master 2024-09-09 08:40:28 +00:00
2 changed files with 118 additions and 10 deletions
Showing only changes of commit ab75193022 - Show all commits

View File

@ -39,6 +39,43 @@ type PackageInfo struct {
Provides []string `yaml:"provides,omitempty"` Provides []string `yaml:"provides,omitempty"`
} }
type InstallationReason string
const (
Manual InstallationReason = "manual"
Dependency InstallationReason = "dependency"
Unknown InstallationReason = "unknown"
)
func GetInstallationReason(pkg, rootDir string) InstallationReason {
installedDir := path.Join(rootDir, "var/lib/bpm/installed/")
pkgDir := path.Join(installedDir, pkg)
if stat, err := os.Stat(path.Join(pkgDir, "installation_reason")); err != nil || stat.IsDir() {
return Manual
}
bytes, err := os.ReadFile(path.Join(pkgDir, "installation_reason"))
if err != nil {
return Unknown
}
reason := string(bytes)
if reason == "manual" {
return Manual
} else if reason == "dependency" {
return Dependency
}
return Unknown
}
func SetInstallationReason(pkg string, reason InstallationReason, rootDir string) error {
installedDir := path.Join(rootDir, "var/lib/bpm/installed/")
pkgDir := path.Join(installedDir, pkg)
err := os.WriteFile(path.Join(pkgDir, "installation_reason"), []byte(reason), 0644)
if err != nil {
return err
}
return nil
}
func GetPackageInfoRaw(filename string) (string, error) { func GetPackageInfoRaw(filename string) (string, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) { if _, err := os.Stat(filename); os.IsNotExist(err) {
return "", err return "", err
@ -316,7 +353,7 @@ func ReadPackageInfo(contents string, defaultValues bool) (*PackageInfo, error)
return &pkgInfo, nil return &pkgInfo, nil
} }
func CreateInfoFile(pkgInfo PackageInfo) string { func CreateInfoFile(pkgInfo *PackageInfo) string {
bytes, err := yaml.Marshal(&pkgInfo) bytes, err := yaml.Marshal(&pkgInfo)
if err != nil { if err != nil {
return "" return ""
@ -324,6 +361,59 @@ func CreateInfoFile(pkgInfo PackageInfo) string {
return string(bytes) return string(bytes)
} }
func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showRemoteInfo, showInstallationReason bool, pkgInfo *PackageInfo, rootDir string) string {
ret := make([]string, 0)
appendArray := func(label string, array []string) {
if len(array) == 0 {
return
}
ret = append(ret, fmt.Sprintf("%s: %s", label, strings.Join(array, ", ")))
}
appendMap := func(label string, m map[string][]string) {
if len(m) == 0 {
return
}
ret = append(ret, label+":")
for k, v := range m {
if len(v) == 0 {
continue
}
ret = append(ret, fmt.Sprintf(" %s: %s", k, strings.Join(v, ", ")))
}
}
ret = append(ret, "Name: "+pkgInfo.Name)
ret = append(ret, "Description: "+pkgInfo.Description)
ret = append(ret, "Version: "+pkgInfo.Version)
ret = append(ret, "URL: "+pkgInfo.Url)
ret = append(ret, "License: "+pkgInfo.License)
if showArchitecture {
ret = append(ret, "Architecture: "+pkgInfo.Arch)
}
if showType {
ret = append(ret, "Type: "+pkgInfo.Type)
}
if showPackageRelations {
appendArray("Dependencies", pkgInfo.Depends)
appendArray("Make Dependencies", pkgInfo.MakeDepends)
appendArray("Provided packages", pkgInfo.Provides)
appendArray("Conflicting packages", pkgInfo.Conflicts)
appendArray("Optional dependencies", pkgInfo.Optional)
appendMap("Conditional dependencies", pkgInfo.ConditionalDepends)
appendMap("Conditional make dependencies", pkgInfo.ConditionalMakeDepends)
appendMap("Conditional conflicting packages", pkgInfo.ConditionalConflicts)
appendMap("Conditional optional dependencies", pkgInfo.ConditionalOptional)
}
if showInstallationReason {
if IsPackageInstalled(pkgInfo.Name, rootDir) {
ret = append(ret, "Installed: yes")
ret = append(ret, "Installation Reason: "+string(GetInstallationReason(pkgInfo.Name, rootDir)))
} else {
ret = append(ret, "Installed: no")
}
}
return strings.Join(ret, "\n")
}
func extractPackage(pkgInfo *PackageInfo, verbose bool, filename, rootDir string) (error, []string) { func extractPackage(pkgInfo *PackageInfo, verbose bool, filename, rootDir string) (error, []string) {
var files []string var files []string
if !IsPackageInstalled(pkgInfo.Name, rootDir) { if !IsPackageInstalled(pkgInfo.Name, rootDir) {
@ -848,7 +938,7 @@ fi
compiledInfo = *pkgInfo compiledInfo = *pkgInfo
compiledInfo.Type = "binary" compiledInfo.Type = "binary"
compiledInfo.Arch = GetArch() compiledInfo.Arch = GetArch()
err = os.WriteFile(path.Join(temp, "pkg.info"), []byte(CreateInfoFile(compiledInfo)), 0644) err = os.WriteFile(path.Join(temp, "pkg.info"), []byte(CreateInfoFile(&compiledInfo)), 0644)
if err != nil { if err != nil {
return err, nil return err, nil
} }

28
main.go
View File

@ -30,6 +30,7 @@ var buildSource = false
var skipCheck = false var skipCheck = false
var keepTempDir = false var keepTempDir = false
var forceInstall = false var forceInstall = false
var showInstalled = false
var pkgListNumbers = false var pkgListNumbers = false
var pkgListNames = false var pkgListNames = false
@ -82,12 +83,27 @@ func resolveCommand() {
return return
} }
for n, pkg := range packages { for n, pkg := range packages {
info := bpm_utils.GetPackageInfo(pkg, rootDir, false) var info *bpm_utils.PackageInfo
if _, err := os.Stat(pkg); err == nil && !showInstalled {
info, err = bpm_utils.ReadPackage(pkg)
if err != nil {
fmt.Printf("File (%s) could not be read\n", pkg)
continue
}
} else {
info = bpm_utils.GetPackageInfo(pkg, rootDir, false)
if info == nil { if info == nil {
fmt.Printf("Package (%s) could not be found\n", pkg) fmt.Printf("Package (%s) could not be found\n", pkg)
continue continue
} }
fmt.Print("----------------\n" + bpm_utils.CreateInfoFile(*info)) }
fmt.Println("----------------")
if showInstalled {
fmt.Println(bpm_utils.CreateReadableInfo(false, false, true, false, true, info, rootDir))
} else {
fmt.Println(bpm_utils.CreateReadableInfo(true, true, true, true, true, info, rootDir))
}
if n == len(packages)-1 { if n == len(packages)-1 {
fmt.Println("----------------") fmt.Println("----------------")
} }
@ -115,7 +131,7 @@ func resolveCommand() {
fmt.Printf("Package (%s) could not be found\n", pkg) fmt.Printf("Package (%s) could not be found\n", pkg)
continue continue
} }
fmt.Print("----------------\n" + bpm_utils.CreateInfoFile(*info)) fmt.Println("----------------\n" + bpm_utils.CreateReadableInfo(true, true, true, true, true, info, rootDir))
if n == len(packages)-1 { if n == len(packages)-1 {
fmt.Println("----------------") fmt.Println("----------------")
} }
@ -137,7 +153,7 @@ func resolveCommand() {
log.Fatalf("Could not read package\nError: %s\n", err) log.Fatalf("Could not read package\nError: %s\n", err)
} }
if !yesAll { if !yesAll {
fmt.Print("----------------\n" + bpm_utils.CreateInfoFile(*pkgInfo)) fmt.Println("----------------\n" + bpm_utils.CreateReadableInfo(true, true, true, true, false, pkgInfo, rootDir))
fmt.Println("----------------") fmt.Println("----------------")
} }
verb := "install" verb := "install"
@ -238,7 +254,7 @@ func resolveCommand() {
fmt.Printf("Package (%s) could not be found\n", pkg) fmt.Printf("Package (%s) could not be found\n", pkg)
continue continue
} }
fmt.Print("----------------\n" + bpm_utils.CreateInfoFile(*pkgInfo)) fmt.Println("----------------\n" + bpm_utils.CreateReadableInfo(true, true, true, true, true, pkgInfo, rootDir))
fmt.Println("----------------") fmt.Println("----------------")
if rootDir != "/" { if rootDir != "/" {
fmt.Println("Warning: Operating in " + rootDir) fmt.Println("Warning: Operating in " + rootDir)
@ -319,6 +335,7 @@ func printHelp() {
fmt.Println("-> bpm version | shows information on the installed version of bpm") fmt.Println("-> bpm version | shows information on the installed version of bpm")
fmt.Println("-> bpm info [-R] | shows information on an installed package") fmt.Println("-> bpm info [-R] | shows information on an installed package")
fmt.Println(" -R=<path> lets you define the root path which will be used") fmt.Println(" -R=<path> lets you define the root path which will be used")
fmt.Println(" -i shows information about the currently installed package")
fmt.Println("-> bpm list [-R, -c, -n] | lists all installed packages") fmt.Println("-> bpm list [-R, -c, -n] | lists all installed packages")
fmt.Println(" -R=<path> lets you define the root path which will be used") fmt.Println(" -R=<path> lets you define the root path which will be used")
fmt.Println(" -c lists the amount of installed packages") fmt.Println(" -c lists the amount of installed packages")
@ -351,6 +368,7 @@ func resolveFlags() {
// Info flags // Info flags
infoFlagSet := flag.NewFlagSet("Info flags", flag.ExitOnError) infoFlagSet := flag.NewFlagSet("Info flags", flag.ExitOnError)
infoFlagSet.StringVar(&rootDir, "R", "/", "Set the destination root") infoFlagSet.StringVar(&rootDir, "R", "/", "Set the destination root")
infoFlagSet.BoolVar(&showInstalled, "i", false, "Shows information about the currently installed package")
infoFlagSet.Usage = printHelp infoFlagSet.Usage = printHelp
// Install flags // Install flags
installFlagSet := flag.NewFlagSet("Install flags", flag.ExitOnError) installFlagSet := flag.NewFlagSet("Install flags", flag.ExitOnError)