mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
Check if LICENSE exists
This commit is contained in:
parent
a599632379
commit
320d88f8d2
3 changed files with 53 additions and 5 deletions
|
@ -41,6 +41,7 @@ type License struct {
|
|||
func init() {
|
||||
// Allows a user to not use a license.
|
||||
Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}
|
||||
Licenses["empty"] = License{"Empty", []string{"empty", "blank"}, "", ""}
|
||||
|
||||
initApache2()
|
||||
initMit()
|
||||
|
@ -55,7 +56,6 @@ func init() {
|
|||
// getLicense returns license specified by user in flag or in config.
|
||||
// If user didn't specify the license, it returns none
|
||||
//
|
||||
// TODO: Inspect project for existing license
|
||||
func getLicense() License {
|
||||
// If explicitly flagged, use that.
|
||||
if userLicense != "" {
|
||||
|
@ -73,8 +73,8 @@ func getLicense() License {
|
|||
return findLicense(viper.GetString("license"))
|
||||
}
|
||||
|
||||
// If user didn't set any license, use none by default
|
||||
return Licenses["none"]
|
||||
// If user didn't set any license, use empty by default
|
||||
return Licenses["empty"]
|
||||
}
|
||||
|
||||
func copyrightLine() string {
|
||||
|
|
|
@ -3,6 +3,8 @@ package cmd
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"text/template"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -65,13 +67,59 @@ func (p *Project) Create() error {
|
|||
}
|
||||
|
||||
// create license
|
||||
return p.createLicenseFile()
|
||||
if p.Legal.Name != "None" {
|
||||
return p.createLicenseFile()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Project) createLicenseFile() error {
|
||||
data := map[string]interface{}{
|
||||
"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))
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -50,7 +50,7 @@ func init() {
|
|||
cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")))
|
||||
cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")))
|
||||
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
||||
viper.SetDefault("license", "none")
|
||||
viper.SetDefault("license", "empty")
|
||||
|
||||
rootCmd.AddCommand(addCmd)
|
||||
rootCmd.AddCommand(initCmd)
|
||||
|
|
Loading…
Add table
Reference in a new issue