config.go 6.4 KB

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