config.go 5.1 KB

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