You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.6 KiB

  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. }