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