2024-12-29 16:46:19 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2025-01-01 20:52:09 +01:00
|
|
|
l "go.wzykubek.xyz/licensmith/internal/license"
|
2024-12-29 16:46:19 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var AuthorName string
|
|
|
|
var AuthorEmail string
|
|
|
|
var OutputFile string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(addCmd)
|
|
|
|
addCmd.Flags().StringVar(&AuthorName, "name", "", "Author name (read from Git by default)")
|
|
|
|
addCmd.Flags().StringVar(&AuthorEmail, "email", "", "Author email (read from Git by default)")
|
|
|
|
addCmd.Flags().StringVarP(&OutputFile, "output", "o", "LICENSE", "Output file")
|
|
|
|
}
|
|
|
|
|
|
|
|
var addCmd = &cobra.Command{
|
2025-01-01 21:01:11 +01:00
|
|
|
Use: "add [license id]",
|
2024-12-29 16:46:19 +01:00
|
|
|
Short: "Add LICENSE based on SPDX ID",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
licenseID := args[0]
|
|
|
|
|
2025-01-01 20:52:09 +01:00
|
|
|
ctx, err := l.NewContext(AuthorName, AuthorEmail)
|
2024-12-29 16:46:19 +01:00
|
|
|
if err != nil && err.Error() == "can't read Git config" {
|
|
|
|
fmt.Println("Error: Can't read Git config")
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
|
2025-01-01 20:52:09 +01:00
|
|
|
license := l.License{
|
|
|
|
ID: licenseID,
|
|
|
|
Context: ctx,
|
2024-12-29 16:46:19 +01:00
|
|
|
}
|
|
|
|
|
2025-01-01 20:52:09 +01:00
|
|
|
err = license.Gen()
|
2025-01-01 21:01:11 +01:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "usupported license" {
|
|
|
|
fmt.Printf("Error: There is no '%s' license\n", licenseID)
|
|
|
|
os.Exit(2)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Internal Error:", err)
|
|
|
|
os.Exit(127)
|
|
|
|
}
|
2024-12-29 16:46:19 +01:00
|
|
|
}
|
|
|
|
|
2025-01-01 20:52:09 +01:00
|
|
|
if err = license.Write(OutputFile); err != nil {
|
2025-01-01 21:01:11 +01:00
|
|
|
fmt.Println("Error: Can't write file:", err)
|
2024-12-29 16:46:19 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|