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

Don't touch my licence #60

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (
)

var (
initCmd = &cobra.Command{
skipLicense = false
initCmd = &cobra.Command{
Use: "init [path]",
Aliases: []string{"initialize", "initialise", "create"},
Short: "Initialize a Cobra Application",
Expand Down Expand Up @@ -70,6 +71,7 @@ func initializeProject(args []string) (string, error) {
Copyright: copyrightLine(),
Viper: viper.GetBool("useViper"),
AppName: path.Base(modName),
SkipLicence: skipLicense,
}

if err := project.Create(); err != nil {
Expand Down Expand Up @@ -107,6 +109,10 @@ func parseModInfo() (Mod, CurDir) {
return mod, dir
}

func init() {
initCmd.Flags().BoolVarP(&skipLicense, "skipLicense", "s", false, "skip license creation")
}

type Mod struct {
Path, Dir, GoMod string
}
Expand Down
62 changes: 44 additions & 18 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -31,16 +32,30 @@ func TestGoldenInitCmd(t *testing.T) {
defer os.RemoveAll(dir)

tests := []struct {
name string
args []string
pkgName string
expectErr bool
name string
pkgName string
args []string
expectedFiles []string
unexpectedFiles []string
expectErr bool
skipLicense bool
}{
{
name: "successfully creates a project based on module",
args: []string{"testproject"},
pkgName: "github.com/spf13/testproject",
expectErr: false,
name: "successfully creates a project based on module",
args: []string{"testproject"},
pkgName: "github.com/spf13/testproject",
expectErr: false,
expectedFiles: []string{"LICENSE", "main.go", "cmd/root.go"},
skipLicense: false,
},
{
name: "successfully creates a project based on module",
args: []string{"testproject"},
pkgName: "github.com/spf13/testproject",
expectErr: false,
expectedFiles: []string{"main.go", "cmd/root.go"},
unexpectedFiles: []string{"LICENSE"},
skipLicense: true,
},
}

Expand All @@ -49,34 +64,45 @@ func TestGoldenInitCmd(t *testing.T) {

viper.Set("useViper", true)
viper.Set("license", "apache")

if tt.skipLicense {
skipLicense = true
}

projectPath, err := initializeProject(tt.args)
defer func() {
if projectPath != "" {
os.RemoveAll(projectPath)
_ = os.RemoveAll(projectPath)
}
}()

if !tt.expectErr && err != nil {
t.Fatalf("did not expect an error, got %s", err)
}
if tt.expectErr {
if err == nil {
t.Fatal("expected an error but got none")
} else {
// got an expected error nothing more to do
return
}
if tt.expectErr && err == nil {
t.Fatal("expected an error but got none")
}

expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
for _, f := range expectedFiles {
if err != nil {
t.Fatal(err)
}

for _, f := range tt.expectedFiles {
generatedFile := fmt.Sprintf("%s/%s", projectPath, f)
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
err := compareFiles(generatedFile, goldenFile)
if err != nil {
t.Fatal(err)
}
}

for _, f := range tt.unexpectedFiles {
path := fmt.Sprintf("%s/%s", projectPath, f)
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
continue
}
t.Fatalf("%s should not be generated", path)
}
})
}
}
2 changes: 0 additions & 2 deletions cmd/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,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 Down
7 changes: 6 additions & 1 deletion cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ type Project struct {
PkgName string
Copyright string
AbsolutePath string
AppName string
Legal License
Viper bool
AppName string
SkipLicence bool
}

type Command struct {
Expand Down Expand Up @@ -69,6 +70,10 @@ func (p *Project) Create() error {
}

func (p *Project) createLicenseFile() error {
if p.SkipLicence {
return nil
}

data := map[string]interface{}{
"copyright": copyrightLine(),
}
Expand Down