config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // YayConf holds the current config values for yay.
  21. var config *settings.Configuration
  22. // Editor returns the preferred system editor.
  23. func editor() (editor string, args []string) {
  24. switch {
  25. case config.Editor != "":
  26. editor, err := exec.LookPath(config.Editor)
  27. if err != nil {
  28. fmt.Fprintln(os.Stderr, err)
  29. } else {
  30. return editor, strings.Fields(config.EditorFlags)
  31. }
  32. fallthrough
  33. case os.Getenv("EDITOR") != "":
  34. if editorArgs := strings.Fields(os.Getenv("EDITOR")); len(editorArgs) != 0 {
  35. editor, err := exec.LookPath(editorArgs[0])
  36. if err != nil {
  37. fmt.Fprintln(os.Stderr, err)
  38. } else {
  39. return editor, editorArgs[1:]
  40. }
  41. }
  42. fallthrough
  43. case os.Getenv("VISUAL") != "":
  44. if editorArgs := strings.Fields(os.Getenv("VISUAL")); len(editorArgs) != 0 {
  45. editor, err := exec.LookPath(editorArgs[0])
  46. if err != nil {
  47. fmt.Fprintln(os.Stderr, err)
  48. } else {
  49. return editor, editorArgs[1:]
  50. }
  51. }
  52. fallthrough
  53. default:
  54. fmt.Fprintln(os.Stderr)
  55. text.Errorln(gotext.Get("%s is not set", text.Bold(text.Cyan("$EDITOR"))))
  56. text.Warnln(gotext.Get("Add %s or %s to your environment variables", text.Bold(text.Cyan("$EDITOR")), text.Bold(text.Cyan("$VISUAL"))))
  57. for {
  58. text.Infoln(gotext.Get("Edit PKGBUILD with?"))
  59. editorInput, err := getInput("")
  60. if err != nil {
  61. fmt.Fprintln(os.Stderr, err)
  62. continue
  63. }
  64. editorArgs := strings.Fields(editorInput)
  65. if len(editorArgs) == 0 {
  66. continue
  67. }
  68. editor, err := exec.LookPath(editorArgs[0])
  69. if err != nil {
  70. fmt.Fprintln(os.Stderr, err)
  71. continue
  72. }
  73. return editor, editorArgs[1:]
  74. }
  75. }
  76. }
  77. func getInput(defaultValue string) (string, error) {
  78. text.Info()
  79. if defaultValue != "" || settings.NoConfirm {
  80. fmt.Println(defaultValue)
  81. return defaultValue, nil
  82. }
  83. reader := bufio.NewReader(os.Stdin)
  84. buf, overflow, err := reader.ReadLine()
  85. if err != nil {
  86. return "", err
  87. }
  88. if overflow {
  89. return "", fmt.Errorf(gotext.Get("input too long"))
  90. }
  91. return string(buf), nil
  92. }