First Commit. Search Working.

This commit is contained in:
Jguer 2016-09-05 03:17:51 +01:00
commit a11be21387
4 changed files with 250 additions and 0 deletions

27
.gitignore vendored Normal file
View file

@ -0,0 +1,27 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

83
src/aur.go Normal file
View file

@ -0,0 +1,83 @@
package main
import (
"encoding/json"
"fmt"
c "github.com/fatih/color"
"net/http"
"sort"
)
// AurResult describes an AUR package
type AurResult struct {
Description string `json:"Description"`
FirstSubmitted int `json:"FirstSubmitted"`
ID int `json:"ID"`
LastModified int `json:"LastModified"`
Maintainer string `json:"Maintainer"`
Name string `json:"Name"`
NumVotes int `json:"NumVotes"`
OutOfDate interface{} `json:"OutOfDate"`
PackageBase string `json:"PackageBase"`
PackageBaseID int `json:"PackageBaseID"`
Popularity int `json:"Popularity"`
URL string `json:"URL"`
URLPath string `json:"URLPath"`
Version string `json:"Version"`
}
// 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)
}
// 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 (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 searchAurPackages(pkg string) (search AurSearch) {
getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkg, &search)
sort.Sort(search)
return search
}
func (r AurSearch) printSearch(index int) (err error) {
yellow := c.New(c.FgYellow).SprintFunc()
green := c.New(c.FgGreen).SprintFunc()
for i, result := range r.Results {
if index != SearchMode {
fmt.Printf("%d aur/%s %s (%d)\n %s\n", i+index, yellow(result.Name), green(result.Version), result.NumVotes, result.Description)
} else {
fmt.Printf("aur/%s %s (%d)\n %s\n", yellow(result.Name), green(result.Version), result.NumVotes, result.Description)
}
}
return
}
func (a AurResult) installResult() error {
return nil
}

63
src/main.go Normal file
View file

@ -0,0 +1,63 @@
package main
import (
"bufio"
"flag"
"fmt"
c "github.com/fatih/color"
"os"
"strconv"
"strings"
)
// PacmanBin describes the default installation point of pacman
const PacmanBin string = "/usr/bin/pacman"
// SearchMode is search without numbers
const SearchMode int = -1
func getNums() (numbers []int, err error) {
var numberString string
green := c.New(c.FgGreen).SprintFunc()
fmt.Printf("%s\nNumbers:", green("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) {
aurRes := searchAurPackages(pkg)
repoRes, err := SearchPackages(pkg)
repoRes.printSearch(0)
err = aurRes.printSearch(repoRes.Resultcount)
nums, err := getNums()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(nums)
}
func main() {
flag.Parse()
searchTerm := flag.Args()
defaultMode(searchTerm[0])
}

77
src/repo.go Normal file
View file

@ -0,0 +1,77 @@
package main
import (
"fmt"
c "github.com/fatih/color"
"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
}
func getInstalledPackage(pkg string) (err error) {
cmd := exec.Command(PacmanBin, "-Qi", pkg)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
return
}
// SearchPackages handles repo searches
func SearchPackages(pkg string) (search RepoSearch, err error) {
cmd := exec.Command(PacmanBin, "-Ss", pkg)
cmdOutput, _ := cmd.Output()
outputSlice := strings.Split(string(cmdOutput), "\n")
if outputSlice[0] == "" {
return
}
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 (s RepoSearch) printSearch(index int) (err error) {
yellow := c.New(c.FgYellow).SprintFunc()
green := c.New(c.FgGreen).SprintFunc()
for i, result := range s.Results {
if index != SearchMode {
fmt.Printf("%d %s/%s %s\n%s\n", i, result.Repository, yellow(result.Name), green(result.Version), result.Description)
} else {
fmt.Printf("%s/%s %s\n%s\n", result.Repository, yellow(result.Name), green(result.Version), result.Description)
}
}
return nil
}