config.go 7.4 KB

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