initial commit

This commit is contained in:
ston1th 2016-09-05 22:23:43 +02:00
commit 26571ba9a2
5 changed files with 190 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
golic

24
LICENSE Normal file
View file

@ -0,0 +1,24 @@
Copyright (C) 2016 Marius Schellenberger
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The names of the authors and/or contributors may not be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ston1th BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

26
Makefile Normal file
View file

@ -0,0 +1,26 @@
CC=go
BUILD=build -v
GCFLAGS=--gcflags '-e'
LDFLAGS=--ldflags '-s -w'
PROGRAM=golic
all: $(PROGRAM)
$(PROGRAM): codeqa
CGO_ENABLED=0 $(CC) $(BUILD) $(GCFLAGS) $(LDFLAGS)
clean:
$(CC) clean -x
codeqa: gofmt golint misspell
gofmt:
gofmt -w .
golint:
$(GOPATH)/bin/golint ./...
misspell:
$(GOPATH)/bin/misspell **/*
.PHONY: clean codeqa gofmt golint misspell

5
README.md Normal file
View file

@ -0,0 +1,5 @@
# golic
golic is a simple program to generate a third-party licenses file.
golic uses Godep and the `vendor/` directory as its source of LICENSE files.

134
main.go Normal file
View file

@ -0,0 +1,134 @@
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
)
type deps struct {
ImportPath string
}
type godep struct {
Deps []deps
}
type license struct {
Path string
URL string
Text string
}
const (
markdown = `# Third Party Licenses
{{range $item := .}}
## [{{$item.Path}}]({{$item.URL}})
{{$item.Text}}
{{end}}`
text = `Third Party Licenses
{{range $item := .}}
{{$item.Path}}
{{$item.URL}}
{{$item.Text}}
{{end}}`
)
var (
md bool
file string
lics []license
errFound = errors.New("file found")
licre = regexp.MustCompile("([Ll][Ii][Cc][Ee][Nn][Ss][Ee].*)")
pathre = regexp.MustCompile("vendor/(.*/(.*/.*))/")
)
func init() {
flag.BoolVar(&md, "md", false, "enable markdown output")
flag.StringVar(&file, "f", "third_party_licenses.txt", "output file")
}
func trim(s string) string {
if strings.Count(s, "/") <= 2 {
return s
}
return trim(s[:strings.LastIndex(s, "/")])
}
func main() {
flag.Parse()
var (
tmp *template.Template
gd godep
)
if md {
tmp = template.Must(template.New("").Parse(markdown))
} else {
tmp = template.Must(template.New("").Parse(text))
}
godeps, err := ioutil.ReadFile("./Godeps/Godeps.json")
if err != nil {
exit(err)
}
err = json.Unmarshal(godeps, &gd)
if err != nil {
exit(err)
}
paths := make(map[string]struct{})
for _, d := range gd.Deps {
path := trim(d.ImportPath)
if _, ok := paths[path]; ok {
continue
}
err := filepath.Walk(filepath.Join("vendor", path), walker)
if err != nil && err != errFound {
fmt.Fprintln(os.Stderr, err)
}
paths[path] = struct{}{}
}
output, err := os.Create(file)
if err != nil {
exit(err)
}
if err := tmp.Execute(output, lics); err != nil {
output.Close()
exit(err)
}
}
func walker(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if info.IsDir() {
return nil
}
if !licre.MatchString(info.Name()) {
return nil
}
text, err := ioutil.ReadFile(path)
if err != nil {
return err
}
match := pathre.FindAllStringSubmatch(path, -1)
lics = append(lics, license{
Path: match[0][2],
URL: match[0][1],
Text: string(text),
})
return errFound
}
func exit(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}