license.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package license
  2. import (
  3. "bytes"
  4. _ "embed"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/ulikunitz/xz"
  8. )
  9. //go:embed licenses.xz
  10. var compressedLicenses []byte
  11. type License struct {
  12. Name string `json:"name"`
  13. License string `json:"license"`
  14. URL string `json:"url"`
  15. Version string `json:"version"`
  16. }
  17. type ComponentInfo struct {
  18. Backend []License `json:"backend"`
  19. Frontend []License `json:"frontend"`
  20. }
  21. // GetLicenseInfo returns the license information for all components
  22. func GetLicenseInfo() (*ComponentInfo, error) {
  23. if len(compressedLicenses) == 0 {
  24. return nil, fmt.Errorf("no license data available, run go generate to collect licenses")
  25. }
  26. // Decompress the xz data
  27. reader, err := xz.NewReader(bytes.NewReader(compressedLicenses))
  28. if err != nil {
  29. return nil, fmt.Errorf("failed to create xz reader: %v", err)
  30. }
  31. var decompressed bytes.Buffer
  32. _, err = decompressed.ReadFrom(reader)
  33. if err != nil {
  34. return nil, fmt.Errorf("failed to decompress license data: %v", err)
  35. }
  36. // Parse JSON
  37. var info ComponentInfo
  38. if err := json.Unmarshal(decompressed.Bytes(), &info); err != nil {
  39. return nil, fmt.Errorf("failed to parse license data: %v", err)
  40. }
  41. return &info, nil
  42. }
  43. // GetBackendLicenses returns only backend license information
  44. func GetBackendLicenses() ([]License, error) {
  45. info, err := GetLicenseInfo()
  46. if err != nil {
  47. return nil, err
  48. }
  49. return info.Backend, nil
  50. }
  51. // GetFrontendLicenses returns only frontend license information
  52. func GetFrontendLicenses() ([]License, error) {
  53. info, err := GetLicenseInfo()
  54. if err != nil {
  55. return nil, err
  56. }
  57. return info.Frontend, nil
  58. }
  59. // GetLicenseStats returns statistics about the licenses
  60. func GetLicenseStats() (map[string]interface{}, error) {
  61. info, err := GetLicenseInfo()
  62. if err != nil {
  63. return nil, err
  64. }
  65. stats := make(map[string]interface{})
  66. stats["total_backend"] = len(info.Backend)
  67. stats["total_frontend"] = len(info.Frontend)
  68. stats["total"] = len(info.Backend) + len(info.Frontend)
  69. // Count license types
  70. licenseCount := make(map[string]int)
  71. for _, license := range info.Backend {
  72. licenseCount[license.License]++
  73. }
  74. for _, license := range info.Frontend {
  75. licenseCount[license.License]++
  76. }
  77. stats["license_distribution"] = licenseCount
  78. return stats, nil
  79. }