-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check if LICENSE exists. #4
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed Default License type. |
||
} | ||
|
||
func copyrightLine() string { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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\.?.*`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used Regex to find License in the project directory. |
||
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] ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirms to add a LICENSE if one exists. |
||
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] ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ask user to overwrite the LICENSE file if exists. |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default License changed. |
||
|
||
rootCmd.AddCommand(addCmd) | ||
rootCmd.AddCommand(initCmd) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New License type. Also set as the default type.
None License type used to omit License creation.