Get copyrights from license file.

This commit is contained in:
Stanislav Nikitin 2020-02-08 12:51:42 +05:00
parent 7726d76512
commit 2ccf308e1b
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
3 changed files with 34 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"log"
"os"
"strconv"
"strings"
// local
"go.dev.pztrn.name/glp/structs"
@ -40,7 +41,7 @@ func (o *outputter) Write(deps []*structs.Dependency, outFile string) {
// Write dependencies information.
for _, dep := range deps {
_ = writer.Write([]string{dep.Name, dep.Version, dep.License.Name, dep.VCS.VCSPath, dep.License.URL, dep.Parent, dep.License.Copyrights})
_ = writer.Write([]string{dep.Name, dep.Version, dep.License.Name, dep.VCS.VCSPath, dep.License.URL, dep.Parent, strings.Join(dep.License.Copyrights, ",")})
}
writer.Flush()

View File

@ -2,6 +2,7 @@ package projecter
import (
// stdlib
"bufio"
"log"
"os"
"path/filepath"
@ -63,6 +64,31 @@ func (p *Project) initialize(packagePath string) {
}
}
// Parses license file for copyrights.
func (p *Project) parseLicenseForCopyrights(licencePath string) []string {
f, err := os.Open(licencePath)
if err != nil {
log.Println("Failed to open license file for reading:", err.Error())
return nil
}
var copyrights []string
// Read file data line by line.
gosum := bufio.NewScanner(f)
gosum.Split(bufio.ScanLines)
for gosum.Scan() {
line := gosum.Text()
if strings.HasPrefix(strings.ToLower(line), "copyright ") && !strings.Contains(strings.ToLower(line), "notice") {
copyrights = append(copyrights, line)
}
}
return copyrights
}
// Starts project parsing.
func (p *Project) process() {
// We should determine project type.
@ -137,8 +163,12 @@ func (p *Project) process() {
dep.License.Name = licenseName
// Generate license path.
// Generate license URL.
urlFormatter := strings.NewReplacer("{dir}", "", "{/dir}", "", "{file}", licenseFile, "{/file}", licenseFile, "#L{line}", "")
dep.License.URL = urlFormatter.Replace(dep.VCS.SourceURLFileTemplate)
// As we should have dependency locally available we should try
// to parse license file to get copyrights.
dep.License.Copyrights = p.parseLicenseForCopyrights(filepath.Join(dep.LocalPath, licenseFile))
}
}

View File

@ -1,7 +1,7 @@
package structs
type License struct {
Copyrights string
Copyrights []string
Name string
URL string
}