config.go 5.0 KB

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