From 1c0f0486b4fa4ad93386ed5d21d99e085df061b9 Mon Sep 17 00:00:00 2001 From: Jguer Date: Tue, 13 Sep 2016 02:06:24 +0100 Subject: [PATCH] Major refactor, exclusive use of alpm --- actions.go | 78 +++++++++++++ aur/aur.go | 291 +++++++++++++++++++++++++++++++++++++++++++++++++ install.go | 118 -------------------- pacman.go | 91 ++++++++++++++++ search.go | 201 ---------------------------------- utilstructs.go | 116 -------------------- yay.go | 89 ++------------- 7 files changed, 467 insertions(+), 517 deletions(-) create mode 100644 actions.go create mode 100644 aur/aur.go delete mode 100644 install.go create mode 100644 pacman.go delete mode 100644 search.go delete mode 100644 utilstructs.go diff --git a/actions.go b/actions.go new file mode 100644 index 00000000..3d6d2b71 --- /dev/null +++ b/actions.go @@ -0,0 +1,78 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "github.com/Jguer/go-alpm" + "github.com/Jguer/yay/aur" + "os" + "strconv" + "strings" +) + +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) + if err != nil { + return + } + + if repoRes.Resultcount == 0 && aurRes.Resultcount == 0 { + return errors.New("No Packages match search") + } + repoRes.printSearch(0) + // err = aurRes.printSearch(repoRes.Resultcount) + + fmt.Printf("\x1B[32m%s\033[0m\nNumbers:", "Type numbers to install. Separate each number with a space.") + reader := bufio.NewReader(os.Stdin) + numberString, err = reader.ReadString('\n') + if err != nil { + fmt.Println(err) + return + } + + var index int + result := strings.Fields(numberString) + for _, numS := range result { + num, err = strconv.Atoi(numS) + if err != nil { + 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 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...) + } + } + + return +} + +func searchMode(pkg string, conf alpm.PacmanConfig) (err error) { + _, err = aur.Search(pkg, true) + if err != nil { + return err + } + repo, err := SearchPackages(pkg, conf) + if err != nil { + return err + } + + // aur.printSearch(SearchMode) + repo.printSearch(SearchMode) + + return nil +} diff --git a/aur/aur.go b/aur/aur.go new file mode 100644 index 00000000..9804df3c --- /dev/null +++ b/aur/aur.go @@ -0,0 +1,291 @@ +package aur + +import ( + "encoding/json" + "errors" + "fmt" + "github.com/Jguer/go-alpm" + "io" + "net/http" + "os" + "os/exec" + "sort" + "strings" +) + +// TarBin describes the default installation point of tar command +// Probably will replace untar with code solution +const TarBin string = "/usr/bin/tar" + +// BaseURL givers the AUR default address +const BaseURL string = "https://aur.archlinux.org" + +// MakepkgBin describes the default installation point of makepkg command +const MakepkgBin string = "/usr/bin/makepkg" + +// Result describes an AUR package +type Result struct { + ID int `json:"ID"` + Name string `json:"Name"` + PackageBaseID int `json:"PackageBaseID"` + PackageBase string `json:"PackageBase"` + Version string `json:"Version"` + Description string `json:"Description"` + URL string `json:"URL"` + NumVotes int `json:"NumVotes"` + Popularity float32 `json:"Popularity"` + OutOfDate interface{} `json:"OutOfDate"` + Maintainer string `json:"Maintainer"` + FirstSubmitted int `json:"FirstSubmitted"` + LastModified int `json:"LastModified"` + URLPath string `json:"URLPath"` + Depends []string `json:"Depends"` + MakeDepends []string `json:"MakeDepends"` + OptDepends []string `json:"OptDepends"` + Conflicts []string `json:"Conflicts"` + License []string `json:"License"` + Keywords []string `json:"Keywords"` +} + +// Query describes an AUR json Query +type Query struct { + Resultcount int `json:"resultcount"` + Results []Result `json:"results"` + Type string `json:"type"` + Version int `json:"version"` +} + +// Editor gives the default system editor, uses vi in last case +var Editor = "vi" + +func init() { + if os.Getenv("EDITOR") != "" { + Editor = os.Getenv("EDITOR") + } +} + +func (r Query) Len() int { + return len(r.Results) +} + +func (r Query) Less(i, j int) bool { + return r.Results[i].NumVotes > r.Results[j].NumVotes +} + +func (r Query) Swap(i, j int) { + r.Results[i], r.Results[j] = r.Results[j], r.Results[i] +} + +func downloadFile(filepath string, url string) (err error) { + // Create the file + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + + // Get the data + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + // Writer the body to file + _, err = io.Copy(out, resp.Body) + if err != nil { + return err + } + + return nil +} + +// getJSON handles JSON retrieval and decoding to struct +func getJSON(url string, target interface{}) error { + r, err := http.Get(url) + if err != nil { + return err + } + defer r.Body.Close() + + return json.NewDecoder(r.Body).Decode(target) +} + +// Search returns an AUR search +func Search(pkg string, sortS bool) (r Query, err error) { + err = getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkg, &r) + if sortS { + sort.Sort(r) + } + return +} + +// Info returns an AUR search with package details +func Info(pkg string) (r Query, err error) { + err = getJSON("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]="+pkg, &r) + return +} + +// 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) { + info, err := Info(pkg) + if err != nil { + return + } + + if info.Resultcount == 0 { + return errors.New("Package '" + pkg + "' does not exist") + } + + 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) { + // No need to use filepath.separators because it won't run on inferior platforms + err = os.MkdirAll(baseDir+"builds", 0755) + if err != nil { + fmt.Println(err) + return + } + + tarLocation := baseDir + a.Name + ".tar.gz" + defer os.Remove(baseDir + a.Name + ".tar.gz") + + err = downloadFile(tarLocation, BaseURL+a.URLPath) + if err != nil { + return + } + + err = exec.Command(TarBin, "-xf", tarLocation, "-C", baseDir).Run() + if err != nil { + return + } + defer os.RemoveAll(baseDir + a.Name) + _, err = a.Dependencies(conf) + if err != nil { + return + } + + fmt.Print("\033[1m\x1b[32m==> Edit PKGBUILD? (y/n)\033[0m") + var response string + fmt.Scanln(&response) + if strings.ContainsAny(response, "y & Y") { + editcmd := exec.Command(Editor, baseDir+a.Name+"/"+"PKGBUILD") + editcmd.Stdout = os.Stdout + editcmd.Stderr = os.Stderr + editcmd.Stdin = os.Stdin + err = editcmd.Run() + } + + err = os.Chdir(baseDir + a.Name) + if err != nil { + return + } + var args string + if len(flags) != 0 { + args = fmt.Sprintf(" %s", strings.Join(flags, " ")) + } + makepkgcmd := exec.Command(MakepkgBin, "-sri"+args) + makepkgcmd.Stdout = os.Stdout + makepkgcmd.Stderr = os.Stderr + makepkgcmd.Stdin = os.Stdin + err = makepkgcmd.Run() + + return +} + +// Dependencies returns package dependencies splitting between AUR results and Repo Results not installed +func (a Result) Dependencies(conf alpm.PacmanConfig) (final []string, err error) { + f := func(c rune) bool { + return c == '>' || c == '<' || c == '=' || c == ' ' + } + info, err := Info(a.Name) + if err != nil { + return + } + + if len(info.Results) == 0 { + return final, errors.New("Failed to get deps from RPC") + } + + var found bool + for _, deps := range info.Results[0].MakeDepends { + fields := strings.FieldsFunc(deps, f) + + if found, err = IspkgInstalled(fields[0]); found { + if err != nil { + return + } + continue + } + + if found, err = IspkgInRepo(fields[0], conf); !found { + if err != nil { + return + } + final = append(final, fields[0]) + } + } + + for _, deps := range info.Results[0].Depends { + fields := strings.FieldsFunc(deps, f) + + if found, err = IspkgInstalled(fields[0]); found { + if err != nil { + return + } + continue + } + + if found, err = IspkgInRepo(fields[0], conf); !found { + if err != nil { + return + } + final = append(final, fields[0]) + } + } + return +} + +// IspkgInstalled returns true if pkgName is installed +func IspkgInstalled(pkgName string) (bool, error) { + h, err := alpm.Init("/", "/var/lib/pacman") + defer h.Release() + if err != nil { + return false, err + } + + localDb, err := h.LocalDb() + if err != nil { + return false, err + } + + for _, pkg := range localDb.PkgCache().Slice() { + if pkg.Name() == pkgName { + return true, nil + } + } + + return false, nil +} + +// IspkgInRepo returns true if pkgName is in a synced repo +func IspkgInRepo(pkgName string, conf alpm.PacmanConfig) (bool, error) { + h, err := conf.CreateHandle() + defer h.Release() + if err != nil { + return false, err + } + + dbList, _ := h.SyncDbs() + for _, db := range dbList.Slice() { + for _, pkg := range db.PkgCache().Slice() { + if pkg.Name() == pkgName { + return true, nil + } + } + } + return false, nil +} diff --git a/install.go b/install.go deleted file mode 100644 index ec1301ec..00000000 --- a/install.go +++ /dev/null @@ -1,118 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "os" - "os/exec" - "strings" -) - -func installnumArray(num []int, aurRes AurSearch, repoRes RepoSearch, flags ...string) (err error) { - if len(num) == 0 { - return errors.New("Installing AUR array: No nums selected") - } - - var index int - for _, i := range num { - if i > repoRes.Resultcount-1 { - index = i - repoRes.Resultcount - err = aurRes.Results[i-index].install(flags...) - if err != nil { - // Do not abandon program, we might still be able to install the rest - fmt.Println(err) - } - } else { - InstallPackage(repoRes.Results[i].Name, flags...) - } - } - - return err -} - -func installAURPackage(pkg string, flags ...string) (err error) { - info, err := infoAurPackage(pkg) - if err != nil { - return - } - - if info.Resultcount == 0 { - return errors.New("Package '" + pkg + "' does not exist") - } - - info.Results[0].install(flags...) - return err -} - -func (a AurResult) install(flags ...string) (err error) { - // No need to use filepath.separators because it won't run on inferior platforms - err = os.MkdirAll(BuildDir+"builds", 0755) - if err != nil { - fmt.Println(err) - return - } - - tarLocation := BuildDir + a.Name + ".tar.gz" - defer os.Remove(BuildDir + a.Name + ".tar.gz") - - err = downloadFile(tarLocation, BaseURL+a.URLPath) - if err != nil { - return - } - - err = exec.Command(TarBin, "-xf", tarLocation, "-C", BuildDir).Run() - if err != nil { - return - } - defer os.RemoveAll(BuildDir + a.Name) - err = a.getAURDependencies() - if err != nil { - return - } - - fmt.Print("\033[1m\x1b[32m==> Edit PKGBUILD? (y/n)\033[0m") - var response string - fmt.Scanln(&response) - if strings.ContainsAny(response, "y & Y") { - editcmd := exec.Command(Editor, BuildDir+a.Name+"/"+"PKGBUILD") - editcmd.Stdout = os.Stdout - editcmd.Stderr = os.Stderr - editcmd.Stdin = os.Stdin - err = editcmd.Run() - } - - err = os.Chdir(BuildDir + a.Name) - if err != nil { - return - } - var args string - if len(flags) != 0 { - args = fmt.Sprintf(" %s", strings.Join(flags, " ")) - } - makepkgcmd := exec.Command(MakepkgBin, "-sri"+args) - makepkgcmd.Stdout = os.Stdout - makepkgcmd.Stderr = os.Stderr - makepkgcmd.Stdin = os.Stdin - err = makepkgcmd.Run() - - return -} - -// InstallPackage handles package install -func InstallPackage(pkg string, flags ...string) (err error) { - if isInRepo(pkg) { - var args string - if len(flags) != 0 { - args = fmt.Sprintf(" %s", strings.Join(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 = installAURPackage(os.Args[2], os.Args[3:]...) - } - - return nil -} diff --git a/pacman.go b/pacman.go new file mode 100644 index 00000000..70255705 --- /dev/null +++ b/pacman.go @@ -0,0 +1,91 @@ +package main + +import ( + "fmt" + "github.com/Jguer/go-alpm" + "github.com/Jguer/yay/aur" + "os" + "os/exec" + "strings" +) + +// RepoResult describes a Repository package +type RepoResult struct { + Description string + Repository string + Version string + Name string +} + +// RepoSearch describes a Repository search +type RepoSearch struct { + Resultcount int + Results []RepoResult +} + +// InstallPackage handles package install +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, " ")) + } + 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:]...) + } + + 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) { + h, err := conf.CreateHandle() + defer h.Release() + if err != nil { + } + + dbList, _ := h.SyncDbs() + + for _, db := range dbList.Slice() { + for _, pkg := range db.PkgCache().Slice() { + if strings.Contains(pkg.Name(), pkgName) { + fmt.Println(pkg.Name()) + } + } + } + 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) + } 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) + } + } + + return nil +} diff --git a/search.go b/search.go deleted file mode 100644 index 5d1cf447..00000000 --- a/search.go +++ /dev/null @@ -1,201 +0,0 @@ -package main - -import ( - "bufio" - "errors" - "fmt" - "github.com/Jguer/go-alpm" - "os" - "os/exec" - "sort" - "strings" -) - -func searchAurPackages(pkg string, index int) (search AurSearch, err error) { - err = getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkg, &search) - if index != SearchMode { - sort.Sort(search) - } - return -} - -// SearchPackages handles repo searches -func SearchPackages(pkg string) (search RepoSearch, err error) { - h, er := alpm.Init("/", "/var/lib/pacman") - if er != nil { - fmt.Println(er) - return - } - defer h.Release() - - fmt.Println("before dblist") - dbList, _ := h.SyncDbs() - fmt.Println("after dblist") - // db, _ := h.RegisterSyncDb("core", 0) - // h.RegisterSyncDb("community", 0) - // h.RegisterSyncDb("extra", 0) - - _, err = h.SyncDbByName("core") - fmt.Println(err) - fmt.Printf("%+v\n", dbList) - - db, _ := h.LocalDb() - for _, pkg := range db.PkgCache().Slice() { - fmt.Printf("%s %s\n %s\n", - pkg.Name(), pkg.Version(), pkg.Description()) - } - - for _, db := range dbList.Slice() { - fmt.Printf("%+v\n", db) - db, _ := h.LocalDb() - for _, pkg := range db.PkgCache().Slice() { - fmt.Printf("%s %s\n %s\n", - pkg.Name(), pkg.Version(), pkg.Description()) - } - } - return -} - -// SearchPackagesa handles repo searches -func SearchPackagesa(pkg string) (search RepoSearch, err error) { - cmdOutput, err := exec.Command(PacmanBin, "-Ss", pkg).Output() - outputSlice := strings.Split(string(cmdOutput), "\n") - if outputSlice[0] == "" { - return search, nil - } - - i := true - var tempStr string - var rRes *RepoResult - for _, pkgStr := range outputSlice { - if i { - rRes = new(RepoResult) - fmt.Sscanf(pkgStr, "%s %s\n", &tempStr, &rRes.Version) - repoNameSlc := strings.Split(tempStr, "/") - rRes.Repository = repoNameSlc[0] - rRes.Name = repoNameSlc[1] - i = false - } else { - rRes.Description = pkgStr - search.Resultcount++ - search.Results = append(search.Results, *rRes) - i = true - } - } - return -} - -func infoAurPackage(pkg string) (info AurSearch, err error) { - err = getJSON("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]="+pkg, &info) - return -} - -func (r AurSearch) printSearch(index int) (err error) { - for i, result := range r.Results { - if index != SearchMode { - fmt.Printf("%d \033[1maur/\x1B[33m%s \x1B[36m%s\033[0m (%d)\n %s\n", - i+index, result.Name, result.Version, result.NumVotes, result.Description) - } else { - fmt.Printf("\033[1maur/\x1B[33m%s \x1B[36m%s\033[0m (%d)\n %s\n", - result.Name, result.Version, result.NumVotes, result.Description) - } - } - - 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) - } 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) - } - } - - return nil -} - -// To implement -func (a AurResult) getDepsfromFile(pkgbuildLoc string) (err error) { - var depend string - file, err := os.Open(pkgbuildLoc) - if err != nil { - return err - } - defer file.Close() - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - if strings.Contains(scanner.Text(), "optdepends=(") { - continue - } - if strings.Contains(scanner.Text(), "depends=(") { - depend = scanner.Text() - fields := strings.Fields(depend) - - for _, i := range fields { - fmt.Println(i) - } - break - } - } - - return nil -} - -func (a AurResult) getDepsFromRPC() (final []string, err error) { - f := func(c rune) bool { - return c == '>' || c == '<' || c == '=' || c == ' ' - } - info, err := infoAurPackage(a.Name) - if err != nil { - return - } - - if len(info.Results) == 0 { - return final, errors.New("Failed to get deps from RPC") - } - - for _, deps := range info.Results[0].MakeDepends { - fields := strings.FieldsFunc(deps, f) - if !isInRepo(fields[0]) { - final = append(final, fields[0]) - } - } - - for _, deps := range info.Results[0].Depends { - fields := strings.FieldsFunc(deps, f) - if !isInRepo(fields[0]) { - final = append(final, fields[0]) - } - } - - return -} - -func (a AurResult) getAURDependencies() (err error) { - pkglist, err := a.getDepsFromRPC() - fmt.Printf("%+v\n", pkglist) - - for _, i := range pkglist { - err = installAURPackage(i, "--asdeps") - if err != nil { - for _, e := range pkglist { - removePackage(e, "sdc") - } - return - } - } - return nil -} - -func getInstalledPackage(pkg string) (err error) { - cmd := exec.Command(PacmanBin, "-Qi", pkg) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - return -} diff --git a/utilstructs.go b/utilstructs.go deleted file mode 100644 index f1505283..00000000 --- a/utilstructs.go +++ /dev/null @@ -1,116 +0,0 @@ -package main - -import ( - "encoding/json" - "io" - "net/http" - "os" - "os/exec" -) - -// RepoResult describes a Repository package -type RepoResult struct { - Description string - Repository string - Version string - Name string -} - -// RepoSearch describes a Repository search -type RepoSearch struct { - Resultcount int - Results []RepoResult -} - -// AurResult describes an AUR package -type AurResult struct { - ID int `json:"ID"` - Name string `json:"Name"` - PackageBaseID int `json:"PackageBaseID"` - PackageBase string `json:"PackageBase"` - Version string `json:"Version"` - Description string `json:"Description"` - URL string `json:"URL"` - NumVotes int `json:"NumVotes"` - Popularity float32 `json:"Popularity"` - OutOfDate interface{} `json:"OutOfDate"` - Maintainer string `json:"Maintainer"` - FirstSubmitted int `json:"FirstSubmitted"` - LastModified int `json:"LastModified"` - URLPath string `json:"URLPath"` - Depends []string `json:"Depends"` - MakeDepends []string `json:"MakeDepends"` - OptDepends []string `json:"OptDepends"` - Conflicts []string `json:"Conflicts"` - License []string `json:"License"` - Keywords []string `json:"Keywords"` -} - -// AurSearch describes an AUR search -type AurSearch struct { - Resultcount int `json:"resultcount"` - Results []AurResult `json:"results"` - Type string `json:"type"` - Version int `json:"version"` -} - -func downloadFile(filepath string, url string) (err error) { - // Create the file - out, err := os.Create(filepath) - if err != nil { - return err - } - defer out.Close() - - // Get the data - resp, err := http.Get(url) - if err != nil { - return err - } - defer resp.Body.Close() - - // Writer the body to file - _, err = io.Copy(out, resp.Body) - if err != nil { - return err - } - - return nil -} - -// getJSON handles JSON retrieval and decoding to struct -func getJSON(url string, target interface{}) error { - r, err := http.Get(url) - if err != nil { - return err - } - defer r.Body.Close() - - return json.NewDecoder(r.Body).Decode(target) -} - -func (r AurSearch) Len() int { - return len(r.Results) -} - -func (r AurSearch) Less(i, j int) bool { - return r.Results[i].NumVotes > r.Results[j].NumVotes -} - -func (r AurSearch) Swap(i, j int) { - r.Results[i], r.Results[j] = r.Results[j], r.Results[i] -} - -func isInRepo(pkg string) bool { - if _, err := exec.Command(PacmanBin, "-Sp", pkg).Output(); err != nil { - return false - } - return true -} - -func isInstalled(pkg string) bool { - if _, err := exec.Command(PacmanBin, "-Qq", pkg).Output(); err != nil { - return false - } - return true -} diff --git a/yay.go b/yay.go index 88803c1f..d8f0cfa0 100644 --- a/yay.go +++ b/yay.go @@ -1,23 +1,16 @@ package main import ( - "bufio" - "errors" "fmt" "os" - "strconv" "strings" ) // PacmanBin describes the default installation point of pacman const PacmanBin string = "/usr/bin/pacman" -// MakepkgBin describes the default installation point of makepkg command -const MakepkgBin string = "/usr/bin/makepkg" - -// TarBin describes the default installation point of tar command -// Probably will replace untar with code solution -const TarBin string = "/usr/bin/tar" +// PacmanConf describes the default pacman config file +const PacmanConf string = "/etc/pacman.conf" // SearchMode is search without numbers const SearchMode int = -1 @@ -25,85 +18,17 @@ const SearchMode int = -1 // BuildDir is the root for package building const BuildDir string = "/tmp/yaytmp/" -// BaseURL givers the AUR default address -const BaseURL string = "https://aur.archlinux.org" - -// Editor gives the default system editor, uses vi in last case -var Editor = "vi" - -func getNums() (numbers []int, err error) { - var numberString string - fmt.Printf("\x1B[32m%s\033[0m\nNumbers:", "Type numbers to install. Separate each number with a space.") - reader := bufio.NewReader(os.Stdin) - numberString, err = reader.ReadString('\n') - if err != nil { - fmt.Println(err) - return - } - - result := strings.Fields(numberString) - var num int - for _, numS := range result { - num, err = strconv.Atoi(numS) - if err != nil { - fmt.Println(err) - return - } - numbers = append(numbers, num) - } - - return -} - -func defaultMode(pkg string) (err error) { - aurRes, err := searchAurPackages(pkg, 0) - repoRes, err := SearchPackages(pkg) - if err != nil { - return - } - - if repoRes.Resultcount == 0 && aurRes.Resultcount == 0 { - return errors.New("No Packages match search") - } - repoRes.printSearch(0) - err = aurRes.printSearch(repoRes.Resultcount) - - nums, err := getNums() - if err != nil { - return - } - err = installnumArray(nums, aurRes, repoRes) - if err != nil { - return - } - return -} - -func searchMode(pkg string) (err error) { - aur, err := searchAurPackages(pkg, SearchMode) - repo, err := SearchPackages(pkg) - if err != nil { - return err - } - - aur.printSearch(SearchMode) - repo.printSearch(SearchMode) - - return nil -} - func main() { var err error - if os.Getenv("EDITOR") != "" { - Editor = os.Getenv("EDITOR") - } + conf, err := readConfig(PacmanConf) + if os.Args[1] == "-Ss" { - err = searchMode(strings.Join(os.Args[3:], " ")) + err = searchMode(strings.Join(os.Args[2:], " "), conf) } else if os.Args[1] == "-S" { - err = InstallPackage(os.Args[2], os.Args[3:]...) + err = InstallPackage(os.Args[2], conf, os.Args[3:]...) } else { - err = defaultMode(os.Args[1]) + err = searchAndInstall(os.Args[1], conf, os.Args[3:]...) } if err != nil {