repo.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. )
  8. // RepoResult describes a Repository package
  9. type RepoResult struct {
  10. Description string
  11. Repository string
  12. Version string
  13. Name string
  14. }
  15. // RepoSearch describes a Repository search
  16. type RepoSearch struct {
  17. Resultcount int
  18. Results []RepoResult
  19. }
  20. func getInstalledPackage(pkg string) (err error) {
  21. cmd := exec.Command(PacmanBin, "-Qi", pkg)
  22. cmd.Stdout = os.Stdout
  23. cmd.Stderr = os.Stderr
  24. err = cmd.Run()
  25. return
  26. }
  27. // SearchPackages handles repo searches
  28. func SearchPackages(pkg string) (search RepoSearch, err error) {
  29. cmdOutput, err := exec.Command(PacmanBin, "-Ss", pkg).Output()
  30. outputSlice := strings.Split(string(cmdOutput), "\n")
  31. if outputSlice[0] == "" {
  32. return search, nil
  33. }
  34. i := true
  35. var tempStr string
  36. var rRes *RepoResult
  37. for _, pkgStr := range outputSlice {
  38. if i {
  39. rRes = new(RepoResult)
  40. fmt.Sscanf(pkgStr, "%s %s\n", &tempStr, &rRes.Version)
  41. repoNameSlc := strings.Split(tempStr, "/")
  42. rRes.Repository = repoNameSlc[0]
  43. rRes.Name = repoNameSlc[1]
  44. i = false
  45. } else {
  46. rRes.Description = pkgStr
  47. search.Resultcount++
  48. search.Results = append(search.Results, *rRes)
  49. i = true
  50. }
  51. }
  52. return
  53. }
  54. func isInRepo(pkg string) bool {
  55. if _, err := exec.Command(PacmanBin, "-Sp", pkg).Output(); err != nil {
  56. return false
  57. }
  58. return true
  59. }
  60. func (s RepoSearch) printSearch(index int) (err error) {
  61. for i, result := range s.Results {
  62. if index != SearchMode {
  63. fmt.Printf("%d \033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
  64. i, result.Repository, result.Name, result.Version, result.Description)
  65. } else {
  66. fmt.Printf("\033[1m%s/\x1B[33m%s \x1B[36m%s\033[0m\n%s\n",
  67. result.Repository, result.Name, result.Version, result.Description)
  68. }
  69. }
  70. return nil
  71. }