config.go 5.2 KB

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