repo.go 1.7 KB

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