diff --git a/cmd/init.go b/cmd/init.go index 062b613..a5acf28 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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", @@ -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 { @@ -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 } diff --git a/cmd/init_test.go b/cmd/init_test.go index 0461552..470d600 100644 --- a/cmd/init_test.go +++ b/cmd/init_test.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "io/ioutil" "os" @@ -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, }, } @@ -49,27 +64,30 @@ 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) @@ -77,6 +95,14 @@ func TestGoldenInitCmd(t *testing.T) { 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) + } }) } } diff --git a/cmd/licenses.go b/cmd/licenses.go index 30c7b24..b9b6132 100644 --- a/cmd/licenses.go +++ b/cmd/licenses.go @@ -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 != "" { diff --git a/cmd/project.go b/cmd/project.go index ec8980e..532ff09 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -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 { @@ -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(), }