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

validate-krew-manifest: add license check #642

Merged
merged 2 commits into from
Aug 30, 2020
Merged
Changes from 1 commit
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
40 changes: 39 additions & 1 deletion cmd/validate-krew-manifest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog"

"sigs.k8s.io/krew/internal/environment"
"sigs.k8s.io/krew/internal/index/indexscanner"
"sigs.k8s.io/krew/internal/index/validation"
"sigs.k8s.io/krew/internal/installation"
Expand Down Expand Up @@ -159,7 +160,44 @@ func installPlatformSpec(manifestFile string, p index.Platform) error {
output := strings.ReplaceAll(string(b), "\n", "\n\t")
return errors.Wrapf(err, "plugin install command failed: %s", output)
}
return nil

err = validateLicenseFileExists(tmpDir)
return errors.Wrap(err, "LICENSE (or alike) file is not extracted from the archive as part of installation")
}

var licenseFiles = []string{
"license",
"license.txt",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also allow license.md?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"copying",
"copying.txt",
}

func validateLicenseFileExists(krewRoot string) error {
dir := environment.NewPaths(krewRoot).InstallPath()
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() {
files = append(files, info.Name())
}
return nil
})
if err != nil {
return errors.Wrap(err, "failed to walk installation directory")
}

for _, f := range files {
klog.V(8).Infof("found installed file: %s", f)
f = strings.ToLower(filepath.Base(f))
for _, lf := range licenseFiles {
if lf == f {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

klog.V(8).Infof("found license file: %s", f)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a map instead?

var licenseFiles = map[string]bool{
  "license": true,
  "license.txt": true,
  "copying": true,
  "copying.txt":true,
}
// ...
if licenseFiles[f] {
  return nil
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return errors.Errorf("could not find license file among [%s]", strings.Join(files, ", "))
}

// findAnyMatchingPlatform finds an <os,arch> pair matches to given selector
Expand Down