config.go 2.4 KB

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