config.go 6.3 KB

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