2021-08-24 19:47:09 +03:00
//go:build ignore
2017-03-15 15:30:16 +03:00
package main
import (
"archive/tar"
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
2020-08-11 23:05:34 +03:00
"code.gitea.io/gitea/modules/util"
2017-03-15 15:30:16 +03:00
)
2017-03-15 18:11:26 +03:00
func main ( ) {
var (
2020-09-04 17:02:37 +03:00
prefix = "gitea-licenses"
url = "https://api.github.com/repos/spdx/license-list-data/tarball"
githubApiToken = ""
githubUsername = ""
destination = ""
2017-03-15 18:11:26 +03:00
)
2017-03-15 15:30:16 +03:00
flag . StringVar ( & destination , "dest" , "options/license/" , "destination for the licenses" )
2020-09-04 17:02:37 +03:00
flag . StringVar ( & githubUsername , "username" , "" , "github username" )
flag . StringVar ( & githubApiToken , "token" , "" , "github api token" )
2017-03-15 15:30:16 +03:00
flag . Parse ( )
2021-09-22 08:38:34 +03:00
file , err := os . CreateTemp ( os . TempDir ( ) , prefix )
2017-03-15 15:30:16 +03:00
if err != nil {
log . Fatalf ( "Failed to create temp file. %s" , err )
}
2020-08-11 23:05:34 +03:00
defer util . Remove ( file . Name ( ) )
2017-03-15 15:30:16 +03:00
2022-08-22 07:33:01 +03:00
if err := os . RemoveAll ( destination ) ; err != nil {
log . Fatalf ( "Cannot clean destination folder: %v" , err )
}
if err := os . MkdirAll ( destination , 0 o755 ) ; err != nil {
log . Fatalf ( "Cannot create destination: %v" , err )
}
2020-09-04 17:02:37 +03:00
req , err := http . NewRequest ( "GET" , url , nil )
if err != nil {
log . Fatalf ( "Failed to download archive. %s" , err )
}
if len ( githubApiToken ) > 0 && len ( githubUsername ) > 0 {
req . SetBasicAuth ( githubUsername , githubApiToken )
}
2017-03-15 15:30:16 +03:00
2020-09-04 17:02:37 +03:00
resp , err := http . DefaultClient . Do ( req )
2017-03-15 15:30:16 +03:00
if err != nil {
log . Fatalf ( "Failed to download archive. %s" , err )
}
defer resp . Body . Close ( )
if _ , err := io . Copy ( file , resp . Body ) ; err != nil {
log . Fatalf ( "Failed to copy archive to file. %s" , err )
}
if _ , err := file . Seek ( 0 , 0 ) ; err != nil {
log . Fatalf ( "Failed to reset seek on archive. %s" , err )
}
gz , err := gzip . NewReader ( file )
if err != nil {
log . Fatalf ( "Failed to gunzip the archive. %s" , err )
}
tr := tar . NewReader ( gz )
for {
hdr , err := tr . Next ( )
if err == io . EOF {
break
}
if err != nil {
log . Fatalf ( "Failed to iterate archive. %s" , err )
}
2018-02-12 21:20:30 +03:00
if ! strings . Contains ( hdr . Name , "/text/" ) {
continue
}
2017-03-15 15:30:16 +03:00
if filepath . Ext ( hdr . Name ) != ".txt" {
continue
}
if strings . HasPrefix ( filepath . Base ( hdr . Name ) , "README" ) {
continue
}
if strings . HasPrefix ( filepath . Base ( hdr . Name ) , "deprecated_" ) {
continue
}
out , err := os . Create ( path . Join ( destination , strings . TrimSuffix ( filepath . Base ( hdr . Name ) , ".txt" ) ) )
if err != nil {
log . Fatalf ( "Failed to create new file. %s" , err )
}
defer out . Close ( )
if _ , err := io . Copy ( out , tr ) ; err != nil {
log . Fatalf ( "Failed to write new file. %s" , err )
} else {
fmt . Printf ( "Written %s\n" , out . Name ( ) )
}
}
fmt . Println ( "Done" )
}