-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
243 lines (205 loc) · 6.58 KB
/
main.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"bufio"
"embed"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type Config struct {
Author string `json:"author"`
}
//go:embed templates/*
var licenseTemplates embed.FS
func readConfig(configFile string) (string, error) {
// If -config flag is specified, use that path
if configFile != "" {
content, err := os.ReadFile(configFile)
if err != nil {
return "", fmt.Errorf("could not read config file '%s': %w", configFile, err)
}
var config Config
err = json.Unmarshal(content, &config)
if err != nil {
return "", fmt.Errorf("could not parse config file '%s': %w", configFile, err)
}
return config.Author, nil
}
// Otherwise, check $XDG_CONFIG_HOME
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome != "" {
configFile = filepath.Join(xdgConfigHome, "licenseit", "config.json")
content, err := os.ReadFile(configFile)
if err == nil {
var config Config
err = json.Unmarshal(content, &config)
if err == nil {
return config.Author, nil
}
}
}
return "", nil
}
func findTemplate(templateBaseName string) (string, error) {
extensions := []string{".txt", ".md"}
for _, ext := range extensions {
templateName := templateBaseName + ext
_, err := licenseTemplates.ReadFile("templates/" + templateName)
if err == nil {
return templateName, nil
}
}
_, err := licenseTemplates.ReadFile("templates/" + templateBaseName)
if err == nil {
return templateBaseName, nil
}
return "", fmt.Errorf("could not find a template for '%s' with supported extensions", templateBaseName)
}
func loadTemplate(templateName string) (string, error) {
// Read the template from the embedded file system
content, err := licenseTemplates.ReadFile("templates/" + templateName)
if err != nil {
return "", fmt.Errorf("could not read template file '%s': %w", templateName, err)
}
return string(content), nil
}
func generateLicense(template string, author string, date string) string {
template = strings.ReplaceAll(template, "{author}", author)
template = strings.ReplaceAll(template, "{date}", date)
return template
}
func saveLicense(licenseContent string, licenseName string, licenseDir string) error {
err := os.MkdirAll(licenseDir, 0755)
if err != nil {
return fmt.Errorf("could not create directory '%s': %w", licenseDir, err)
}
filePath := filepath.Join(licenseDir, licenseName)
// If file exists, prompt for overwrite
if _, err := os.Stat(filePath); err == nil {
fmt.Printf("File '%s' already exists. Overwrite? (y/N): ", filePath)
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("could not read user input: %w", err)
}
response = strings.TrimSpace(response)
if strings.ToLower(response) != "y" {
return fmt.Errorf("operation aborted by user; file exists")
}
}
err = os.WriteFile(filePath, []byte(licenseContent), 0644)
if err != nil {
return fmt.Errorf("could not write license to file '%s': %w", filePath, err)
}
return nil
}
func listTemplates() error {
entries, err := licenseTemplates.ReadDir("templates")
if err != nil {
return fmt.Errorf("could not read templates directory: %w", err)
}
fmt.Println("Available license templates:")
for _, entry := range entries {
fmt.Println(" -", entry.Name())
}
return nil
}
func printHelp() {
programName := filepath.Base(os.Args[0])
fmt.Printf("Usage: %s <template-name> [options]\n", programName)
fmt.Println("Generate a license file based on a template and configuration.")
fmt.Println()
fmt.Println("Commands:")
fmt.Println(" preview Show available license templates")
fmt.Println()
fmt.Println("Arguments:")
fmt.Println(" <template-name> The base name of the license template to use (e.g., 'MIT')")
fmt.Println()
fmt.Println("Options:")
fmt.Println(" -author <name> The author of the license")
fmt.Println(" -config <file> Path to the configuration file")
fmt.Println(" -file <filename> Name of the generated license file")
fmt.Println(" -dir <directory> Directory to save generated licenses")
fmt.Println(" -help Show this help message and exit.")
}
func main() {
// Show help if no arguments provided
if len(os.Args) < 2 || os.Args[1] == "-help" || os.Args[1] == "--help" {
printHelp()
os.Exit(0)
}
if os.Args[1] == "preview" {
err := listTemplates()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
licenseBaseName := os.Args[1]
newArgs := []string{os.Args[0]}
newArgs = append(newArgs, os.Args[2:]...)
os.Args = newArgs
authorFlag := flag.String("author", "", "Author's name for the license")
configFileFlag := flag.String("config", "", "Path to the configuration file")
licenseFileFlag := flag.String("file", "", "Name of the generated license file")
licenseDirFlag := flag.String("dir", ".", "Directory to save generated licenses")
flag.Parse()
author := *authorFlag
if author == "" {
authorFromConfig, err := readConfig(*configFileFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: %v\n", err)
}
if authorFromConfig != "" {
author = authorFromConfig
}
}
if author == "" {
fmt.Print("Author not provided. Please enter the author's name: ")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
os.Exit(1)
}
author = strings.TrimSpace(input)
}
if author == "" {
fmt.Fprintf(os.Stderr, "Error: Author is required.\n")
printHelp()
os.Exit(1)
}
templateName, err := findTemplate(licenseBaseName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading template: %v\n", err)
os.Exit(1)
}
template, err := loadTemplate(templateName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading template: %v\n", err)
os.Exit(1)
}
currentYear := time.Now().Year()
licenseContent := generateLicense(template, author, fmt.Sprintf("%d", currentYear))
// Determine the generated license file name from --file flag or fall back to template name
licenseFileName := *licenseFileFlag
if licenseFileName == "" {
licenseFileName = licenseBaseName + filepath.Ext(templateName)
}
err = saveLicense(licenseContent, licenseFileName, *licenseDirFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "Error saving license: %v\n", err)
os.Exit(1)
}
// Compute absolute path for the output file
absPath, err := filepath.Abs(filepath.Join(*licenseDirFlag, licenseFileName))
if err != nil {
absPath = filepath.Join(*licenseDirFlag, licenseFileName)
}
fmt.Printf("License '%s' created at '%s' for author: %s\n", licenseFileName, absPath, author)
}