config.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "github.com/leonelquinteros/gotext"
  9. "github.com/Jguer/yay/v10/pkg/settings"
  10. "github.com/Jguer/yay/v10/pkg/text"
  11. )
  12. // Verbosity settings for search
  13. const (
  14. numberMenu = iota
  15. detailed
  16. minimal
  17. )
  18. var yayVersion = "10.0.0"
  19. var localePath = "/usr/share/locale"
  20. // savedInfo holds the current vcs info
  21. var savedInfo vcsInfo
  22. // YayConf holds the current config values for yay.
  23. var config *settings.Configuration
  24. // Editor returns the preferred system editor.
  25. func editor() (editor string, args []string) {
  26. switch {
  27. case config.Editor != "":
  28. editor, err := exec.LookPath(config.Editor)
  29. if err != nil {
  30. fmt.Fprintln(os.Stderr, err)
  31. } else {
  32. return editor, strings.Fields(config.EditorFlags)
  33. }
  34. fallthrough
  35. case os.Getenv("EDITOR") != "":
  36. if editorArgs := strings.Fields(os.Getenv("EDITOR")); len(editorArgs) != 0 {
  37. editor, err := exec.LookPath(editorArgs[0])
  38. if err != nil {
  39. fmt.Fprintln(os.Stderr, err)
  40. } else {
  41. return editor, editorArgs[1:]
  42. }
  43. }
  44. fallthrough
  45. case os.Getenv("VISUAL") != "":
  46. if editorArgs := strings.Fields(os.Getenv("VISUAL")); len(editorArgs) != 0 {
  47. editor, err := exec.LookPath(editorArgs[0])
  48. if err != nil {
  49. fmt.Fprintln(os.Stderr, err)
  50. } else {
  51. return editor, editorArgs[1:]
  52. }
  53. }
  54. fallthrough
  55. default:
  56. fmt.Fprintln(os.Stderr)
  57. text.Errorln(gotext.Get("%s is not set", text.Bold(text.Cyan("$EDITOR"))))
  58. text.Warnln(gotext.Get("Add %s or %s to your environment variables", text.Bold(text.Cyan("$EDITOR")), text.Bold(text.Cyan("$VISUAL"))))
  59. for {
  60. text.Infoln(gotext.Get("Edit PKGBUILD with?"))
  61. editorInput, err := getInput("")
  62. if err != nil {
  63. fmt.Fprintln(os.Stderr, err)
  64. continue
  65. }
  66. editorArgs := strings.Fields(editorInput)
  67. if len(editorArgs) == 0 {
  68. continue
  69. }
  70. editor, err := exec.LookPath(editorArgs[0])
  71. if err != nil {
  72. fmt.Fprintln(os.Stderr, err)
  73. continue
  74. }
  75. return editor, editorArgs[1:]
  76. }
  77. }
  78. }
  79. func getInput(defaultValue string) (string, error) {
  80. text.Info()
  81. if defaultValue != "" || config.NoConfirm {
  82. fmt.Println(defaultValue)
  83. return defaultValue, nil
  84. }
  85. reader := bufio.NewReader(os.Stdin)
  86. buf, overflow, err := reader.ReadLine()
  87. if err != nil {
  88. return "", err
  89. }
  90. if overflow {
  91. return "", fmt.Errorf(gotext.Get("input too long"))
  92. }
  93. return string(buf), nil
  94. }