1package mox
2
3import (
4 "fmt"
5 "io"
6 "io/fs"
7 "strings"
8)
9
10var LicensesFsys fs.FS
11
12// LicensesWrite writes the licenses to dst.
13func LicensesWrite(dst io.Writer) error {
14 copyFile := func(p string) error {
15 f, err := LicensesFsys.Open(p)
16 if err != nil {
17 return fmt.Errorf("open license file: %v", err)
18 }
19 if _, err := io.Copy(dst, f); err != nil {
20 return fmt.Errorf("copy license file: %v", err)
21 }
22 if err := f.Close(); err != nil {
23 return fmt.Errorf("close license file: %v", err)
24 }
25 return nil
26 }
27
28 if _, err := fmt.Fprintf(dst, "# github.com/mjl-/mox/LICENSE\n\n"); err != nil {
29 return err
30 }
31 if err := copyFile("LICENSE.MIT"); err != nil {
32 return err
33 }
34
35 if _, err := fmt.Fprintf(dst, "\n\n# https://publicsuffix.org - Public Suffix List Mozilla\n\n"); err != nil {
36 return err
37 }
38 if err := copyFile("LICENSE.MPLv2.0"); err != nil {
39 return err
40 }
41
42 err := fs.WalkDir(LicensesFsys, "licenses", func(path string, d fs.DirEntry, err error) error {
43 if !d.Type().IsRegular() {
44 return nil
45 }
46 if _, err := fmt.Fprintf(dst, "\n\n# %s\n\n", strings.TrimPrefix(path, "licenses/")); err != nil {
47 return err
48 }
49 return copyFile(path)
50 })
51 if err != nil {
52 return fmt.Errorf("walk licenses: %v", err)
53 }
54 return nil
55}
56