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