|
|
- package Package
-
- import (
- "errors"
- "fmt"
- "os"
- "os/exec"
-
- "PackageManager/Archive"
- "PackageManager/Client/Database"
- "PackageManager/Variables"
- )
-
- func runInstallCmd(cmdStr string) error {
- var (
- cmd *exec.Cmd
- )
-
- if cmdStr == "" {
- return nil
- }
-
- cmd = exec.Command("/bin/sh", "-c", cmdStr)
- cmd.Dir = Variables.RootDir
- cmd.Stdin = os.Stdin
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
- return cmd.Run()
- }
-
- func CheckPackageDependancies(deps map[string]string) ([]int64, error) {
- var (
- name, version string
- depIds []int64
- id int64
- e error
- )
-
- for name, version = range deps {
- id, e = Database.IsPackageInstalled(name, version)
- if e != nil {
- return depIds, e
- }
- depIds = append(depIds, id)
- }
-
- return depIds, e
- }
-
- func InstallPackage(pkgs []string) error {
- var (
- manifest Manifest
- depIds []int64
- pkg string
- mStr string
- e error
- )
-
- for _, pkg = range pkgs {
- _, e = os.Stat(pkg)
- if os.IsNotExist(e) {
- return errors.New(fmt.Sprintf("Invalid package %s", pkg))
- }
- }
-
- for _, pkg = range pkgs {
- fmt.Printf(
- "Installing %s...\n",
- pkg,
- )
-
- mStr, e = Archive.ExtractManifestFile(pkg)
- if e != nil {
- return e
- }
-
- manifest, e = ParseManifestFile(mStr)
- if e != nil {
- return e
- }
-
- if !Variables.IgnoreDepsCheck {
- depIds, e = CheckPackageDependancies(manifest.Dependancies)
- if e != nil {
- return e
- }
- }
-
- e = runInstallCmd(manifest.PreInstallCmds)
- if e != nil {
- return e
- }
-
- e = Archive.ExtractArchive(pkg, Variables.RootDir)
- if e != nil {
- return e
- }
-
- e = runInstallCmd(manifest.PostInstallCmds)
- if e != nil {
- return e
- }
-
- e = Database.InsertPackage(manifest.Name, manifest.Version, depIds)
- if e != nil {
- return e
- }
-
- fmt.Printf(
- "%s successfully installed\n",
- pkg,
- )
- }
-
- return nil
- }
|