Welcome to mirror list, hosted at ThFree Co, Russian Federation.

noticegen.go « noticegen « tools - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d2a9f21bde254888d1cb74bcb5dfc9289f11a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main

import (
	"encoding/json"
	"flag"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"text/template"
)

var (
	sourcePath = flag.String("source", "", "directory path containing license files")
	tmplPath   = flag.String("template", "", "file path to notice template")
)

type license struct {
	Filename string
	Path     string
	Text     string
}

func main() {
	flag.Parse()

	if *sourcePath == "" || *tmplPath == "" {
		log.Fatal("must provide flags 'source' and 'template'")
	}

	tmpl, err := template.ParseFiles(*tmplPath)
	if err != nil {
		log.Fatal(err)
	}

	var licenses []license

	data, err := exec.Command("go", "mod", "edit", "-json").Output()
	if err != nil {
		log.Fatal(err)
	}
	modInfo := struct {
		Module struct {
			Path string
		}
	}{}
	if err := json.Unmarshal(data, &modInfo); err != nil {
		log.Fatal(err)
	}

	if err := filepath.Walk(*sourcePath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}

		p, err := filepath.Rel(*sourcePath, filepath.Dir(path))
		if err != nil {
			log.Fatal(err)
		}

		if p == modInfo.Module.Path {
			return filepath.SkipDir
		}

		t, err := os.ReadFile(path)
		if err != nil {
			log.Fatal(err)
		}

		licenses = append(licenses, license{
			Filename: filepath.Base(path),
			Path:     p,
			Text:     string(t),
		})

		return nil
	}); err != nil {
		log.Fatal(err)
	}

	if err := tmpl.Execute(os.Stdout, licenses); err != nil {
		log.Fatal(err)
	}
}