Files
qulay/main.go
T

171 lines
4.3 KiB
Go
Raw Normal View History

2026-03-25 14:41:05 +03:00
package main
import (
"fmt"
"os"
"path/filepath"
"qulay/database"
"qulay/config"
"qulay/package"
"qulay/helpers"
"slices"
"strings"
)
var _name string = filepath.Base(os.Args[0])
var _cmds = []string{"h", "+", "-", "g", "s", "p", "u", "r", "q", "v"}
var _root_cmds = []string{"+", "-", "u", "i"}
2026-05-04 14:07:50 +03:00
const _version string = "0.6"
2026-03-25 14:41:05 +03:00
const _help string = `{_name}: command-line interface to the Qulay Package Manager
Usage:
{_name} h -> help menu
{_name} + pkg -> download and install package
{_name} - pkg -> remove package
{_name} g pkg -> get package version
{_name} s pkg -> search packages by name
{_name} p repo -> purge local repo directory
{_name} u -> update all repositories
{_name} r -> get all repositories URLs
{_name} q -> get all installed packages
{_name} v -> get {_name} version
Options:
:v -> verbose output
:a -> ask before doing
:nl -> disable recording in databases and logs
:nd -> do not download deps
:r -> reinstall already installed deps
Vars:
DESTDIR -> path where the package will be installed/removed`
func parseArgs(args []string) ([]string, []string, []string) {
var cmds, oths, opts []string
for _, arg := range args {
if strings.HasPrefix(arg, ":") {
opts = append(opts, arg)
} else if slices.Contains(_cmds, arg) && len(cmds) < 1 {
cmds = append(cmds, arg)
} else {
oths = append(oths, arg)
}
}
return cmds, oths, opts
}
func printHelp() {
r := strings.NewReplacer("{_name}", _name)
fmt.Println(r.Replace(_help))
}
func main() {
var args []string = os.Args[1:]
cmds, oths, opts := parseArgs(args)
destDir := os.Getenv("DESTDIR")
if destDir == "" {
destDir = "/"
}
if len(args) == 0 || len(cmds) == 0 {
printHelp()
os.Exit(1)
}
if os.Getuid() != 0 && slices.Contains(_root_cmds, cmds[0]) {
fmt.Println("!! this command is root-only. run this with root privileges")
os.Exit(1)
}
// fmt.Println(cmds, oths, opts)
if cmds[0] == "h" {
printHelp()
} else if cmds[0] == "+" {
if len(oths) < 1 {
fmt.Println("!! no package(s) received")
os.Exit(1)
}
for _, n := range oths {
err := pkg.Install(n, opts, destDir)
if err != nil {
fmt.Println("!!", err)
os.Exit(1)
}
}
} else if cmds[0] == "-" {
if len(oths) < 1 {
fmt.Println("!! no package(s) received")
os.Exit(1)
}
for _, n := range oths {
err := pkg.Remove(n, opts, destDir)
if err != nil {
fmt.Println("!!", err)
os.Exit(1)
}
}
} else if cmds[0] == "u" {
2026-05-04 14:07:50 +03:00
database.DownloadRepos(database.GetReposUrls(destDir), destDir)
2026-03-25 14:41:05 +03:00
} else if cmds[0] == "g" {
if len(oths) < 1 {
fmt.Println("!! no package(s) received")
os.Exit(1)
}
for _, name := range oths {
fmt.Println(database.GetPackageVersion(name, destDir))
}
} else if cmds[0] == "r" {
2026-05-04 14:07:50 +03:00
for _, url := range database.GetReposUrls(destDir) {
2026-03-25 14:41:05 +03:00
fmt.Println(url)
}
} else if cmds[0] == "q" {
for name, version := range database.ReadInstalled(destDir) {
fmt.Println(name, "-", version[0])
}
} else if cmds[0] == "s" {
if len(oths) < 1 {
pkgs := pkg.GetPackages(destDir)
2026-05-04 14:07:50 +03:00
fmt.Println(pkgs)
2026-03-25 14:41:05 +03:00
for _, p := range pkgs {
if database.IsInstalled(p.Repo + "/" + p.Name, destDir) {
fmt.Printf("[+] %s/%s - %v - %v\n", p.Repo, p.Name, p.Data[0], p.Data[1])
} else {
fmt.Printf(" %s/%s - %v - %v\n", p.Repo, p.Name, p.Data[0], p.Data[1])
}
}
} else {
for _, n := range oths {
pkgs := pkg.FindPackages(n, destDir)
for _, p := range pkgs {
if database.IsInstalled(p.Repo + "/" + p.Name, destDir) {
fmt.Printf("[+] %s/%s - %v - %v\n", p.Repo, p.Name, p.Data[0], p.Data[1])
} else {
fmt.Printf(" %s/%s - %v - %v\n", p.Repo, p.Name, p.Data[0], p.Data[1])
}
}
}
}
} else if cmds[0] == "p" {
if slices.Contains(opts, ":a") {
if !helpers.Ask("do you want purge repo(s)?") {
os.Exit(1)
}
}
if len(oths) >= 1 {
for _, n := range oths {
err := database.PurgeRepository(n, destDir)
if err != nil {
fmt.Println("!!", err)
os.Exit(1)
} else {
fmt.Println(":: deleted", n, "repository in", paths.ReposDir)
}
}
}
} else if cmds[0] == "v" {
fmt.Println(_version)
} else {
fmt.Println("w! unknown command:", cmds[0])
printHelp()
os.Exit(1)
}
}