xref: /aosp_15_r20/external/licenseclassifier/v2/assets/embed.go (revision 46c4c49da23cae783fa41bf46525a6505638499a)
1*46c4c49dSIbrahim Kanouche// Copyright 2022 Google Inc.
2*46c4c49dSIbrahim Kanouche//
3*46c4c49dSIbrahim Kanouche// Licensed under the Apache License, Version 2.0 (the "License");
4*46c4c49dSIbrahim Kanouche// you may not use this file except in compliance with the License.
5*46c4c49dSIbrahim Kanouche// You may obtain a copy of the License at
6*46c4c49dSIbrahim Kanouche//
7*46c4c49dSIbrahim Kanouche//	http://www.apache.org/licenses/LICENSE-2.0
8*46c4c49dSIbrahim Kanouche//
9*46c4c49dSIbrahim Kanouche// Unless required by applicable law or agreed to in writing, software
10*46c4c49dSIbrahim Kanouche// distributed under the License is distributed on an "AS IS" BASIS,
11*46c4c49dSIbrahim Kanouche// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*46c4c49dSIbrahim Kanouche// See the License for the specific language governing permissions and
13*46c4c49dSIbrahim Kanouche// limitations under the License.
14*46c4c49dSIbrahim Kanouchepackage assets
15*46c4c49dSIbrahim Kanouche
16*46c4c49dSIbrahim Kanoucheimport (
17*46c4c49dSIbrahim Kanouche	"embed"
18*46c4c49dSIbrahim Kanouche	"io/fs"
19*46c4c49dSIbrahim Kanouche	"strings"
20*46c4c49dSIbrahim Kanouche
21*46c4c49dSIbrahim Kanouche	classifier "github.com/google/licenseclassifier/v2"
22*46c4c49dSIbrahim Kanouche)
23*46c4c49dSIbrahim Kanouche
24*46c4c49dSIbrahim Kanouche//go:embed */*/*
25*46c4c49dSIbrahim Kanouchevar licenseFS embed.FS
26*46c4c49dSIbrahim Kanouche
27*46c4c49dSIbrahim Kanouche// DefaultClassifier returns a classifier loaded with the contents of the
28*46c4c49dSIbrahim Kanouche// assets directory.
29*46c4c49dSIbrahim Kanouchefunc DefaultClassifier() (*classifier.Classifier, error) {
30*46c4c49dSIbrahim Kanouche	c := classifier.NewClassifier(.8)
31*46c4c49dSIbrahim Kanouche
32*46c4c49dSIbrahim Kanouche	err := fs.WalkDir(licenseFS, ".", func(path string, d fs.DirEntry, err error) error {
33*46c4c49dSIbrahim Kanouche		if err != nil {
34*46c4c49dSIbrahim Kanouche			return err
35*46c4c49dSIbrahim Kanouche		}
36*46c4c49dSIbrahim Kanouche		if d.IsDir() {
37*46c4c49dSIbrahim Kanouche			return nil
38*46c4c49dSIbrahim Kanouche		}
39*46c4c49dSIbrahim Kanouche
40*46c4c49dSIbrahim Kanouche		b, err := licenseFS.ReadFile(path)
41*46c4c49dSIbrahim Kanouche		if err != nil {
42*46c4c49dSIbrahim Kanouche			return err
43*46c4c49dSIbrahim Kanouche		}
44*46c4c49dSIbrahim Kanouche		splits := strings.Split(path, "/")
45*46c4c49dSIbrahim Kanouche		category, name, variant := splits[0], splits[1], splits[2]
46*46c4c49dSIbrahim Kanouche		c.AddContent(category, name, variant, b)
47*46c4c49dSIbrahim Kanouche		return nil
48*46c4c49dSIbrahim Kanouche	})
49*46c4c49dSIbrahim Kanouche
50*46c4c49dSIbrahim Kanouche	if err != nil {
51*46c4c49dSIbrahim Kanouche		return nil, err
52*46c4c49dSIbrahim Kanouche	}
53*46c4c49dSIbrahim Kanouche	return c, nil
54*46c4c49dSIbrahim Kanouche
55*46c4c49dSIbrahim Kanouche}
56*46c4c49dSIbrahim Kanouche
57*46c4c49dSIbrahim Kanouche// ReadLicenseFile locates and reads the license archive file.  Absolute paths are used unmodified.  Relative paths are expected to be in the licenses directory of the licenseclassifier package.
58*46c4c49dSIbrahim Kanouchefunc ReadLicenseFile(filename string) ([]byte, error) {
59*46c4c49dSIbrahim Kanouche	return licenseFS.ReadFile(filename)
60*46c4c49dSIbrahim Kanouche}
61*46c4c49dSIbrahim Kanouche
62*46c4c49dSIbrahim Kanouche// ReadLicenseDir reads directory containing the license files.
63*46c4c49dSIbrahim Kanouchefunc ReadLicenseDir() ([]fs.DirEntry, error) {
64*46c4c49dSIbrahim Kanouche	return licenseFS.ReadDir(".")
65*46c4c49dSIbrahim Kanouche}
66