Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}, "", ""}
Copy link
Author

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.


initApache2()
initMit()
Expand All @@ -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 != "" {
Expand All @@ -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"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed Default License type.

}

func copyrightLine() string {
Expand Down
50 changes: 49 additions & 1 deletion cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"regexp"
"text/template"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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\.?.*`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used Regex to find License in the project directory.
Regex: Should contain the word license and should not have an extension / should be a .txt or .md

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] ")
Copy link
Author

Choose a reason for hiding this comment

The 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] ")
Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default License changed.


rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(initCmd)
Expand Down