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.

104 lines
1.8 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-licenses"
  20. url = "https://api.github.com/repos/spdx/license-list-data/tarball"
  21. destination = ""
  22. )
  23. flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses")
  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 !strings.Contains(hdr.Name, "/text/") {
  55. continue
  56. }
  57. if filepath.Ext(hdr.Name) != ".txt" {
  58. continue
  59. }
  60. if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
  61. continue
  62. }
  63. if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
  64. continue
  65. }
  66. out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
  67. if err != nil {
  68. log.Fatalf("Failed to create new file. %s", err)
  69. }
  70. defer out.Close()
  71. if _, err := io.Copy(out, tr); err != nil {
  72. log.Fatalf("Failed to write new file. %s", err)
  73. } else {
  74. fmt.Printf("Written %s\n", out.Name())
  75. }
  76. }
  77. fmt.Println("Done")
  78. }