Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nasa9084 committed Dec 27, 2017
0 parents commit cb15ee1
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 0 deletions.
114 changes: 114 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

# Created by https://www.gitignore.io/api/go,emacs,macos

### Emacs ###
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*

# Org-mode
.org-id-locations
*_archive

# flymake-mode
*_flymake.*

# eshell files
/eshell/history
/eshell/lastdir

# elpa packages
/elpa/

# reftex files
*.rel

# AUCTeX auto folder
/auto/

# cask packages
.cask/
dist/

# Flycheck
flycheck_*.el

# server auth directory
/server/

# projectiles files
.projectile
projectile-bookmarks.eld

# directory configuration
.dir-locals.el

# saveplace
places

# url cache
url/cache/

# cedet
ede-projects.el

# smex
smex-items

# company-statistics
company-statistics-cache.el

# anaconda-mode
anaconda-mode/

### Go ###
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.gitignore.io/api/go,emacs,macos
vendor
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/jessevdk/go-flags"
version = "1.3.0"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 nasa9084

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
git-license
===========

Get LICENSE from [GitHub Licenses API](https://developer.github.com/v3/licenses/).

## Installation

### go get

`go get github.com/nasa9084/git-license`

### download binary

Download from [releases page](https://github.com/nasa9084/git-license/releases) and put the binary into your PATH.

### build your own

``` shell
$ git clone https://github.com/nasa9084/git-license.git
$ cd git-license
$ dep ensure
$ go build .
```

and copy or move `git-license` binary into your PATH.

## How to use

Basically:

``` shell
$ git license --username YOUR_USER_NAME LICENSE_NAME
```

such as: `git license --username nasa9084 mit`.

You can see the list of license names with:

``` shell
$ git license -l
```

### Show HELP

``` shell
$ git license --help
```
Binary file added git-license
Binary file not shown.
131 changes: 131 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"

flags "github.com/jessevdk/go-flags"
)

type options struct {
GitHubAPIURI string `long:"github-api-uri" default:"https://api.github.com/licenses" default-mask:"-" env:"GITHUB_API_URI" description:"URI for GitHub licenses API, without trailing slash"`
ListFlag bool `short:"l" long:"list" description:"show license name list"`
Username string `short:"u" long:"username" env:"USERNAME" description:"Username to embed to LICENSE file"`
Year string `short:"y" long:"year" default-mask:"current year" description:"License year"`
PosArgs struct {
License string `positional-arg-name:"LICENSE"`
} `positional-args:"true"`
}

type license struct {
Key string `json:"key"`
Name string `json:"name"`
SPDXID string `json:"spdx_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
}

type licenseDetail struct {
Key string `json:"key"`
Name string `json:"name"`
SPDXID string `json:"spdx_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
Description string `json:"description"`
Implementation string `json:"implementation"`
Permissions []string `json:"permissions"`
Conditions []string `json:"conditions"`
Limitations []string `json:"limitations"`
Body string `json:"body"`
}

func main() { os.Exit(exec()) }

func exec() int {
var opts options
if _, err := flags.Parse(&opts); err != nil {
if fe, ok := err.(*flags.Error); ok && fe.Type == flags.ErrHelp {
return 0
}

printErr(err)
return 1
}
if opts.ListFlag {
return showList(opts)
}
return showLicense(opts)
}

func showList(opts options) int {
res, err := http.Get(opts.GitHubAPIURI)
if err != nil {
printErr(err)
return 1
}
if res.StatusCode != http.StatusOK {
apiErr(res.StatusCode, res.Body)
return 1
}
var licenses []license
if err := json.NewDecoder(res.Body).Decode(&licenses); err != nil {
printErr(err)
return 1
}
var names []string
for _, l := range licenses {
names = append(names, l.Key)
}
fmt.Print(strings.Join(names, "\n"))
return 0
}

func showLicense(opts options) int {
if opts.PosArgs.License == "" || opts.Username == "" {
printErr(fmt.Errorf("license and username is required"))
return 1
}
uri := opts.GitHubAPIURI + "/" + strings.ToLower(opts.PosArgs.License)
res, err := http.Get(uri)
if err != nil {
printErr(err)
return 1
}
if res.StatusCode != http.StatusOK {
apiErr(res.StatusCode, res.Body)
return 1
}
var license licenseDetail
if err := json.NewDecoder(res.Body).Decode(&license); err != nil {
printErr(err)
return 1
}
year := opts.Year
if year == "" {
year = strconv.Itoa(time.Now().Year())
}
text := license.Body
text = strings.Replace(text, "[year]", year, -1)
text = strings.Replace(text, "[fullname]", opts.Username, -1)
fmt.Print(text)
return 0
}

func apiErr(code int, body io.Reader) {
b, err := ioutil.ReadAll(body)
if err != nil {
printErr(err)
}
printErr(fmt.Errorf("api error [%d] %s", code, string(b)))
}

func printErr(err error) {
fmt.Fprintf(os.Stderr, "error occured: %s\n", err)
}

0 comments on commit cb15ee1

Please sign in to comment.