Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fgksgf committed May 21, 2022
1 parent 3fb007d commit 7a38c4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
26 changes: 11 additions & 15 deletions pkg/license/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package license
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/google/licensecheck"
Expand All @@ -40,10 +40,6 @@ const (
var (
_scanner *licensecheck.Scanner
scannerOnce sync.Once

dualLicensePatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)This project is covered by two different licenses: (?P<license>[^.]+)`),
}
)

// scanner returns a licensecheck.Scanner instance with its build-in licenses.
Expand All @@ -60,22 +56,22 @@ func scanner() *licensecheck.Scanner {
}

// Identify identifies the Spdx ID of the given license content.
// If it's a dual-license, it will return `<Licenses 1> and <Licenses 2>`.
func Identify(content string) (string, error) {
for _, pattern := range dualLicensePatterns {
matches := pattern.FindStringSubmatch(content)
for i, name := range pattern.SubexpNames() {
if name == "license" && len(matches) >= i {
return matches[i], nil
}
}
}

coverage := scanner().Scan([]byte(content))
if coverage.Percent < coverageThreshold {
return "", fmt.Errorf("cannot identify the license, coverage: %.1f%%", coverage.Percent)
}

return coverage.Match[0].ID, nil
var sb strings.Builder
sb.WriteString(coverage.Match[0].ID)

for i := 1; i < len(coverage.Match); i++ {
sb.WriteString(" and ")
sb.WriteString(coverage.Match[i].ID)
}

return sb.String(), nil
}

// GetLicenseContent returns the content of the license file with the given Spdx ID.
Expand Down
2 changes: 1 addition & 1 deletion pkg/license/identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
`,
want: "MIT and Apache",
want: "MIT and Apache-2.0",
},
{
name: "MIT",
Expand Down

0 comments on commit 7a38c4e

Please sign in to comment.