Added --installation-reason flag

This commit is contained in:
EnumDev 2024-11-05 15:34:55 +02:00
parent b625fe97ef
commit 2accc30390
3 changed files with 37 additions and 18 deletions

34
main.go
View File

@ -35,6 +35,7 @@ var pkgListNames = false
var reinstall = false var reinstall = false
var reinstallAll = false var reinstallAll = false
var noOptional = false var noOptional = false
var installationReason = ""
var nosync = true var nosync = true
var removeUnused = false var removeUnused = false
var doCleanup = false var doCleanup = false
@ -178,10 +179,21 @@ func resolveCommand() {
return return
} }
// Check if installationReason argument is valid
ir := utils.Unknown
if installationReason == "manual" {
ir = utils.Manual
} else if installationReason == "dependency" {
ir = utils.Dependency
} else if installationReason != "" {
log.Fatalf("Error: %s is not a valid installation reason", installationReason)
}
operation := utils.BPMOperation{ operation := utils.BPMOperation{
Actions: make([]utils.OperationAction, 0), Actions: make([]utils.OperationAction, 0),
UnresolvedDepends: make([]string, 0), UnresolvedDepends: make([]string, 0),
RootDir: rootDir, RootDir: rootDir,
ForceInstallationReason: ir,
} }
// Search for packages // Search for packages
@ -296,9 +308,10 @@ func resolveCommand() {
} }
operation := utils.BPMOperation{ operation := utils.BPMOperation{
Actions: make([]utils.OperationAction, 0), Actions: make([]utils.OperationAction, 0),
UnresolvedDepends: make([]string, 0), UnresolvedDepends: make([]string, 0),
RootDir: rootDir, RootDir: rootDir,
ForceInstallationReason: utils.Unknown,
} }
// Search for packages // Search for packages
@ -537,7 +550,7 @@ func printHelp() {
fmt.Println(" -c lists the amount of installed packages") fmt.Println(" -c lists the amount of installed packages")
fmt.Println(" -n lists only the names of installed packages") fmt.Println(" -n lists only the names of installed packages")
fmt.Println("-> bpm search <search terms...> | Searches for packages through declared repositories") fmt.Println("-> bpm search <search terms...> | Searches for packages through declared repositories")
fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --reinstall-all, --no-optional] <packages...> | installs the following files") fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --reinstall-all, --no-optional, --installation-reason] <packages...> | installs the following files")
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(" -v Show additional information about what BPM is doing") fmt.Println(" -v Show additional information about what BPM is doing")
fmt.Println(" -y skips the confirmation prompt") fmt.Println(" -y skips the confirmation prompt")
@ -549,6 +562,7 @@ func printHelp() {
fmt.Println(" --reinstall Reinstalls packages even if they do not have a newer version available") fmt.Println(" --reinstall Reinstalls packages even if they do not have a newer version available")
fmt.Println(" --reinstall-all Same as --reinstall but also reinstalls dependencies") fmt.Println(" --reinstall-all Same as --reinstall but also reinstalls dependencies")
fmt.Println(" --no-optional Prevents installation of optional dependencies") fmt.Println(" --no-optional Prevents installation of optional dependencies")
fmt.Println(" --installation-reason=<manual/dependency> sets the installation reason for all newly installed packages")
fmt.Println("-> bpm update [-R, -v, -y, -f, --reinstall, --no-sync] | updates all packages that are available in the repositories") fmt.Println("-> bpm update [-R, -v, -y, -f, --reinstall, --no-sync] | updates all packages that are available in the repositories")
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(" -v Show additional information about what BPM is doing") fmt.Println(" -v Show additional information about what BPM is doing")
@ -560,11 +574,12 @@ func printHelp() {
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(" -v Show additional information about what BPM is doing") fmt.Println(" -v Show additional information about what BPM is doing")
fmt.Println(" -y skips the confirmation prompt") fmt.Println(" -y skips the confirmation prompt")
fmt.Println("-> bpm remove [-R, -v, -y, --unused] <packages...> | removes the following packages") fmt.Println("-> bpm remove [-R, -v, -y, --unused, --cleanup] <packages...> | removes the following packages")
fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -v Show additional information about what BPM is doing")
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(" -y skips the confirmation prompt") fmt.Println(" -y skips the confirmation prompt")
fmt.Println(" -unused removes only packages that aren't required as dependencies by other packages") fmt.Println(" -unused removes only packages that aren't required as dependencies by other packages")
fmt.Println(" -cleanup performs a dependency cleanup")
fmt.Println("-> bpm cleanup [-R, -v, -y] | remove all unused dependency packages") fmt.Println("-> bpm cleanup [-R, -v, -y] | remove all unused dependency packages")
fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -v Show additional information about what BPM is doing")
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")
@ -599,6 +614,7 @@ func resolveFlags() {
installFlagSet.BoolVar(&reinstall, "reinstall", false, "Reinstalls packages even if they do not have a newer version available") installFlagSet.BoolVar(&reinstall, "reinstall", false, "Reinstalls packages even if they do not have a newer version available")
installFlagSet.BoolVar(&reinstallAll, "reinstall-all", false, "Same as --reinstall but also reinstalls dependencies") installFlagSet.BoolVar(&reinstallAll, "reinstall-all", false, "Same as --reinstall but also reinstalls dependencies")
installFlagSet.BoolVar(&noOptional, "no-optional", false, "Prevents installation of optional dependencies") installFlagSet.BoolVar(&noOptional, "no-optional", false, "Prevents installation of optional dependencies")
installFlagSet.StringVar(&installationReason, "installation-reason", "", "Set the installation reason for all newly installed packages")
installFlagSet.Usage = printHelp installFlagSet.Usage = printHelp
// Update flags // Update flags
updateFlagSet := flag.NewFlagSet("Update flags", flag.ExitOnError) updateFlagSet := flag.NewFlagSet("Update flags", flag.ExitOnError)
@ -621,7 +637,7 @@ func resolveFlags() {
removeFlagSet.BoolVar(&verbose, "v", false, "Show additional information about what BPM is doing") removeFlagSet.BoolVar(&verbose, "v", false, "Show additional information about what BPM is doing")
removeFlagSet.BoolVar(&yesAll, "y", false, "Skip confirmation prompts") removeFlagSet.BoolVar(&yesAll, "y", false, "Skip confirmation prompts")
removeFlagSet.BoolVar(&removeUnused, "unused", false, "Removes only packages that aren't required as dependencies by other packages") removeFlagSet.BoolVar(&removeUnused, "unused", false, "Removes only packages that aren't required as dependencies by other packages")
removeFlagSet.BoolVar(&doCleanup, "cleanup", false, "Perform a dependency cleanup ") removeFlagSet.BoolVar(&doCleanup, "cleanup", false, "Perform a dependency cleanup")
removeFlagSet.Usage = printHelp removeFlagSet.Usage = printHelp
// Cleanup flags // Cleanup flags
cleanupFlagSet := flag.NewFlagSet("Cleanup flags", flag.ExitOnError) cleanupFlagSet := flag.NewFlagSet("Cleanup flags", flag.ExitOnError)

View File

@ -10,9 +10,10 @@ import (
) )
type BPMOperation struct { type BPMOperation struct {
Actions []OperationAction Actions []OperationAction
UnresolvedDepends []string UnresolvedDepends []string
RootDir string RootDir string
ForceInstallationReason InstallationReason
} }
func (operation *BPMOperation) ActionsContainPackage(pkg string) bool { func (operation *BPMOperation) ActionsContainPackage(pkg string) bool {
@ -396,7 +397,12 @@ func (operation *BPMOperation) Execute(verbose, force bool) error {
return errors.New(fmt.Sprintf("could not install package (%s): %s\n", bpmpkg.PkgInfo.Name, err)) return errors.New(fmt.Sprintf("could not install package (%s): %s\n", bpmpkg.PkgInfo.Name, err))
} }
fmt.Printf("Package (%s) was successfully installed\n", bpmpkg.PkgInfo.Name) fmt.Printf("Package (%s) was successfully installed\n", bpmpkg.PkgInfo.Name)
if value.IsDependency { if operation.ForceInstallationReason != Unknown {
err := SetInstallationReason(bpmpkg.PkgInfo.Name, operation.ForceInstallationReason, operation.RootDir)
if err != nil {
return errors.New(fmt.Sprintf("could not set installation reason for package (%s): %s\n", value.BpmPackage.PkgInfo.Name, err))
}
} else if value.IsDependency && !IsPackageInstalled(bpmpkg.PkgInfo.Name, operation.RootDir) {
err := SetInstallationReason(bpmpkg.PkgInfo.Name, Dependency, operation.RootDir) err := SetInstallationReason(bpmpkg.PkgInfo.Name, Dependency, operation.RootDir)
if err != nil { if err != nil {
return errors.New(fmt.Sprintf("could not set installation reason for package (%s): %s\n", value.BpmPackage.PkgInfo.Name, err)) return errors.New(fmt.Sprintf("could not set installation reason for package (%s): %s\n", value.BpmPackage.PkgInfo.Name, err))

View File

@ -1185,11 +1185,8 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc,
return err return err
} }
pkgDir := path.Join(installedDir, bpmpkg.PkgInfo.Name) pkgDir := path.Join(installedDir, bpmpkg.PkgInfo.Name)
err = os.RemoveAll(pkgDir)
if err != nil { err = os.MkdirAll(pkgDir, 0755)
return err
}
err = os.Mkdir(pkgDir, 0755)
if err != nil { if err != nil {
return err return err
} }