config.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. alpm "github.com/jguer/go-alpm"
  10. )
  11. // Verbosity settings for search
  12. const (
  13. NumberMenu = iota
  14. Detailed
  15. Minimal
  16. )
  17. // Describes Sorting method for numberdisplay
  18. const (
  19. BottomUp = iota
  20. TopDown
  21. )
  22. // Configuration stores yay's config.
  23. type Configuration struct {
  24. BuildDir string `json:"buildDir"`
  25. Editor string `json:"editor"`
  26. MakepkgBin string `json:"makepkgbin"`
  27. PacmanBin string `json:"pacmanbin"`
  28. PacmanConf string `json:"pacmanconf"`
  29. TarBin string `json:"tarbin"`
  30. ReDownload string `json:"redownload"`
  31. ReBuild string `json:"rebuild"`
  32. AnswerClean string `json:"answerclean"`
  33. AnswerEdit string `json:"answeredit"`
  34. AnswerUpgrade string `json:"answerupgrade"`
  35. GitBin string `json:"gitbin"`
  36. GpgBin string `json:"gpgbin"`
  37. GpgFlags string `json:"gpgflags"`
  38. MFlags string `json:"mflags"`
  39. SortBy string `json:"sortby"`
  40. RequestSplitN int `json:"requestsplitn"`
  41. SearchMode int `json:"-"`
  42. SortMode int `json:"sortmode"`
  43. SudoLoop bool `json:"sudoloop"`
  44. TimeUpdate bool `json:"timeupdate"`
  45. NoConfirm bool `json:"-"`
  46. Devel bool `json:"devel"`
  47. CleanAfter bool `json:"cleanAfter"`
  48. }
  49. var version = "3.373"
  50. // configFileName holds the name of the config file.
  51. const configFileName string = "config.json"
  52. // vcsFileName holds the name of the vcs file.
  53. const vcsFileName string = "vcs.json"
  54. // completionFilePrefix holds the prefix used for storing shell completion files.
  55. const completionFilePrefix string = "aur_"
  56. // baseURL givers the AUR default address.
  57. const baseURL string = "https://aur.archlinux.org"
  58. // useColor enables/disables colored printing
  59. var useColor bool
  60. // configHome handles config directory home
  61. var configHome string
  62. // cacheHome handles cache home
  63. var cacheHome string
  64. // savedInfo holds the current vcs info
  65. var savedInfo vcsInfo
  66. // configfile holds yay config file path.
  67. var configFile string
  68. // vcsfile holds yay vcs info file path.
  69. var vcsFile string
  70. // completion file
  71. var completionFile string
  72. // shouldSaveConfig holds whether or not the config should be saved
  73. var shouldSaveConfig bool
  74. // YayConf holds the current config values for yay.
  75. var config Configuration
  76. // AlpmConf holds the current config values for pacman.
  77. var alpmConf alpm.PacmanConfig
  78. // AlpmHandle is the alpm handle used by yay.
  79. var alpmHandle *alpm.Handle
  80. func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  81. file, err := os.Open(pacmanconf)
  82. if err != nil {
  83. return
  84. }
  85. conf, err = alpm.ParseConfig(file)
  86. if err != nil {
  87. return
  88. }
  89. return
  90. }
  91. // SaveConfig writes yay config to file.
  92. func (config *Configuration) saveConfig() error {
  93. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  94. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  95. if err != nil {
  96. return err
  97. }
  98. defer in.Close()
  99. _, err = in.Write(marshalledinfo)
  100. if err != nil {
  101. return err
  102. }
  103. err = in.Sync()
  104. return err
  105. }
  106. func defaultSettings(config *Configuration) {
  107. config.BuildDir = cacheHome + "/"
  108. config.CleanAfter = false
  109. config.Editor = ""
  110. config.Devel = false
  111. config.MakepkgBin = "makepkg"
  112. config.NoConfirm = false
  113. config.PacmanBin = "pacman"
  114. config.PacmanConf = "/etc/pacman.conf"
  115. config.GpgFlags = ""
  116. config.MFlags = ""
  117. config.SortMode = BottomUp
  118. config.SortBy = "votes"
  119. config.SudoLoop = false
  120. config.TarBin = "bsdtar"
  121. config.GitBin = "git"
  122. config.GpgBin = "gpg"
  123. config.TimeUpdate = false
  124. config.RequestSplitN = 150
  125. config.ReDownload = "no"
  126. config.ReBuild = "no"
  127. config.AnswerClean = ""
  128. config.AnswerEdit = ""
  129. config.AnswerUpgrade = ""
  130. }
  131. // Editor returns the preferred system editor.
  132. func editor() string {
  133. switch {
  134. case config.Editor != "":
  135. editor, err := exec.LookPath(config.Editor)
  136. if err != nil {
  137. fmt.Println(err)
  138. } else {
  139. return editor
  140. }
  141. fallthrough
  142. case os.Getenv("EDITOR") != "":
  143. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  144. if err != nil {
  145. fmt.Println(err)
  146. } else {
  147. return editor
  148. }
  149. fallthrough
  150. case os.Getenv("VISUAL") != "":
  151. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  152. if err != nil {
  153. fmt.Println(err)
  154. } else {
  155. return editor
  156. }
  157. fallthrough
  158. default:
  159. fmt.Println(bold(red("Warning:")),
  160. bold(magenta("$EDITOR")), "is not set")
  161. fmt.Println("Please add $EDITOR or to your environment variables.")
  162. editorLoop:
  163. fmt.Print(green("Edit PKGBUILD with:"))
  164. var editorInput string
  165. _, err := fmt.Scanln(&editorInput)
  166. if err != nil {
  167. fmt.Println(err)
  168. goto editorLoop
  169. }
  170. editor, err := exec.LookPath(editorInput)
  171. if err != nil {
  172. fmt.Println(err)
  173. goto editorLoop
  174. }
  175. return editor
  176. }
  177. }
  178. // ContinueTask prompts if user wants to continue task.
  179. //If NoConfirm is set the action will continue without user input.
  180. func continueTask(s string, def string) (cont bool) {
  181. if config.NoConfirm {
  182. return true
  183. }
  184. var postFix string
  185. if def == "nN" {
  186. postFix = " [Y/n] "
  187. } else {
  188. postFix = " [y/N] "
  189. }
  190. var response string
  191. fmt.Print(bold(green(arrow+" "+s+" ")), bold(postFix))
  192. n, err := fmt.Scanln(&response)
  193. if err != nil || n == 0 {
  194. return true
  195. }
  196. if response == string(def[0]) || response == string(def[1]) {
  197. return false
  198. }
  199. return true
  200. }
  201. func getInput(defaultValue string) (string, error) {
  202. if defaultValue != "" || config.NoConfirm {
  203. fmt.Println(defaultValue)
  204. return defaultValue, nil
  205. }
  206. reader := bufio.NewReader(os.Stdin)
  207. buf, overflow, err := reader.ReadLine()
  208. if err != nil {
  209. return "", err
  210. }
  211. if overflow {
  212. return "", fmt.Errorf("Input too long")
  213. }
  214. return string(buf), nil
  215. }
  216. func (config Configuration) String() string {
  217. var buf bytes.Buffer
  218. enc := json.NewEncoder(&buf)
  219. enc.SetIndent("", "\t")
  220. if err := enc.Encode(config); err != nil {
  221. fmt.Println(err)
  222. }
  223. return buf.String()
  224. }