code.gitea.io/gitea@v1.21.7/build/generate-licenses.go (about)

     1  //go:build ignore
     2  
     3  package main
     4  
     5  import (
     6  	"archive/tar"
     7  	"compress/gzip"
     8  	"flag"
     9  	"fmt"
    10  	"io"
    11  	"log"
    12  	"net/http"
    13  	"os"
    14  	"path"
    15  	"path/filepath"
    16  	"strings"
    17  
    18  	"code.gitea.io/gitea/modules/util"
    19  )
    20  
    21  func main() {
    22  	var (
    23  		prefix         = "gitea-licenses"
    24  		url            = "https://api.github.com/repos/spdx/license-list-data/tarball"
    25  		githubApiToken = ""
    26  		githubUsername = ""
    27  		destination    = ""
    28  	)
    29  
    30  	flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses")
    31  	flag.StringVar(&githubUsername, "username", "", "github username")
    32  	flag.StringVar(&githubApiToken, "token", "", "github api token")
    33  	flag.Parse()
    34  
    35  	file, err := os.CreateTemp(os.TempDir(), prefix)
    36  	if err != nil {
    37  		log.Fatalf("Failed to create temp file. %s", err)
    38  	}
    39  
    40  	defer util.Remove(file.Name())
    41  
    42  	if err := os.RemoveAll(destination); err != nil {
    43  		log.Fatalf("Cannot clean destination folder: %v", err)
    44  	}
    45  
    46  	if err := os.MkdirAll(destination, 0o755); err != nil {
    47  		log.Fatalf("Cannot create destination: %v", err)
    48  	}
    49  
    50  	req, err := http.NewRequest("GET", url, nil)
    51  	if err != nil {
    52  		log.Fatalf("Failed to download archive. %s", err)
    53  	}
    54  
    55  	if len(githubApiToken) > 0 && len(githubUsername) > 0 {
    56  		req.SetBasicAuth(githubUsername, githubApiToken)
    57  	}
    58  
    59  	resp, err := http.DefaultClient.Do(req)
    60  	if err != nil {
    61  		log.Fatalf("Failed to download archive. %s", err)
    62  	}
    63  
    64  	defer resp.Body.Close()
    65  
    66  	if _, err := io.Copy(file, resp.Body); err != nil {
    67  		log.Fatalf("Failed to copy archive to file. %s", err)
    68  	}
    69  
    70  	if _, err := file.Seek(0, 0); err != nil {
    71  		log.Fatalf("Failed to reset seek on archive. %s", err)
    72  	}
    73  
    74  	gz, err := gzip.NewReader(file)
    75  	if err != nil {
    76  		log.Fatalf("Failed to gunzip the archive. %s", err)
    77  	}
    78  
    79  	tr := tar.NewReader(gz)
    80  
    81  	for {
    82  		hdr, err := tr.Next()
    83  
    84  		if err == io.EOF {
    85  			break
    86  		}
    87  
    88  		if err != nil {
    89  			log.Fatalf("Failed to iterate archive. %s", err)
    90  		}
    91  
    92  		if !strings.Contains(hdr.Name, "/text/") {
    93  			continue
    94  		}
    95  
    96  		if filepath.Ext(hdr.Name) != ".txt" {
    97  			continue
    98  		}
    99  
   100  		if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
   101  			continue
   102  		}
   103  
   104  		if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
   105  			continue
   106  		}
   107  		out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
   108  		if err != nil {
   109  			log.Fatalf("Failed to create new file. %s", err)
   110  		}
   111  
   112  		defer out.Close()
   113  
   114  		if _, err := io.Copy(out, tr); err != nil {
   115  			log.Fatalf("Failed to write new file. %s", err)
   116  		} else {
   117  			fmt.Printf("Written %s\n", out.Name())
   118  		}
   119  	}
   120  
   121  	fmt.Println("Done")
   122  }