diff --git a/cmd/licenses.go b/cmd/licenses.go index 30c7b24..4fd2821 100644 --- a/cmd/licenses.go +++ b/cmd/licenses.go @@ -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 { diff --git a/cmd/project.go b/cmd/project.go index ec8980e..08fe5b3 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -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 diff --git a/cmd/root.go b/cmd/root.go index b44c2e7..9d624bd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 ") - viper.SetDefault("license", "none") + viper.SetDefault("license", "empty") rootCmd.AddCommand(addCmd) rootCmd.AddCommand(initCmd)