diff --git a/Client/Package/CreatePackage.go b/Client/Package/CreatePackage.go index 50e878f..27e3c2c 100644 --- a/Client/Package/CreatePackage.go +++ b/Client/Package/CreatePackage.go @@ -129,6 +129,8 @@ func CreatePackage() error { Filesystem.CopyFile(file, filepath.Join(tmpDir, file)) } + // TODO: Add dependancy management here + e = Archive.TarGzip(tmpDir, pkgNameVersion+".tar.gz") if e != nil { return e diff --git a/Client/Package/InstallPackage.go b/Client/Package/InstallPackage.go new file mode 100644 index 0000000..136d38a --- /dev/null +++ b/Client/Package/InstallPackage.go @@ -0,0 +1,29 @@ +package Package + +import ( + "errors" + "fmt" + "os" + + "PackageManager/Archive" +) + +func InstallPackage(pkgs []string) error { + var ( + pkg 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 { + e = Archive.UntarGzip(pkg, "/") + } + + return nil +} diff --git a/Client/main.go b/Client/main.go index faec932..b875ccc 100644 --- a/Client/main.go +++ b/Client/main.go @@ -8,18 +8,27 @@ import ( "PackageManager/Client/Filesystem" "PackageManager/Client/Package" "PackageManager/Color" + "PackageManager/Helper" ) func main() { var ( - updateFilesytemFlag bool - updateFilesytemFlagLong bool - createPackageFlag bool - createPackageFlagLong bool + updateFilesytemFlag bool + updateFilesytemFlagLong bool + createPackageFlag bool + createPackageFlagLong bool + installLocalPackageFlag bool + installLocalPackageFlagLong bool e error ) + e = Helper.CheckRoot() + if e != nil { + fmt.Println(Color.Fatal(e)) + return + } + e = Database.InitDB() if e != nil { panic(e) @@ -27,10 +36,13 @@ func main() { // Initialise flags flag.BoolVar(&updateFilesytemFlag, "Uf", false, "Update filesystem database") - flag.BoolVar(&updateFilesytemFlagLong, "update-filesystem", false, "Update filesystem database") + flag.BoolVar(&updateFilesytemFlagLong, "-update-filesystem", false, "Update filesystem database") flag.BoolVar(&createPackageFlag, "Cp", false, "Create package") - flag.BoolVar(&createPackageFlagLong, "create-package", false, "Create Package") + flag.BoolVar(&createPackageFlagLong, "-create-package", false, "Create Package") + + flag.BoolVar(&installLocalPackageFlag, "Il", false, "Install local package") + flag.BoolVar(&installLocalPackageFlagLong, "-install-local-package", false, "Install local Package") flag.Parse() @@ -51,6 +63,14 @@ func main() { return } + if installLocalPackageFlag || installLocalPackageFlagLong { + e = Package.InstallPackage(flag.Args()) + if e != nil { + panic(e) + } + return + } + flag.Usage() fmt.Println(Color.Fatal("Nothing to do")) diff --git a/Helper/CheckRoot.go b/Helper/CheckRoot.go new file mode 100644 index 0000000..925b90c --- /dev/null +++ b/Helper/CheckRoot.go @@ -0,0 +1,42 @@ +package Helper + +import ( + "errors" + "os/exec" + "strconv" +) + +func CheckRoot() error { + var ( + cmd *exec.Cmd + output []byte + i int + e error + ) + + // TODO Make cross platform + + cmd = exec.Command("id", "-u") + output, e = cmd.Output() + + if e != nil { + return e + } + + // output has trailing \n + // need to remove the \n + // otherwise it will cause error for strconv.Atoi + // log.Println(output[:len(output)-1]) + + // 0 = root, 501 = non-root user + i, e = strconv.Atoi(string(output[:len(output)-1])) + if e != nil { + return e + } + + if i != 0 { + return errors.New("Please run as root") + } + + return nil +}