Skip to content

Instantly share code, notes, and snippets.

@rjeczalik
Created June 10, 2016 16:33
Show Gist options
  • Save rjeczalik/b0b0561e3d181322c765dabc07c52562 to your computer and use it in GitHub Desktop.
Save rjeczalik/b0b0561e3d181322c765dabc07c52562 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/ryanuber/go-license"
)
func die(v interface{}) {
fmt.Fprintln(os.Stderr, v)
os.Exit(1)
}
func main() {
var packages []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
packages = append(packages, scanner.Text())
}
if err := scanner.Err(); err != nil {
die(err)
}
for _, pkg := range packages {
if !strings.HasPrefix(pkg, "github.com/") {
fmt.Printf("%s\t(error, packages hosted on github are currently supported)\n", pkg)
continue
}
project := strings.TrimPrefix(pkg, "github.com/")
licenses := []string{
"https://raw.githubusercontent.com/" + project + "/master/LICENSE",
"https://raw.githubusercontent.com/" + project + "/master/LICENSE.txt",
"https://raw.githubusercontent.com/" + project + "/master/LICENSE.md",
"https://raw.githubusercontent.com/" + project + "/master/License",
}
var p []byte
var e error
for _, l := range licenses {
resp, err := http.Get(l)
if resp.StatusCode != 200 && err == nil {
resp.Body.Close()
err = errors.New(http.StatusText(resp.StatusCode))
}
if err != nil {
e = err
continue
}
p, e = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if e == nil {
break
}
}
if len(p) == 0 || e != nil {
fmt.Printf("%s\t(%s)\n", pkg, e)
continue
}
lic := license.New("", string(p))
if err := lic.GuessType(); err != nil {
fmt.Printf("%s\t(%s)\n", pkg, err)
continue
}
fmt.Printf("%s\t%s\n", pkg, lic.Type)
}
}
@rjeczalik
Copy link
Author

go list -f '{{join .Deps "\n"}}' koding/klient | grep -E '^(github|golang|gopkg)' | cut -d/ -f1,2,3 | sort -u | go run ~/guesslicense.go 
github.com/aws/aws-sdk-go   Apache-2.0
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment