diff --git a/main.go b/main.go index 28026d4..33fe539 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ var pkgListNames = false var reinstall = false var reinstallAll = false var noOptional = false +var installationReason = "" var nosync = true var removeUnused = false var doCleanup = false @@ -178,10 +179,21 @@ func resolveCommand() { 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{ - Actions: make([]utils.OperationAction, 0), - UnresolvedDepends: make([]string, 0), - RootDir: rootDir, + Actions: make([]utils.OperationAction, 0), + UnresolvedDepends: make([]string, 0), + RootDir: rootDir, + ForceInstallationReason: ir, } // Search for packages @@ -296,9 +308,10 @@ func resolveCommand() { } operation := utils.BPMOperation{ - Actions: make([]utils.OperationAction, 0), - UnresolvedDepends: make([]string, 0), - RootDir: rootDir, + Actions: make([]utils.OperationAction, 0), + UnresolvedDepends: make([]string, 0), + RootDir: rootDir, + ForceInstallationReason: utils.Unknown, } // Search for packages @@ -537,7 +550,7 @@ func printHelp() { fmt.Println(" -c lists the amount of installed packages") fmt.Println(" -n lists only the names of installed packages") fmt.Println("-> bpm search | Searches for packages through declared repositories") - fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --reinstall-all, --no-optional] | installs the following files") + fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --reinstall-all, --no-optional, --installation-reason] | installs the following files") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") 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-all Same as --reinstall but also reinstalls dependencies") fmt.Println(" --no-optional Prevents installation of optional dependencies") + fmt.Println(" --installation-reason= 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(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") @@ -560,11 +574,12 @@ func printHelp() { fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -y skips the confirmation prompt") - fmt.Println("-> bpm remove [-R, -v, -y, --unused] | removes the following packages") + fmt.Println("-> bpm remove [-R, -v, -y, --unused, --cleanup] | removes the following packages") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -y skips the confirmation prompt") 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(" -v Show additional information about what BPM is doing") fmt.Println(" -R= 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(&reinstallAll, "reinstall-all", false, "Same as --reinstall but also reinstalls 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 // Update flags 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(&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(&doCleanup, "cleanup", false, "Perform a dependency cleanup ") + removeFlagSet.BoolVar(&doCleanup, "cleanup", false, "Perform a dependency cleanup") removeFlagSet.Usage = printHelp // Cleanup flags cleanupFlagSet := flag.NewFlagSet("Cleanup flags", flag.ExitOnError) diff --git a/utils/operations.go b/utils/operations.go index 7f73f00..462dee5 100644 --- a/utils/operations.go +++ b/utils/operations.go @@ -10,9 +10,10 @@ import ( ) type BPMOperation struct { - Actions []OperationAction - UnresolvedDepends []string - RootDir string + Actions []OperationAction + UnresolvedDepends []string + RootDir string + ForceInstallationReason InstallationReason } 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)) } 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) if err != nil { return errors.New(fmt.Sprintf("could not set installation reason for package (%s): %s\n", value.BpmPackage.PkgInfo.Name, err)) diff --git a/utils/package_utils.go b/utils/package_utils.go index 06dc952..17fba2a 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -1185,11 +1185,8 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, return err } pkgDir := path.Join(installedDir, bpmpkg.PkgInfo.Name) - err = os.RemoveAll(pkgDir) - if err != nil { - return err - } - err = os.Mkdir(pkgDir, 0755) + + err = os.MkdirAll(pkgDir, 0755) if err != nil { return err }