Created
November 22, 2016 09:34
-
-
Save rjeczalik/094d57205d6e068ecfe7a6bae072ca6e to your computer and use it in GitHub Desktop.
Guesses license type (depracated, use GitHub API instead).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment