config.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. alpm "github.com/jguer/go-alpm"
  11. )
  12. // Verbosity settings for search
  13. const (
  14. NumberMenu = iota
  15. Detailed
  16. Minimal
  17. )
  18. // Describes Sorting method for numberdisplay
  19. const (
  20. BottomUp = iota
  21. TopDown
  22. )
  23. // Configuration stores yay's config.
  24. type Configuration struct {
  25. BuildDir string `json:"buildDir"`
  26. Editor string `json:"editor"`
  27. EditorFlags string `json:"editorflags"`
  28. MakepkgBin string `json:"makepkgbin"`
  29. PacmanBin string `json:"pacmanbin"`
  30. PacmanConf string `json:"pacmanconf"`
  31. TarBin string `json:"tarbin"`
  32. ReDownload string `json:"redownload"`
  33. ReBuild string `json:"rebuild"`
  34. AnswerClean string `json:"answerclean"`
  35. AnswerEdit string `json:"answeredit"`
  36. AnswerUpgrade string `json:"answerupgrade"`
  37. GitBin string `json:"gitbin"`
  38. GpgBin string `json:"gpgbin"`
  39. GpgFlags string `json:"gpgflags"`
  40. MFlags string `json:"mflags"`
  41. SortBy string `json:"sortby"`
  42. RequestSplitN int `json:"requestsplitn"`
  43. SearchMode int `json:"-"`
  44. SortMode int `json:"sortmode"`
  45. SudoLoop bool `json:"sudoloop"`
  46. TimeUpdate bool `json:"timeupdate"`
  47. NoConfirm bool `json:"-"`
  48. Devel bool `json:"devel"`
  49. CleanAfter bool `json:"cleanAfter"`
  50. }
  51. var version = "3.373"
  52. // configFileName holds the name of the config file.
  53. const configFileName string = "config.json"
  54. // vcsFileName holds the name of the vcs file.
  55. const vcsFileName string = "vcs.json"
  56. // completionFilePrefix holds the prefix used for storing shell completion files.
  57. const completionFilePrefix string = "aur_"
  58. // baseURL givers the AUR default address.
  59. const baseURL string = "https://aur.archlinux.org"
  60. // useColor enables/disables colored printing
  61. var useColor bool
  62. // configHome handles config directory home
  63. var configHome string
  64. // cacheHome handles cache home
  65. var cacheHome string
  66. // savedInfo holds the current vcs info
  67. var savedInfo vcsInfo
  68. // configfile holds yay config file path.
  69. var configFile string
  70. // vcsfile holds yay vcs info file path.
  71. var vcsFile string
  72. // completion file
  73. var completionFile string
  74. // shouldSaveConfig holds whether or not the config should be saved
  75. var shouldSaveConfig bool
  76. // YayConf holds the current config values for yay.
  77. var config Configuration
  78. // AlpmConf holds the current config values for pacman.
  79. var alpmConf alpm.PacmanConfig
  80. // AlpmHandle is the alpm handle used by yay.
  81. var alpmHandle *alpm.Handle
  82. func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  83. file, err := os.Open(pacmanconf)
  84. if err != nil {
  85. return
  86. }
  87. conf, err = alpm.ParseConfig(file)
  88. if err != nil {
  89. return
  90. }
  91. return
  92. }
  93. // SaveConfig writes yay config to file.
  94. func (config *Configuration) saveConfig() error {
  95. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  96. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  97. if err != nil {
  98. return err
  99. }
  100. defer in.Close()
  101. _, err = in.Write(marshalledinfo)
  102. if err != nil {
  103. return err
  104. }
  105. err = in.Sync()
  106. return err
  107. }
  108. func defaultSettings(config *Configuration) {
  109. config.BuildDir = cacheHome + "/"
  110. config.CleanAfter = false
  111. config.Editor = ""
  112. config.EditorFlags = ""
  113. config.Devel = false
  114. config.MakepkgBin = "makepkg"
  115. config.NoConfirm = false
  116. config.PacmanBin = "pacman"
  117. config.PacmanConf = "/etc/pacman.conf"
  118. config.GpgFlags = ""
  119. config.MFlags = ""
  120. config.SortMode = BottomUp
  121. config.SortBy = "votes"
  122. config.SudoLoop = false
  123. config.TarBin = "bsdtar"
  124. config.GitBin = "git"
  125. config.GpgBin = "gpg"
  126. config.TimeUpdate = false
  127. config.RequestSplitN = 150
  128. config.ReDownload = "no"
  129. config.ReBuild = "no"
  130. config.AnswerClean = ""
  131. config.AnswerEdit = ""
  132. config.AnswerUpgrade = ""
  133. }
  134. // Editor returns the preferred system editor.
  135. func editor() (string, []string) {
  136. switch {
  137. case config.Editor != "":
  138. editor, err := exec.LookPath(config.Editor)
  139. if err != nil {
  140. fmt.Println(err)
  141. } else {
  142. return editor, strings.Fields(config.EditorFlags)
  143. }
  144. fallthrough
  145. case os.Getenv("EDITOR") != "":
  146. editorArgs := strings.Fields(os.Getenv("EDITOR"))
  147. editor, err := exec.LookPath(editorArgs[0])
  148. if err != nil {
  149. fmt.Println(err)
  150. } else {
  151. return editor, editorArgs[1:]
  152. }
  153. fallthrough
  154. case os.Getenv("VISUAL") != "":
  155. editorArgs := strings.Fields(os.Getenv("VISUAL"))
  156. editor, err := exec.LookPath(editorArgs[0])
  157. if err != nil {
  158. fmt.Println(err)
  159. } else {
  160. return editor, editorArgs[1:]
  161. }
  162. fallthrough
  163. default:
  164. fmt.Println()
  165. fmt.Println(bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
  166. fmt.Println(bold(red(arrow)) + bold(" Please add ") + bold(cyan("$EDITOR")) + bold(" or ") + bold(cyan("$VISUAL")) + bold(" to your environment variables."))
  167. for {
  168. fmt.Print(green(bold(arrow + " Edit PKGBUILD with: ")))
  169. editorInput, err := getInput("")
  170. if err != nil {
  171. fmt.Println(err)
  172. continue
  173. }
  174. editorArgs := strings.Fields(editorInput)
  175. editor, err := exec.LookPath(editorArgs[0])
  176. if err != nil {
  177. fmt.Println(err)
  178. continue
  179. }
  180. return editor, editorArgs[1:]
  181. }
  182. }
  183. }
  184. // ContinueTask prompts if user wants to continue task.
  185. //If NoConfirm is set the action will continue without user input.
  186. func continueTask(s string, def string) (cont bool) {
  187. if config.NoConfirm {
  188. return true
  189. }
  190. var postFix string
  191. if def == "nN" {
  192. postFix = " [Y/n] "
  193. } else {
  194. postFix = " [y/N] "
  195. }
  196. var response string
  197. fmt.Print(bold(green(arrow)+" "+s+" "), bold(postFix))
  198. n, err := fmt.Scanln(&response)
  199. if err != nil || n == 0 {
  200. return true
  201. }
  202. if response == string(def[0]) || response == string(def[1]) {
  203. return false
  204. }
  205. return true
  206. }
  207. func getInput(defaultValue string) (string, error) {
  208. if defaultValue != "" || config.NoConfirm {
  209. fmt.Println(defaultValue)
  210. return defaultValue, nil
  211. }
  212. reader := bufio.NewReader(os.Stdin)
  213. buf, overflow, err := reader.ReadLine()
  214. if err != nil {
  215. return "", err
  216. }
  217. if overflow {
  218. return "", fmt.Errorf("Input too long")
  219. }
  220. return string(buf), nil
  221. }
  222. func (config Configuration) String() string {
  223. var buf bytes.Buffer
  224. enc := json.NewEncoder(&buf)
  225. enc.SetIndent("", "\t")
  226. if err := enc.Encode(config); err != nil {
  227. fmt.Println(err)
  228. }
  229. return buf.String()
  230. }