config.go 7.2 KB

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