Skip to content

Commit

Permalink
parent
Browse files Browse the repository at this point in the history
  • Loading branch information
司芳源 committed May 23, 2019
1 parent 99d9760 commit 6ad2d2f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
65 changes: 27 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package main

import (
"encoding/xml"
"fmt"
"github.com/sify21/gopomlicense/config"
"github.com/sify21/gopomlicense/pom"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/net/html/charset"
"log"
"net/http"
"regexp"
"strconv"
"strings"
Expand All @@ -20,7 +17,7 @@ func main() {
pflag.String(config.POM_FILE, "", "pom file (absolute path)")
pflag.Bool("help", false, "show help message")
pflag.String(config.MVN_CMD, "mvn", "maven command location")
pflag.String(config.FORMAT, "%i. %nArtifact Name: %a%n(License: %b%nLicense Url: %c%n)----%n", "output format.\n\t%n: new line\n\t%i: artifact index(begin from 1)\n\t%a: artifact name\n\t(): license related format should be put in()\n\t%b: license name\n\t%c: license url\n\t")
pflag.String(config.FORMAT, "%i. %nArtifact Name: %a%nWebsite: %d%n(License: %b%nLicense Url: %c%n)----%n", "output format.\n\t%n: new line\n\t%i: artifact index(begin from 1)\n\t%a: artifact name\n\t(): license related format should be put in()\n\t%b: license name\n\t%c: license url\n\t%d: artifact website")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if viper.GetBool("help") || viper.GetString(config.POM_FILE) == "" {
Expand All @@ -32,7 +29,30 @@ func main() {
var projectsWithoutLicense []pom.Project
ch := make(chan interface{}, len(artifacts))
for _, v := range artifacts {
go FetchMavenLicense(ch, v)
a := v
go func() {
project, err := pom.ResolveToProject(viper.GetString(config.MAVEN_URL), a)
if err != nil {
log.Printf("Can't resolve to project. %s %v", a.String(), err)
ch <- a
return
}
projectWithLicense, err := pom.FetchMavenLicense(viper.GetString(config.MAVEN_URL), project)
if err != nil {
log.Printf("Fetch license error. %s %v", a.String(), err)
ch <- a
return
}
if projectWithLicense == nil {
ch <- *project
return
}
if project.Url == "" {
project.Url = projectWithLicense.Url
}
project.Licenses = projectWithLicense.Licenses
ch <- *project
}()
}
for i := 0; i < len(artifacts); i++ {
ret := <-ch
Expand Down Expand Up @@ -68,6 +88,8 @@ func main() {
after := strings.ReplaceAll(artifactFormatAfter, "%i", strconv.FormatInt(int64(k+1), 10))
before = strings.ReplaceAll(before, "%a", v.Name)
after = strings.ReplaceAll(after, "%a", v.Name)
before = strings.ReplaceAll(before, "%d", v.Url)
after = strings.ReplaceAll(after, "%d", v.Url)
licStr := ""
for _, l := range v.Licenses {
lic := strings.ReplaceAll(licenseFormat, "%b", l.Name)
Expand All @@ -78,36 +100,3 @@ func main() {
}
}
}

func FetchMavenLicense(ch chan interface{}, a pom.Artifact) {
result, err := pom.FetchPom(viper.GetString(config.MAVEN_URL), a, func(response *http.Response) (interface{}, error) {
var project pom.Project
decoder := xml.NewDecoder(response.Body)
decoder.CharsetReader = charset.NewReaderLabel
if err := decoder.Decode(&project); err != nil {
return nil, err
}
return project, nil
})
if err != nil {
log.Printf("fetch license error. %+v %v", a, err)
ch <- a
} else if project, ok := result.(pom.Project); ok {
if len(project.Licenses) > 0 {
ch <- project
} else if project.ParentGroupId != "" {
parent := pom.Artifact{
GroupId: project.ParentGroupId,
ArtifactId: project.ParentArtifactId,
Version: project.ParentVersion,
}
FetchMavenLicense(ch, parent)
} else {
log.Printf("fetch license error. %s license not found", project.String())
ch <- project
}
} else {
log.Printf("fetch license error. %+v %s", a, "can't convert to project")
ch <- a
}
}
42 changes: 42 additions & 0 deletions pom/fetch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pom

import (
"encoding/xml"
"fmt"
"golang.org/x/net/html/charset"
"log"
"net/http"
"strings"
Expand All @@ -23,3 +26,42 @@ func FetchPom(mvnUrl string, artifact Artifact, converter ConverterFunc) (interf
}

type ConverterFunc func(response *http.Response) (interface{}, error)

func ResolveToProject(mvnUrl string, artifact Artifact) (*Project, error) {
result, err := FetchPom(mvnUrl, artifact, func(response *http.Response) (interface{}, error) {
var project Project
decoder := xml.NewDecoder(response.Body)
decoder.CharsetReader = charset.NewReaderLabel
if err := decoder.Decode(&project); err != nil {
return nil, err
}
return project, nil
})
if err != nil {
return nil, err
}
if p, ok := result.(Project); ok {
return &p, nil
} else {
return nil, fmt.Errorf("%s result can't convert to project", artifact.String())
}
}

func FetchMavenLicense(mvnUrl string, project *Project) (*Project, error) {
if len(project.Licenses) > 0 {
return project, nil
} else if project.ParentGroupId != "" {
parent := Artifact{
GroupId: project.ParentGroupId,
ArtifactId: project.ParentArtifactId,
Version: project.ParentVersion,
}
p, err := ResolveToProject(mvnUrl, parent)
if err != nil {
return nil, err
}
return FetchMavenLicense(mvnUrl, p)
} else {
return nil, nil
}
}

0 comments on commit 6ad2d2f

Please sign in to comment.