Skip to content

Commit

Permalink
(feat): Replace [year] and [fullname] in LICENSE
Browse files Browse the repository at this point in the history
This commit adds the abbility to replace the [fullname] and [year]
placeholders inside the license file.

UPDATES #2
  • Loading branch information
KevinGimbel committed Aug 19, 2018
1 parent 9de892a commit 857bdac
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# `license`
> Download open source license files for your project
[![](https://godoc.org/github.com/kevingimbel/license/lib?status.svg)](https://godoc.org/github.com/kevingimbel/license/lib) [![](https://goreportcard.com/badge/github.com/kevingimbel/license)](https://goreportcard.com/report/github.com/kevingimbel/license)

`license` is a command line tool which allows you to download open source licenses from [osl.kevingimbel.com](http://osl.kevingimbel.com).

### Demo
Expand Down
28 changes: 23 additions & 5 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ package cmd
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"

"github.com/kevingimbel/license/lib"
"github.com/spf13/cobra"
)

var format string
var (
format string
year string
name string
)

func init() {
RootCmd.AddCommand(getCmd)
getCmd.Flags().StringVarP(&format, "format", "f", "", "LICENSE file format")
getCmd.Flags().StringVarP(&format, "format", "f", "", "LICENSE file format, e.g. md, txt")
getCmd.Flags().StringVarP(&year, "year", "y", time.Now().Format("2006"), "Year for the license")
getCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the author")
}

// getCmd represents the get command
Expand All @@ -32,7 +40,7 @@ To see a list of all licenses, run "license list". Use the ID to download a lice
var licenseName = strings.ToLower(args[0])

if licenseName == "" {
fmt.Println("Please specifiy a license, see license help get")
fmt.Println("Please specify a license, see license help get")
os.Exit(1)
}

Expand Down Expand Up @@ -66,12 +74,22 @@ func downloadLicense(url string) error {
}

file, err := os.Create(fmt.Sprintf("LICENSE%s", format))

defer file.Close()
if err != nil {
return err
}

_, err = io.Copy(file, response.Body)
// Read in response.Body
bodyBytes, err := ioutil.ReadAll(response.Body)
// Convert to string so we can replace the year and fullname
licenseText := string(bodyBytes)
// Replace year and fullname
licenseText = strings.Replace(licenseText, "[year]", year, 1)
if name != "" {
licenseText = strings.Replace(licenseText, "[fullname]", name, 1)
}

_, err = io.WriteString(file, licenseText)

if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cmd

import "github.com/spf13/cobra"

// RootCmd represents the root command which is the "entry" point of Cobra
var RootCmd = &cobra.Command{
Use: "license [sub]",
Short: "license is a cli tool to download license files",
Long: `Download license files for your open source project.`,
Long: `Download license files for your open source project.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) <= 0 {
println("Usage: license [sub]")
Expand Down
5 changes: 3 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import (
var updateCmd = &cobra.Command{
Use: "update",
Short: "Fetch the latest license data",
Long: fmt.Sprintf(`Download the latest license data as JSON from http://osl.kevin.codes/licenses/
Long: fmt.Sprintf(`Download the latest license data as JSON from http://osl.kevingimbel.com/licenses/
The downlaoded data is stored inside the %s file.
The downloaded data is stored inside the %s file.
`, lib.GetOutputFilePath()),
Run: func(cmd *cobra.Command, args []string) {
Update()
},
}

// Update updates the local cache of available licenses
func Update() (int, []lib.OSLJSONFormat) {
// Create http client and prepare the request
httpClient := &http.Client{}
Expand Down
19 changes: 18 additions & 1 deletion lib/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package lib

import "os"
import (
"os"
"time"
)

var (
outdir = os.Getenv("HOME") + "/.license/"
Expand Down Expand Up @@ -71,3 +74,17 @@ func GetUpdateURL() string {
func GetUserAgentHeader() string {
return "license v" + version
}

// GetYear returns the configured year of the license or the current year if none is set
func GetYear() string {
if config.Year != "" {
return config.Year
}

return time.Now().Format("2006")
}

// GetName returns the configured name for the license file or an empty string if none is set
func GetName() string {
return config.Name
}

0 comments on commit 857bdac

Please sign in to comment.