فهرست منبع

Removed pacargo, search and install working

Jguer 8 سال پیش
والد
کامیت
a7c567de97
5فایلهای تغییر یافته به همراه158 افزوده شده و 101 حذف شده
  1. 14 15
      actions.go
  2. 20 15
      aur/aur.go
  3. 0 24
      pacargo/pacargo.go
  4. 58 38
      pacman.go
  5. 66 9
      yay.go

+ 14 - 15
actions.go

@@ -11,21 +11,21 @@ import (
 	"strings"
 )
 
-func searchAndInstall(pkgName string, conf alpm.PacmanConfig, flags ...string) (err error) {
+func searchAndInstall(pkgName string, conf alpm.PacmanConfig, flags string) (err error) {
 	var num int
 	var numberString string
 
-	aurRes, err := aur.Search(pkgName, true)
-	repoRes, err := SearchPackages(pkgName, conf)
+	a, err := aur.Search(pkgName, true)
+	r, err := SearchPackages(pkgName, conf)
 	if err != nil {
 		return
 	}
 
-	if repoRes.Resultcount == 0 && aurRes.Resultcount == 0 {
+	if len(r.Results) == 0 && a.Resultcount == 0 {
 		return errors.New("No Packages match search")
 	}
-	repoRes.printSearch(0)
-	aurRes.PrintSearch(repoRes.Resultcount)
+	r.PrintSearch(0, conf)
+	a.PrintSearch(len(r.Results))
 
 	fmt.Printf("\x1B[32m%s\033[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
 	reader := bufio.NewReader(os.Stdin)
@@ -43,18 +43,17 @@ func searchAndInstall(pkgName string, conf alpm.PacmanConfig, flags ...string) (
 			fmt.Println(err)
 			continue
 		}
-		fmt.Println(num)
 
 		// Install package
-		if num > repoRes.Resultcount-1 {
-			index = num - repoRes.Resultcount
-			err = aurRes.Results[num-index].Install(BuildDir, conf, flags...)
+		if num > len(r.Results)-1 {
+			index = num - len(r.Results)
+			err = a.Results[num-index].Install(BuildDir, conf, flags)
 			if err != nil {
 				// Do not abandon program, we might still be able to install the rest
 				fmt.Println(err)
 			}
 		} else {
-			InstallPackage(repoRes.Results[num].Name, conf, flags...)
+			InstallPackage(r.Results[num].Name, conf, flags)
 		}
 	}
 
@@ -62,17 +61,17 @@ func searchAndInstall(pkgName string, conf alpm.PacmanConfig, flags ...string) (
 }
 
 func searchMode(pkg string, conf alpm.PacmanConfig) (err error) {
-	_, err = aur.Search(pkg, true)
+	a, err := aur.Search(pkg, true)
 	if err != nil {
 		return err
 	}
-	repo, err := SearchPackages(pkg, conf)
+	r, err := SearchPackages(pkg, conf)
 	if err != nil {
 		return err
 	}
 
-	aur.printSearch(SearchMode)
-	repo.printSearch(SearchMode)
+	r.PrintSearch(SearchMode, conf)
+	a.PrintSearch(SearchMode)
 
 	return nil
 }

+ 20 - 15
aur/aur.go

@@ -14,16 +14,19 @@ import (
 )
 
 // TarBin describes the default installation point of tar command
-// Probably will replace untar with code solution
+// Probably will replace untar with code solution.
 const TarBin string = "/usr/bin/tar"
 
-// BaseURL givers the AUR default address
+// BaseURL givers the AUR default address.
 const BaseURL string = "https://aur.archlinux.org"
 
-// MakepkgBin describes the default installation point of makepkg command
+// MakepkgBin describes the default installation point of makepkg command.
 const MakepkgBin string = "/usr/bin/makepkg"
 
-// Result describes an AUR package
+// SearchMode is search without numbers.
+const SearchMode int = -1
+
+// Result describes an AUR package.
 type Result struct {
 	ID             int         `json:"ID"`
 	Name           string      `json:"Name"`
@@ -77,14 +80,14 @@ func (r Query) Swap(i, j int) {
 }
 
 // PrintSearch handles printing search results in a given format
-func (r Query) PrintSearch(searchFormat bool) {
+func (r *Query) PrintSearch(start int) {
 	for i, result := range r.Results {
-		if searchFormat {
+		if start == -1 {
 			fmt.Printf("\033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
 				"aur", result.Name, result.Version, result.Description)
 		} else {
 			fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
-
+				start+i, "aur", result.Name, result.Version, result.Description)
 		}
 	}
 }
@@ -140,7 +143,7 @@ func Info(pkg string) (r Query, err error) {
 }
 
 // Install sends system commands to make and install a package from pkgName
-func Install(pkg string, baseDir string, conf alpm.PacmanConfig, flags ...string) (err error) {
+func Install(pkg string, baseDir string, conf alpm.PacmanConfig, flags string) (err error) {
 	info, err := Info(pkg)
 	if err != nil {
 		return
@@ -150,12 +153,12 @@ func Install(pkg string, baseDir string, conf alpm.PacmanConfig, flags ...string
 		return errors.New("Package '" + pkg + "' does not exist")
 	}
 
-	info.Results[0].Install(baseDir, conf, flags...)
+	info.Results[0].Install(baseDir, conf, flags)
 	return err
 }
 
 // Install handles install from Result
-func (a Result) Install(baseDir string, conf alpm.PacmanConfig, flags ...string) (err error) {
+func (a *Result) Install(baseDir string, conf alpm.PacmanConfig, flags string) (err error) {
 	// No need to use filepath.separators because it won't run on inferior platforms
 	err = os.MkdirAll(baseDir+"builds", 0755)
 	if err != nil {
@@ -196,11 +199,13 @@ func (a Result) Install(baseDir string, conf alpm.PacmanConfig, flags ...string)
 	if err != nil {
 		return
 	}
-	var args string
-	if len(flags) != 0 {
-		args = fmt.Sprintf(" %s", strings.Join(flags, " "))
+
+	var makepkgcmd *exec.Cmd
+	if flags == "" {
+		makepkgcmd = exec.Command(MakepkgBin, "-sri")
+	} else {
+		makepkgcmd = exec.Command(MakepkgBin, "-sri", flags)
 	}
-	makepkgcmd := exec.Command(MakepkgBin, "-sri"+args)
 	makepkgcmd.Stdout = os.Stdout
 	makepkgcmd.Stderr = os.Stderr
 	makepkgcmd.Stdin = os.Stdin
@@ -210,7 +215,7 @@ func (a Result) Install(baseDir string, conf alpm.PacmanConfig, flags ...string)
 }
 
 // Dependencies returns package dependencies splitting between AUR results and Repo Results not installed
-func (a Result) Dependencies(conf alpm.PacmanConfig) (final []string, err error) {
+func (a *Result) Dependencies(conf alpm.PacmanConfig) (final []string, err error) {
 	f := func(c rune) bool {
 		return c == '>' || c == '<' || c == '=' || c == ' '
 	}

+ 0 - 24
pacargo/pacargo.go

@@ -1,24 +0,0 @@
-package pacargo
-
-import (
-	"fmt"
-	"os"
-)
-
-type operation struct {
-	key         byte
-	description string
-	modifiers   []modifier
-    execute func()
-}
-
-type modifier struct {
-	description string
-}
-
-// ReturnArgs prints os args
-func ReturnArgs() {
-	for _, o := range os.Args {
-		fmt.Println(o)
-	}
-}

+ 58 - 38
pacman.go

@@ -9,83 +9,103 @@ import (
 	"strings"
 )
 
-// RepoResult describes a Repository package
-type RepoResult struct {
-	Description string
+// RepoSearch describes a Repository search.
+type RepoSearch struct {
+	Results []Result
+}
+
+// Result describes a pkg.
+type Result struct {
+	Name        string
 	Repository  string
 	Version     string
-	Name        string
+	Description string
+	Installed   bool
 }
 
-// RepoSearch describes a Repository search
-type RepoSearch struct {
-	Resultcount int
-	Results     []RepoResult
+func readConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
+	file, err := os.Open(pacmanconf)
+	if err != nil {
+		return
+	}
+	conf, err = alpm.ParseConfig(file)
+	if err != nil {
+		return
+	}
+	return
 }
 
 // InstallPackage handles package install
-func InstallPackage(pkg string, conf alpm.PacmanConfig, flags ...string) (err error) {
+func InstallPackage(pkg string, conf alpm.PacmanConfig, flags string) (err error) {
 	if found, err := aur.IspkgInRepo(pkg, conf); found {
 		if err != nil {
 			return err
 		}
-		var args string
-		if len(flags) != 0 {
-			args = fmt.Sprintf(" %s", strings.Join(flags, " "))
+
+		var cmd *exec.Cmd
+		if flags == "" {
+			cmd = exec.Command("sudo", "pacman", "-S", pkg)
+		} else {
+			cmd = exec.Command("sudo", "pacman", "-S", pkg, flags)
 		}
-		cmd := exec.Command("sudo", "pacman", "-S", pkg+args)
 		cmd.Stdout = os.Stdout
 		cmd.Stdin = os.Stdin
 		cmd.Stderr = os.Stderr
 		err = cmd.Run()
 	} else {
-		err = aur.Install(os.Args[2], BuildDir, conf, os.Args[3:]...)
+		err = aur.Install(pkg, BuildDir, conf, flags)
 	}
 
 	return nil
 }
 
-func readConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
-	file, err := os.Open(pacmanconf)
-	if err != nil {
-		return
-	}
-	conf, err = alpm.ParseConfig(file)
-	if err != nil {
-		return
-	}
-	return
-}
-
-// SearchPackages handles repo searches
-func SearchPackages(pkgName string, conf alpm.PacmanConfig) (search RepoSearch, err error) {
+// SearchPackages handles repo searches. Creates a RepoSearch struct.
+func SearchPackages(pkgName string, conf alpm.PacmanConfig) (s RepoSearch, err error) {
 	h, err := conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 	}
 
-	dbList, _ := h.SyncDbs()
+	dbList, err := h.SyncDbs()
+	localdb, err := h.LocalDb()
 
+	var installed bool
 	for _, db := range dbList.Slice() {
 		for _, pkg := range db.PkgCache().Slice() {
 			if strings.Contains(pkg.Name(), pkgName) {
-				fmt.Println(pkg.Name())
+				if r, _ := localdb.PkgByName(pkg.Name()); r != nil {
+					installed = true
+				} else {
+					installed = false
+				}
+
+				s.Results = append(s.Results, Result{
+					Name:        pkg.Name(),
+					Description: pkg.Description(),
+					Version:     pkg.Version(),
+					Repository:  db.Name(),
+					Installed:   installed,
+				})
 			}
 		}
 	}
 	return
 }
 
-func (s RepoSearch) printSearch(index int) (err error) {
-	for i, result := range s.Results {
-		if index != SearchMode {
-			fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
-				i, result.Repository, result.Name, result.Version, result.Description)
+//PrintSearch receives a RepoSearch type and outputs pretty text.
+func (s *RepoSearch) PrintSearch(mode int, conf alpm.PacmanConfig) {
+	for i, pkg := range s.Results {
+		if mode != SearchMode {
+            if pkg.Installed == true {
+			fmt.Printf("%d \033[1m%s/\x1B[33m%s\x1B[36m%s\033[0m\n%s\n",
+				i, pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
+            } else {
+			fmt.Printf("%d \033[1m%s/\x1B[33m%s (Installed)\x1B[36m%s\033[0m\n%s\n",
+				i, pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
+            }
 		} else {
 			fmt.Printf("\033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
-				result.Repository, result.Name, result.Version, result.Description)
+				pkg.Repository, pkg.Name, pkg.Version, pkg.Description)
 		}
 	}
-
-	return nil
 }

+ 66 - 9
yay.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"errors"
 	"fmt"
 	"os"
 	"strings"
@@ -12,23 +13,79 @@ const PacmanBin string = "/usr/bin/pacman"
 // PacmanConf describes the default pacman config file
 const PacmanConf string = "/etc/pacman.conf"
 
-// SearchMode is search without numbers
-const SearchMode bool = true
-
 // BuildDir is the root for package building
 const BuildDir string = "/tmp/yaytmp/"
 
+// SearchMode is search without numbers.
+const SearchMode int = -1
+
+func operation() (operation string, err error) {
+	if len(os.Args) < 2 {
+		return "noop", errors.New("No operation specified.")
+	}
+	for _, arg := range os.Args[1:] {
+		if arg[0] == '-' && arg[1] != '-' {
+			return arg, nil
+		}
+	}
+	return "yogurt", nil
+}
+
+func packages() (packages string, err error) {
+	var ps []string
+	for _, arg := range os.Args[1:] {
+		if arg[0] != '-' {
+			ps = append(ps, arg)
+		}
+	}
+
+	if len(ps) == 0 {
+		return "", nil
+	}
+	packages = strings.Join(ps, " ")
+
+	return
+}
+
+func flags() (flags string, err error) {
+	var fs []string
+	for _, arg := range os.Args[1:] {
+		if arg[0] == '-' && arg[1] == '-' {
+			fs = append(fs, arg)
+		}
+	}
+
+	if len(fs) == 0 {
+		return "", nil
+	}
+
+	flags = strings.Join(fs, " ")
+	return
+}
+
 func main() {
 	var err error
 	conf, err := readConfig(PacmanConf)
 
-	if os.Args[1] == "-Ss" {
-		err = searchMode(strings.Join(os.Args[2:], " "), conf)
+	op, err := operation()
+	if err != nil {
+		fmt.Println(err)
+		os.Exit(1)
+	}
+
+	pkg, _ := packages()
+
+	flag, _ := flags()
 
-	} else if os.Args[1] == "-S" {
-		err = InstallPackage(os.Args[2], conf, os.Args[3:]...)
-	} else {
-		err = searchAndInstall(os.Args[1], conf, os.Args[3:]...)
+	switch op {
+	case "-Ss":
+		err = searchMode(pkg, conf)
+	case "-S":
+		err = InstallPackage(pkg, conf, flag)
+	case "yogurt":
+		err = searchAndInstall(pkg, conf, flag)
+	default:
+		fmt.Println("Pass to pacman")
 	}
 
 	if err != nil {