aboutsummaryrefslogtreecommitdiff
path: root/cmd/license/main.go
blob: 738cf2e953d95500711c4968afa36879b69069f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Command license is a simple software license generator.
package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"sort"
	"text/template"
	"time"

	"github.com/posener/complete"
	"within.website/x/cmd/license/licenses"
	"within.website/x/internal"
)

var (
	name  = flag.String("name", "", "name of the person licensing the software")
	email = flag.String("email", "", "email of the person licensing the software")
	out   = flag.Bool("out", false, "write to a file instead of stdout")

	showAll = flag.Bool("show", false, "show all licenses instead of generating one")
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "%s [options] <license kind>\n\n", os.Args[0])
		flag.PrintDefaults()

		fmt.Fprintln(os.Stderr, "\nBy default the name and email are scraped from `git config`")
		os.Exit(2)
	}

	var names []string
	for name := range licenses.List {
		names = append(names, name)
	}

	sort.Strings(names)

	internal.HandleCompletion(complete.PredictSet(names...), nil)
}

func main() {
	internal.HandleStartup()

	if *showAll {
		fmt.Println("Licenses available:")
		var names []string
		for license := range licenses.List {
			names = append(names, license)
		}

		sort.Strings(names)

		for _, license := range names {
			fmt.Printf("  %s\n", license)
		}

		os.Exit(1)
	}

	if len(flag.Args()) != 1 {
		flag.Usage()
	}

	kind := flag.Arg(0)

	outfile := "LICENSE"

	var licensetext string
	if _, ok := licenses.List[kind]; !ok {
		fmt.Printf("invalid license kind %s\n", kind)
		os.Exit(1)
	}

	licensetext = licenses.List[kind]

	if kind == "unlicense" && *out {
		outfile = "UNLICENSE"
	}

	if kind == "sqlite" && *out {
		outfile = "BLESSING"
	}

	if *name == "" {
		cmd := exec.Command("git", "config", "user.name")

		out, err := cmd.Output()
		if err != nil {
			log.Fatal(err)
		}

		myname := string(out)
		*name = myname[:len(myname)-1]
	}

	if *email == "" {
		cmd := exec.Command("git", "config", "user.email")

		out, err := cmd.Output()
		if err != nil {
			log.Fatal(err)
		}

		myemail := string(out)
		*email = myemail[:len(myemail)-1]
	}

	var wr io.Writer

	if *out {
		fout, err := os.Create(outfile)
		if err != nil {
			log.Fatal(err)
		}
		defer fout.Close()

		wr = fout
	} else {
		wr = os.Stdout
		defer fmt.Println()
	}

	tmpl, err := template.New("license").Parse(licensetext)
	if err != nil {
		log.Fatal(err)
	}

	err = tmpl.Execute(wr, struct {
		Name  string
		Email string
		Year  int
	}{
		Name:  *name,
		Email: *email,
		Year:  time.Now().Year(),
	})
}