1
0

Compare commits

...

6 Commits

6 changed files with 173 additions and 0 deletions

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
ISC License
Copyright 2024 Wiktor Zykubek
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

21
README.md Normal file
View File

@ -0,0 +1,21 @@
# Licensmith
Effortlessly craft the perfect LICENSE for your Git repo in seconds with a single command!
## Usage
This command will generate ISC `LICENSE` file in your current directory, including current year, and your name read from Git configuration:
```bash
licensmith --license ISC
```
By default, Licensmith read your local repository looking for user details (name and e-mail), as a fallback it uses global configuration.
You can also specify different values using:
```bash
licensmith --license ISC --name "John Doe" --email "jdoe@example.com"
```
To list available templates run:
```bash
licensmith --list
```

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.brono.cloud/wzykubek/licensmith
go 1.23.4

128
main.go Normal file
View File

@ -0,0 +1,128 @@
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"strings"
"text/template"
"time"
)
type LicensingData struct {
AuthorName string
AuthorEmail string
Year int
}
var GitConfigError = errors.New("Can't read Git config")
var NotSupportedError = errors.New("Not supported license")
func getGitUserData() (string, string, error) {
var userData [2]string
for i, v := range []string{"user.name", "user.email"} {
cmd := exec.Command("git", "config", "--get", v)
out, err := cmd.Output()
if err != nil {
return "", "", GitConfigError
}
userData[i] = strings.TrimSpace(string(out))
}
return userData[0], userData[1], nil
}
func getTemplateList() []string {
d, err := os.Open("templates")
if err != nil {
panic(err)
}
files, err := d.Readdir(0)
if err != nil {
panic(err)
}
var tmplList []string
for _, v := range files {
tmplList = append(tmplList, strings.Replace(v.Name(), ".tmpl", "", 1))
}
return tmplList
}
func listTemplates() {
tmplList := getTemplateList()
fmt.Println(strings.Join(tmplList, ", "))
}
func genLicense(lcnsName string, lcnsData LicensingData, outFileName string) error {
tmplFile := "templates/" + lcnsName + ".tmpl"
tmpl, err := template.ParseFiles(tmplFile)
if err != nil {
return NotSupportedError
}
outFile, err := os.Create(outFileName)
if err != nil {
return err
}
defer outFile.Close()
err = tmpl.Execute(outFile, lcnsData)
if err != nil {
return err
}
return nil
}
func main() {
OutputFile := flag.String("output", "LICENSE", "Specify different output file")
License := flag.String("license", "", "Choose a license")
authorName := flag.String("name", "", "Set the author name")
authorEmail := flag.String("email", "", "Set the author email")
ListTemplates := flag.Bool("list", false, "List available licenses")
flag.Parse()
if *ListTemplates {
listTemplates()
os.Exit(0)
}
if *License == "" {
fmt.Printf("Error: No license specified\n\nUse --license LICENSE\n\nAvailable licenses:\n")
listTemplates()
os.Exit(1)
}
if *authorName == "" || *authorEmail == "" {
var err error
*authorName, *authorEmail, err = getGitUserData()
if err != nil {
if errors.Is(err, GitConfigError) {
fmt.Printf(
"Error: Can't read Git config.\n\nUse --name \"NAME\" and --email EMAIL instead.\n",
)
os.Exit(3)
}
}
}
lcnsData := LicensingData{
AuthorName: *authorName,
AuthorEmail: *authorEmail,
Year: time.Now().Year(),
}
err := genLicense(*License, lcnsData, *OutputFile)
if err != nil {
if errors.Is(err, NotSupportedError) {
fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *License)
listTemplates()
os.Exit(2)
}
}
}

7
templates/ISC.tmpl Normal file
View File

@ -0,0 +1,7 @@
ISC License
Copyright {{.Year}} {{.AuthorName}}
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

7
templates/MIT.tmpl Normal file
View File

@ -0,0 +1,7 @@
Copyright {{.Year}} {{.AuthorName}}
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.