Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace error handling method in pkg/license #120

Merged
merged 6 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pkg/license/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package license

import (
"fmt"
"os"
"path/filepath"
"sync"
Expand All @@ -43,7 +44,7 @@ func NewCatalogWithOptions(opts *CatalogOptions) (catalog *Catalog, err error) {
doptions.CacheDir = opts.CacheDir
downloader, err := NewDownloaderWithOptions(doptions)
if err != nil {
return nil, errors.Wrap(err, "creating downloader")
return nil, fmt.Errorf("creating downloader: %w", err)
}
catalog = &Catalog{
Downloader: downloader,
Expand All @@ -63,7 +64,7 @@ func (catalog *Catalog) LoadLicenses() error {
logrus.Info("Loading license data from downloader")
licenses, err := catalog.Downloader.GetLicenses()
if err != nil {
return errors.Wrap(err, "getting licenses from downloader")
return fmt.Errorf("getting licenses from downloader: %w", err)
}
catalog.List = licenses
logrus.Infof("Got %d licenses from downloader", len(licenses.Licenses))
Expand All @@ -85,7 +86,7 @@ func (catalog *Catalog) WriteLicensesAsText(targetDir string) error {
}
if !util.Exists(targetDir) {
if err := os.MkdirAll(targetDir, os.FileMode(0o755)); err != nil {
return errors.Wrap(err, "creating license data dir")
return fmt.Errorf("creating license data dir: %w", err)
}
}
wg := sync.WaitGroup{}
Expand All @@ -101,13 +102,17 @@ func (catalog *Catalog) WriteLicensesAsText(targetDir string) error {
if err == nil {
err = lerr
} else {
err = errors.Wrap(err, lerr.Error())
err = fmt.Errorf("%v: %w", lerr, err)
}
}
}(l)
}
wg.Wait()
return errors.Wrap(err, "caught errors while writing license files")

if err != nil {
return fmt.Errorf("caught errors while writing license files: %w", err)
}
return nil
}

// GetLicense returns a license struct from its SPDX ID label
Expand Down
33 changes: 18 additions & 15 deletions pkg/license/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ package license
import (
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/nozzle/throttler"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"sigs.k8s.io/release-utils/http"
Expand All @@ -52,7 +52,7 @@ func NewDownloader() (*Downloader, error) {
// NewDownloaderWithOptions returns a downloader with specific options
func NewDownloaderWithOptions(opts *DownloaderOptions) (*Downloader, error) {
if err := opts.Validate(); err != nil {
return nil, errors.Wrap(err, "validating downloader options")
return nil, fmt.Errorf("validating downloader options: %w", err)
}
impl := DefaultDownloaderImpl{}
impl.SetOptions(opts)
Expand Down Expand Up @@ -80,11 +80,11 @@ func (do *DownloaderOptions) Validate() error {
dir, err := os.MkdirTemp(os.TempDir(), "license-cache-")
do.CacheDir = dir
if err != nil {
return errors.Wrap(err, "creating temporary directory")
return fmt.Errorf("creating temporary directory: %w", err)
}
} else if !util.Exists(do.CacheDir) {
if err := os.MkdirAll(do.CacheDir, os.FileMode(0o755)); err != nil {
return errors.Wrap(err, "creating license downloader cache")
return fmt.Errorf("creating license downloader cache: %w", err)
}
}

Expand Down Expand Up @@ -145,12 +145,12 @@ func (ddi *DefaultDownloaderImpl) GetLicenses() (licenses *List, err error) {
// Get the list of licenses
licensesJSON, err := http.NewAgent().Get(LicenseDataURL + LicenseListFilename)
if err != nil {
return nil, errors.Wrap(err, "fetching licenses list")
return nil, fmt.Errorf("fetching licenses list: %w", err)
}

licenseList := &List{}
if err := json.Unmarshal(licensesJSON, licenseList); err != nil {
return nil, errors.Wrap(err, "parsing SPDX licence list")
return nil, fmt.Errorf("parsing SPDX licence list: %w", err)
}

logrus.Infof("Read data for %d licenses. Downloading.", len(licenseList.LicenseData))
Expand Down Expand Up @@ -200,18 +200,21 @@ func (ddi *DefaultDownloaderImpl) cacheData(url string, data []byte) error {
_, err := os.Stat(filepath.Dir(cacheFileName))
if err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(cacheFileName), os.FileMode(0o755)); err != nil {
return errors.Wrap(err, "creating cache directory")
return fmt.Errorf("creating cache directory: %w", err)
}
}
return errors.Wrap(os.WriteFile(cacheFileName, data, os.FileMode(0o644)), "writing cache file")
if err = os.WriteFile(cacheFileName, data, os.FileMode(0o644)); err != nil {
return fmt.Errorf("writing cache file: %w", err)
}
return nil
}

// getCachedData returns cached data for an URL if we have it
func (ddi *DefaultDownloaderImpl) getCachedData(url string) ([]byte, error) {
cacheFileName := ddi.cacheFileName(url)
finfo, err := os.Stat(cacheFileName)
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err, "checking if cached data exists")
return nil, fmt.Errorf("checking if cached data exists: %w", err)
}

if err != nil {
Expand All @@ -221,11 +224,11 @@ func (ddi *DefaultDownloaderImpl) getCachedData(url string) ([]byte, error) {

if finfo.Size() == 0 {
logrus.Warn("Cached file is empty, removing")
return nil, errors.Wrap(os.Remove(cacheFileName), "removing corrupt cached file")
return nil, fmt.Errorf("removing corrupt cached file: %w", os.Remove(cacheFileName))
}
licensesJSON, err := os.ReadFile(cacheFileName)
if err != nil {
return nil, errors.Wrap(err, "reading cached data file")
return nil, fmt.Errorf("reading cached data file: %w", err)
}
return licensesJSON, nil
}
Expand All @@ -237,7 +240,7 @@ func (ddi *DefaultDownloaderImpl) getLicenseFromURL(url string) (license *Licens
if ddi.Options.EnableCache {
licenseJSON, err = ddi.getCachedData(url)
if err != nil {
return nil, errors.Wrap(err, "checking download cache")
return nil, fmt.Errorf("checking download cache: %w", err)
}
if len(licenseJSON) > 0 {
logrus.Debugf("Data for %s is already cached", url)
Expand All @@ -249,22 +252,22 @@ func (ddi *DefaultDownloaderImpl) getLicenseFromURL(url string) (license *Licens
logrus.Infof("Downloading license data from %s", url)
licenseJSON, err = http.NewAgent().Get(url)
if err != nil {
return nil, errors.Wrapf(err, "getting %s", url)
return nil, fmt.Errorf("getting %s: %w", url, err)
}

logrus.Infof("Downloaded %d bytes from %s", len(licenseJSON), url)

if ddi.Options.EnableCache {
if err := ddi.cacheData(url, licenseJSON); err != nil {
return nil, errors.Wrap(err, "caching url data")
return nil, fmt.Errorf("caching url data: %w", err)
}
}
}

// Parse the SPDX license from the JSON data
l, err := ParseLicense(licenseJSON)
if err != nil {
return nil, errors.Wrap(err, "parsing license json data")
return nil, fmt.Errorf("parsing license json data: %w", err)
}
return l, err
}
29 changes: 16 additions & 13 deletions pkg/license/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
package license

import (
"fmt"
"os"
"path/filepath"
"regexp"

licenseclassifier "github.com/google/licenseclassifier/v2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand All @@ -37,7 +37,7 @@ type ReaderDefaultImpl struct {
func (d *ReaderDefaultImpl) ClassifyFile(path string) (licenseTag string, moreTags []string, err error) {
file, err := os.Open(path)
if err != nil {
return licenseTag, nil, errors.Wrap(err, "opening file for analysis")
return licenseTag, nil, fmt.Errorf("opening file for analysis: %w", err)
}
defer file.Close()

Expand Down Expand Up @@ -67,7 +67,7 @@ func (d *ReaderDefaultImpl) ClassifyLicenseFiles(paths []string) (
for _, f := range paths {
label, _, err := d.ClassifyFile(f)
if err != nil {
return nil, unrecognizedPaths, errors.Wrap(err, "classifying file")
return nil, unrecognizedPaths, fmt.Errorf("classifying file: %w", err)
}
if label == "" {
unrecognizedPaths = append(unrecognizedPaths, f)
Expand All @@ -77,11 +77,11 @@ func (d *ReaderDefaultImpl) ClassifyLicenseFiles(paths []string) (
license := d.catalog.GetLicense(label)
if license == nil {
return nil, unrecognizedPaths,
errors.Errorf("ID does not correspond to a valid license: '%s'", label)
fmt.Errorf("ID does not correspond to a valid license: '%s'", label)
}
licenseText, err := os.ReadFile(f)
if err != nil {
return nil, nil, errors.Wrap(err, "reading license text")
return nil, nil, fmt.Errorf("reading license text: %w", err)
}
// Apend to the return results
licenseList = append(licenseList, &ClassifyResult{f, string(licenseText), license})
Expand All @@ -105,7 +105,7 @@ func (d *ReaderDefaultImpl) LicenseFromFile(path string) (license *License, err
// Run the files through the clasifier
label, _, err := d.ClassifyFile(path)
if err != nil {
return nil, errors.Wrap(err, "classifying file")
return nil, fmt.Errorf("classifying file: %w", err)
}

if label == "" {
Expand All @@ -116,7 +116,7 @@ func (d *ReaderDefaultImpl) LicenseFromFile(path string) (license *License, err
// Get the license corresponding to the ID label
license = d.catalog.GetLicense(label)
if license == nil {
return nil, errors.Errorf("ID does not correspond to a valid license: %s", label)
return nil, fmt.Errorf("ID does not correspond to a valid license: %s", label)
}

return license, nil
Expand Down Expand Up @@ -148,7 +148,7 @@ func (d *ReaderDefaultImpl) FindLicenseFiles(path string) ([]string, error) {
}
return nil
}); err != nil {
return nil, errors.Wrap(err, "scanning the directory for license files")
return nil, fmt.Errorf("scanning the directory for license files: %w", err)
}
logrus.Infof("%d license files found in directory", len(licenseList))
return licenseList, nil
Expand All @@ -158,32 +158,35 @@ func (d *ReaderDefaultImpl) FindLicenseFiles(path string) ([]string, error) {
func (d *ReaderDefaultImpl) Initialize(opts *ReaderOptions) error {
// Validate our options before startin
if err := opts.Validate(); err != nil {
return errors.Wrap(err, "validating the license reader options")
return fmt.Errorf("validating the license reader options: %w", err)
}

// Create the implementation's SPDX object
catalogOpts := DefaultCatalogOpts
catalogOpts.CacheDir = opts.CachePath()
catalog, err := NewCatalogWithOptions(catalogOpts)
if err != nil {
return errors.Wrap(err, "creating SPDX object")
return fmt.Errorf("creating SPDX object: %w", err)
}
d.catalog = catalog

if err := d.catalog.LoadLicenses(); err != nil {
return errors.Wrap(err, "loading licenses")
return fmt.Errorf("loading licenses: %w", err)
}

logrus.Infof("Writing license data to %s", opts.CachePath())

// Write the licenses to disk as th classifier will need them
if err := catalog.WriteLicensesAsText(opts.LicensesPath()); err != nil {
return errors.Wrap(err, "writing license data to disk")
return fmt.Errorf("writing license data to disk: %w", err)
}

// Create the implementation's classifier
d.lc = licenseclassifier.NewClassifier(opts.ConfidenceThreshold)
return errors.Wrap(d.lc.LoadLicenses(opts.LicensesPath()), "loading licenses at init")
if err := d.lc.LoadLicenses(opts.LicensesPath()); err != nil {
return fmt.Errorf("loading licenses at init: %w", err)
}
return nil
}

// Classifier returns the license classifier
Expand Down
Loading