config.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. AURURL string `json:"aururl"`
  32. BuildDir string `json:"buildDir"`
  33. Editor string `json:"editor"`
  34. EditorFlags string `json:"editorflags"`
  35. MakepkgBin string `json:"makepkgbin"`
  36. MakepkgConf string `json:"makepkgconf"`
  37. PacmanBin string `json:"pacmanbin"`
  38. PacmanConf string `json:"pacmanconf"`
  39. TarBin string `json:"tarbin"`
  40. ReDownload string `json:"redownload"`
  41. ReBuild string `json:"rebuild"`
  42. AnswerClean string `json:"answerclean"`
  43. AnswerDiff string `json:"answerdiff"`
  44. AnswerEdit string `json:"answeredit"`
  45. AnswerUpgrade string `json:"answerupgrade"`
  46. GitBin string `json:"gitbin"`
  47. GpgBin string `json:"gpgbin"`
  48. GpgFlags string `json:"gpgflags"`
  49. MFlags string `json:"mflags"`
  50. SortBy string `json:"sortby"`
  51. GitFlags string `json:"gitflags"`
  52. RemoveMake string `json:"removemake"`
  53. RequestSplitN int `json:"requestsplitn"`
  54. SearchMode int `json:"-"`
  55. SortMode int `json:"sortmode"`
  56. CompletionInterval int `json:"completionrefreshtime"`
  57. SudoLoop bool `json:"sudoloop"`
  58. TimeUpdate bool `json:"timeupdate"`
  59. NoConfirm bool `json:"-"`
  60. Devel bool `json:"devel"`
  61. CleanAfter bool `json:"cleanAfter"`
  62. GitClone bool `json:"gitclone"`
  63. Provides bool `json:"provides"`
  64. PGPFetch bool `json:"pgpfetch"`
  65. UpgradeMenu bool `json:"upgrademenu"`
  66. CleanMenu bool `json:"cleanmenu"`
  67. DiffMenu bool `json:"diffmenu"`
  68. EditMenu bool `json:"editmenu"`
  69. CombinedUpgrade bool `json:"combinedupgrade"`
  70. UseAsk bool `json:"useask"`
  71. }
  72. var version = "7.885"
  73. // configFileName holds the name of the config file.
  74. const configFileName string = "config.json"
  75. // vcsFileName holds the name of the vcs file.
  76. const vcsFileName string = "vcs.json"
  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 = 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.AURURL = "https://aur.archlinux.org"
  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. }