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

generate-licenses.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/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 filepath.Ext(hdr.Name) != ".txt" {
  55. continue
  56. }
  57. if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
  58. continue
  59. }
  60. if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
  61. continue
  62. }
  63. out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
  64. if err != nil {
  65. log.Fatalf("Failed to create new file. %s", err)
  66. }
  67. defer out.Close()
  68. if _, err := io.Copy(out, tr); err != nil {
  69. log.Fatalf("Failed to write new file. %s", err)
  70. } else {
  71. fmt.Printf("Written %s\n", out.Name())
  72. }
  73. }
  74. fmt.Println("Done")
  75. }