utils.go 915 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package aur
  2. import (
  3. "encoding/json"
  4. "io"
  5. "net/http"
  6. "os"
  7. )
  8. // BaseURL givers the AUR default address.
  9. const BaseURL string = "https://aur.archlinux.org"
  10. // Editor gives the default system editor, uses vi in last case
  11. var Editor = "vi"
  12. func init() {
  13. if os.Getenv("EDITOR") != "" {
  14. Editor = os.Getenv("EDITOR")
  15. }
  16. }
  17. // getJSON handles JSON retrieval and decoding to struct
  18. func getJSON(url string, target interface{}) error {
  19. r, err := http.Get(url)
  20. if err != nil {
  21. return err
  22. }
  23. defer r.Body.Close()
  24. return json.NewDecoder(r.Body).Decode(target)
  25. }
  26. func downloadFile(filepath string, url string) (err error) {
  27. // Create the file
  28. out, err := os.Create(filepath)
  29. if err != nil {
  30. return err
  31. }
  32. defer out.Close()
  33. // Get the data
  34. resp, err := http.Get(url)
  35. if err != nil {
  36. return err
  37. }
  38. defer resp.Body.Close()
  39. // Writer the body to file
  40. _, err = io.Copy(out, resp.Body)
  41. return err
  42. }