Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

generate-gitignores.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // +build ignore
  2. package main
  3. import (
  4. "archive/tar"
  5. "compress/gzip"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. "strings"
  16. )
  17. func main() {
  18. var (
  19. prefix = "gitea-gitignore"
  20. url = "https://api.github.com/repos/github/gitignore/tarball"
  21. destination = ""
  22. )
  23. flag.StringVar(&destination, "dest", "options/gitignore/", "destination for the gitignores")
  24. flag.Parse()
  25. file, err := ioutil.TempFile(os.TempDir(), prefix)
  26. if err != nil {
  27. log.Fatalf("Failed to create temp file. %s", err)
  28. }
  29. defer os.Remove(file.Name())
  30. resp, err := http.Get(url)
  31. if err != nil {
  32. log.Fatalf("Failed to download archive. %s", err)
  33. }
  34. defer resp.Body.Close()
  35. if _, err := io.Copy(file, resp.Body); err != nil {
  36. log.Fatalf("Failed to copy archive to file. %s", err)
  37. }
  38. if _, err := file.Seek(0, 0); err != nil {
  39. log.Fatalf("Failed to reset seek on archive. %s", err)
  40. }
  41. gz, err := gzip.NewReader(file)
  42. if err != nil {
  43. log.Fatalf("Failed to gunzip the archive. %s", err)
  44. }
  45. tr := tar.NewReader(gz)
  46. for {
  47. hdr, err := tr.Next()
  48. if err == io.EOF {
  49. break
  50. }
  51. if err != nil {
  52. log.Fatalf("Failed to iterate archive. %s", err)
  53. }
  54. if filepath.Ext(hdr.Name) != ".gitignore" {
  55. continue
  56. }
  57. out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".gitignore")))
  58. if err != nil {
  59. log.Fatalf("Failed to create new file. %s", err)
  60. }
  61. defer out.Close()
  62. if _, err := io.Copy(out, tr); err != nil {
  63. log.Fatalf("Failed to write new file. %s", err)
  64. } else {
  65. fmt.Printf("Written %s\n", out.Name())
  66. }
  67. }
  68. fmt.Println("Done")
  69. }