mirror of
https://github.com/EnumeratedDev/bpm.git
synced 2026-09-16 10:36:12 +00:00
Expanded on top of the current argument system by adding flags
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -13,18 +14,12 @@ import (
|
||||
/* A simple-to-use package manager */
|
||||
/* ---------------------------------- */
|
||||
|
||||
var bpmVer = "0.0.1"
|
||||
var bpmVer = "0.0.3"
|
||||
var rootDir string = "/"
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Running %s %s\n", getKernel(), getArch())
|
||||
//fmt.Printf("Running %s %s\n", getKernel(), getArch())
|
||||
resolveCommand()
|
||||
/*_, err := readPackage("test_hello_package/hello.bpm")
|
||||
err := installPackage("test_hello_package/hello.bpm", rootDir)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read package\nError: %s\n", err)
|
||||
}
|
||||
removePackage("hello")*/
|
||||
}
|
||||
|
||||
func getArgs() []string {
|
||||
@@ -64,15 +59,18 @@ func getCommandType() commandType {
|
||||
default:
|
||||
return help
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func resolveCommand() {
|
||||
switch getCommandType() {
|
||||
case version:
|
||||
resolveFlags()
|
||||
fmt.Println("Bubble Package Manager (BPM)")
|
||||
fmt.Println("Version: " + bpmVer)
|
||||
case info:
|
||||
packages := getArgs()[1:]
|
||||
_, i := resolveFlags()
|
||||
packages := getArgs()[1+i:]
|
||||
if len(packages) == 0 {
|
||||
fmt.Println("No packages were given")
|
||||
return
|
||||
@@ -83,13 +81,36 @@ func resolveCommand() {
|
||||
fmt.Printf("Package (%s) could not be found\n", pkg)
|
||||
continue
|
||||
}
|
||||
fmt.Print("---------------\n" + createInfoFile(*info))
|
||||
fmt.Print("----------------\n" + createInfoFile(*info))
|
||||
if n == len(packages)-1 {
|
||||
fmt.Println("---------------")
|
||||
fmt.Println("----------------")
|
||||
}
|
||||
}
|
||||
case list:
|
||||
resolveFlags()
|
||||
packages, err := getInstalledPackages()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not get installed packages\nError: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if len(packages) == 0 {
|
||||
fmt.Println("No packages have been installed")
|
||||
return
|
||||
}
|
||||
for n, pkg := range packages {
|
||||
info := getPackageInfo(pkg)
|
||||
if info == nil {
|
||||
fmt.Printf("Package (%s) could not be found\n", pkg)
|
||||
continue
|
||||
}
|
||||
fmt.Print("----------------\n" + createInfoFile(*info))
|
||||
if n == len(packages)-1 {
|
||||
fmt.Println("----------------")
|
||||
}
|
||||
}
|
||||
case install:
|
||||
files := getArgs()[1:]
|
||||
flags, i := resolveFlags()
|
||||
files := getArgs()[1+i:]
|
||||
if len(files) == 0 {
|
||||
fmt.Println("No files were given to install")
|
||||
return
|
||||
@@ -99,15 +120,42 @@ func resolveCommand() {
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read package\nError: %s\n", err)
|
||||
}
|
||||
fmt.Print("---------------\n" + createInfoFile(*pkgInfo))
|
||||
fmt.Println("---------------")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Do you wish to install this package? [y\\N] ")
|
||||
text, _ := reader.ReadString('\n')
|
||||
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
|
||||
fmt.Printf("Skipping package (%s)...\n", pkgInfo.name)
|
||||
continue
|
||||
fmt.Print("----------------\n" + createInfoFile(*pkgInfo))
|
||||
fmt.Println("----------------")
|
||||
if isPackageInstalled(pkgInfo.name) {
|
||||
if !slices.Contains(flags, "y") {
|
||||
installedInfo := getPackageInfo(pkgInfo.name)
|
||||
if strings.Compare(pkgInfo.version, installedInfo.version) > 0 {
|
||||
fmt.Println("This file contains a newer version of this package (" + installedInfo.version + " -> " + pkgInfo.version + ")")
|
||||
fmt.Print("Do you wish to update this package? [y\\N] ")
|
||||
} else if strings.Compare(pkgInfo.version, installedInfo.version) < 0 {
|
||||
fmt.Println("This file contains an older version of this package (" + installedInfo.version + " -> " + pkgInfo.version + ")")
|
||||
fmt.Print("Do you wish to downgrade this package? (Not recommended) [y\\N] ")
|
||||
} else if strings.Compare(pkgInfo.version, installedInfo.version) == 0 {
|
||||
fmt.Println("This package is already installed on the system and is up to date")
|
||||
fmt.Print("Do you wish to reinstall this package? [y\\N] ")
|
||||
}
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
text, _ := reader.ReadString('\n')
|
||||
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
|
||||
fmt.Printf("Skipping package (%s)...\n", pkgInfo.name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
err := removePackage(pkgInfo.name)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not remove current version of the package\nError: %s\n", err)
|
||||
}
|
||||
} else if !slices.Contains(flags, "y") {
|
||||
fmt.Print("Do you wish to install this package? [y\\N] ")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
text, _ := reader.ReadString('\n')
|
||||
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
|
||||
fmt.Printf("Skipping package (%s)...\n", pkgInfo.name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
err = installPackage(file, rootDir)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not install package\nError: %s\n", err)
|
||||
@@ -115,7 +163,8 @@ func resolveCommand() {
|
||||
fmt.Printf("Package (%s) was successfully installed!\n", pkgInfo.name)
|
||||
}
|
||||
case remove:
|
||||
packages := getArgs()[1:]
|
||||
flags, i := resolveFlags()
|
||||
packages := getArgs()[1+i:]
|
||||
if len(packages) == 0 {
|
||||
fmt.Println("No packages were given")
|
||||
return
|
||||
@@ -126,14 +175,16 @@ func resolveCommand() {
|
||||
fmt.Printf("Package (%s) could not be found\n", pkg)
|
||||
continue
|
||||
}
|
||||
fmt.Print("---------------\n" + createInfoFile(*pkgInfo))
|
||||
fmt.Println("---------------")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Do you wish to remove this package? [y\\N] ")
|
||||
text, _ := reader.ReadString('\n')
|
||||
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
|
||||
fmt.Printf("Skipping package (%s)...\n", pkgInfo.name)
|
||||
continue
|
||||
fmt.Print("----------------\n" + createInfoFile(*pkgInfo))
|
||||
fmt.Println("----------------")
|
||||
if !slices.Contains(flags, "y") {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Do you wish to remove this package? [y\\N] ")
|
||||
text, _ := reader.ReadString('\n')
|
||||
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
|
||||
fmt.Printf("Skipping package (%s)...\n", pkgInfo.name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
err := removePackage(pkg)
|
||||
if err != nil {
|
||||
@@ -142,6 +193,48 @@ func resolveCommand() {
|
||||
fmt.Printf("Package (%s) was successfully removed!\n", pkgInfo.name)
|
||||
}
|
||||
default:
|
||||
fmt.Println("help...")
|
||||
fmt.Println("------Help------")
|
||||
fmt.Println("bpm version | shows information on the installed version of bpm")
|
||||
fmt.Println("bpm info | shows information on an installed package")
|
||||
fmt.Println("bpm list [-e] | lists all installed packages. -e lists only explicitly installed packages")
|
||||
fmt.Println("bpm install [-y] <files...> | installs the following files. -y skips the confirmation prompt")
|
||||
fmt.Println("bpm remove [-y] <packages...> | removes the following packages. -y skips the confirmation prompt")
|
||||
fmt.Println("bpm cleanup | removes all unneeded dependencies")
|
||||
fmt.Println("----------------")
|
||||
}
|
||||
}
|
||||
|
||||
func resolveFlags() ([]string, int) {
|
||||
flags := getArgs()[1:]
|
||||
var ret []string
|
||||
for _, flag := range flags {
|
||||
if strings.HasPrefix(flag, "-") {
|
||||
f := strings.TrimPrefix(flag, "-")
|
||||
switch getCommandType() {
|
||||
default:
|
||||
log.Fatalf("Invalid flag " + flag)
|
||||
case list:
|
||||
v := [...]string{"e"}
|
||||
if !slices.Contains(v[:], f) {
|
||||
log.Fatalf("Invalid flag " + flag)
|
||||
}
|
||||
ret = append(ret, f)
|
||||
case install:
|
||||
v := [...]string{"y"}
|
||||
if !slices.Contains(v[:], f) {
|
||||
log.Fatalf("Invalid flag " + flag)
|
||||
}
|
||||
ret = append(ret, f)
|
||||
case remove:
|
||||
v := [...]string{"y"}
|
||||
if !slices.Contains(v[:], f) {
|
||||
log.Fatalf("Invalid flag " + flag)
|
||||
}
|
||||
ret = append(ret, f)
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return ret, len(ret)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user