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.

118 lines
2.3 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. "code.gitea.io/gitea/modules/util"
  17. )
  18. func main() {
  19. var (
  20. prefix = "gitea-licenses"
  21. url = "https://api.github.com/repos/spdx/license-list-data/tarball"
  22. githubApiToken = ""
  23. githubUsername = ""
  24. destination = ""
  25. )
  26. flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses")
  27. flag.StringVar(&githubUsername, "username", "", "github username")
  28. flag.StringVar(&githubApiToken, "token", "", "github api token")
  29. flag.Parse()
  30. file, err := ioutil.TempFile(os.TempDir(), prefix)
  31. if err != nil {
  32. log.Fatalf("Failed to create temp file. %s", err)
  33. }
  34. defer util.Remove(file.Name())
  35. req, err := http.NewRequest("GET", url, nil)
  36. if err != nil {
  37. log.Fatalf("Failed to download archive. %s", err)
  38. }
  39. if len(githubApiToken) > 0 && len(githubUsername) > 0 {
  40. req.SetBasicAuth(githubUsername, githubApiToken)
  41. }
  42. resp, err := http.DefaultClient.Do(req)
  43. if err != nil {
  44. log.Fatalf("Failed to download archive. %s", err)
  45. }
  46. defer resp.Body.Close()
  47. if _, err := io.Copy(file, resp.Body); err != nil {
  48. log.Fatalf("Failed to copy archive to file. %s", err)
  49. }
  50. if _, err := file.Seek(0, 0); err != nil {
  51. log.Fatalf("Failed to reset seek on archive. %s", err)
  52. }
  53. gz, err := gzip.NewReader(file)
  54. if err != nil {
  55. log.Fatalf("Failed to gunzip the archive. %s", err)
  56. }
  57. tr := tar.NewReader(gz)
  58. for {
  59. hdr, err := tr.Next()
  60. if err == io.EOF {
  61. break
  62. }
  63. if err != nil {
  64. log.Fatalf("Failed to iterate archive. %s", err)
  65. }
  66. if !strings.Contains(hdr.Name, "/text/") {
  67. continue
  68. }
  69. if filepath.Ext(hdr.Name) != ".txt" {
  70. continue
  71. }
  72. if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
  73. continue
  74. }
  75. if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
  76. continue
  77. }
  78. out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
  79. if err != nil {
  80. log.Fatalf("Failed to create new file. %s", err)
  81. }
  82. defer out.Close()
  83. if _, err := io.Copy(out, tr); err != nil {
  84. log.Fatalf("Failed to write new file. %s", err)
  85. } else {
  86. fmt.Printf("Written %s\n", out.Name())
  87. }
  88. }
  89. fmt.Println("Done")
  90. }