1
0
licensmith/main.go

53 lines
1.3 KiB
Go
Raw Normal View History

2024-12-27 05:34:15 +01:00
package main
import (
"flag"
"fmt"
"os"
)
func main() {
OutputFile := flag.String("output", "LICENSE", "Specify different output file")
LicenseID := flag.String("license", "", "Specify license by SPDX ID (e.g. BSD-3-Clause)")
AuthorName := flag.String("name", "", "Set the author name (read from Git by default)")
AuthorEmail := flag.String("email", "", "Set the author email (read from Git by default)")
2024-12-28 20:56:07 +01:00
ListLicenses := flag.Bool("list", false, "List available licenses")
2024-12-27 05:34:15 +01:00
flag.Parse()
2024-12-28 20:56:07 +01:00
if *ListLicenses {
listLicenses()
2024-12-27 05:34:15 +01:00
os.Exit(0)
}
if *LicenseID == "" {
2024-12-27 05:34:15 +01:00
fmt.Printf("Error: No license specified\n\nUse --license LICENSE\n\nAvailable licenses:\n")
2024-12-28 20:56:07 +01:00
listLicenses()
2024-12-27 05:34:15 +01:00
os.Exit(1)
}
licenseCtx, err := NewLicenseContext(*AuthorName, *AuthorEmail)
if err != nil && err.Error() == "Can't read Git config" {
fmt.Printf(
"Error: Can't read Git config.\n\nUse --name \"NAME\" and --email EMAIL instead.\n",
)
os.Exit(3)
2024-12-27 05:34:15 +01:00
}
licenser := Licenser{
LicenseID: *LicenseID,
LicenseContext: licenseCtx,
OutputFile: *OutputFile,
2024-12-27 05:34:15 +01:00
}
err = licenser.Generate()
if err != nil && err.Error() == "Not supported license" {
fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *LicenseID)
2024-12-28 21:11:35 +01:00
listLicenses()
os.Exit(2)
2024-12-27 05:34:15 +01:00
}
if err = licenser.WriteFile(); err != nil {
panic(err)
}
2024-12-27 05:34:15 +01:00
}