Check if LICENSE exists

This commit is contained in:
harjyotbagga 2022-02-19 18:06:46 +05:30
parent a599632379
commit 320d88f8d2
3 changed files with 53 additions and 5 deletions

View file

@ -41,6 +41,7 @@ type License struct {
func init() { func init() {
// Allows a user to not use a license. // Allows a user to not use a license.
Licenses["none"] = License{"None", []string{"none", "false"}, "", ""} Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}
Licenses["empty"] = License{"Empty", []string{"empty", "blank"}, "", ""}
initApache2() initApache2()
initMit() initMit()
@ -55,7 +56,6 @@ func init() {
// getLicense returns license specified by user in flag or in config. // getLicense returns license specified by user in flag or in config.
// If user didn't specify the license, it returns none // If user didn't specify the license, it returns none
// //
// TODO: Inspect project for existing license
func getLicense() License { func getLicense() License {
// If explicitly flagged, use that. // If explicitly flagged, use that.
if userLicense != "" { if userLicense != "" {
@ -73,8 +73,8 @@ func getLicense() License {
return findLicense(viper.GetString("license")) return findLicense(viper.GetString("license"))
} }
// If user didn't set any license, use none by default // If user didn't set any license, use empty by default
return Licenses["none"] return Licenses["empty"]
} }
func copyrightLine() string { func copyrightLine() string {

View file

@ -3,6 +3,8 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"regexp"
"text/template" "text/template"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -65,13 +67,59 @@ func (p *Project) Create() error {
} }
// create license // create license
if p.Legal.Name != "None" {
return p.createLicenseFile() return p.createLicenseFile()
}
return nil
} }
func (p *Project) createLicenseFile() error { func (p *Project) createLicenseFile() error {
data := map[string]interface{}{ data := map[string]interface{}{
"copyright": copyrightLine(), "copyright": copyrightLine(),
} }
licensesExist := []string{}
err := filepath.Walk(p.AbsolutePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && filepath.Ext(path) != ".txt" && filepath.Ext(path) != ".md" && filepath.Ext(path) != "" {
return nil
}
reg := regexp.MustCompile(`(?i).*license\.?.*`)
if reg.MatchString(info.Name()) {
licensesExist = append(licensesExist, info.Name())
}
return nil
})
if err != nil {
return err
}
if len(licensesExist) > 0 {
fmt.Println("Licenses already exist in the project")
fmt.Println("Licenses found:")
for _, license := range licensesExist {
fmt.Printf(" %s\n", license)
}
fmt.Print("Would you like still to add a license? [Y/n] ")
var answer string
fmt.Scanln(&answer)
if !(answer == "y" || answer == "Y") {
return nil
}
licenseFound := false
for _, license := range licensesExist {
if license == "LICENSE" {
licenseFound = true
}
}
if licenseFound {
fmt.Print("LICENSE exists. Would you like to overwrite it? [Y/n] ")
fmt.Scanln(&answer)
if !(answer == "y" || answer == "Y") {
return nil
}
}
}
licenseFile, err := os.Create(fmt.Sprintf("%s/LICENSE", p.AbsolutePath)) licenseFile, err := os.Create(fmt.Sprintf("%s/LICENSE", p.AbsolutePath))
if err != nil { if err != nil {
return err return err

View file

@ -50,7 +50,7 @@ func init() {
cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))) cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")))
cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))) cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")))
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>") viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "none") viper.SetDefault("license", "empty")
rootCmd.AddCommand(addCmd) rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(initCmd) rootCmd.AddCommand(initCmd)