package Package
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
|
|
"PackageManager/Archive"
|
|
"PackageManager/Client/Database"
|
|
"PackageManager/Variables"
|
|
)
|
|
|
|
func runInstallCmd(cmdStrs []string) error {
|
|
var (
|
|
cmd *exec.Cmd
|
|
cmdStr string
|
|
err error
|
|
)
|
|
|
|
if len(cmdStrs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for _, cmdStr = range cmdStrs {
|
|
cmd = exec.Command("/bin/sh", "-c", cmdStr)
|
|
cmd.Dir = Variables.RootDir
|
|
cmd.Stdin = os.Stdin
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
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 fmt.Errorf("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
|
|
}
|