config.go 6.3 KB

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