Skip to content
Myr

Fix cobra init cmd help documentation (#1108)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Adds tests for other cases where we do not expect projects to be created for absolute paths and relative paths in GOPATH.

authored by Warren Fernandes · committed by GitHub · parent 44d55fb · Browse files · 2 files+99 added−41 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+99 added−41 removed

  • other:+99 added−41 removed

2 files+99 added−41 removed

modified:cobra/cmd/init_test.go+66 added-11 removed
1package cmd23import (4 "fmt"5 "io/ioutil"6 "os"7 "path/filepath"8 "testing"910 "github.com/spf13/viper"11)1213func getProject() *Project {14 wd, _ := os.Getwd()
7 unmodified lines
22 }23}2425func TestGoldenInitCmd(t *testing.T) {23 project := getProject()24 defer os.RemoveAll(project.AbsolutePath)2626 if err := project.Create(); err != nil {27 dir, err := ioutil.TempDir("", "cobra-init")28 if err != nil {29 t.Fatal(err)30 }31 defer os.RemoveAll(dir)3230 expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}31 for _, f := range expectedFiles {32 generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)33 goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))34 err := compareFiles(generatedFile, goldenFile)35 if err != nil {36 t.Fatal(err)37 }33 tests := []struct {34 name string35 args []string36 pkgName string37 expectErr bool38 }{39 {40 name: "successfully creates a project with name",41 args: []string{"testproject"},42 pkgName: "github.com/spf13/testproject",43 expectErr: false,44 },45 {46 name: "returns error when passing an absolute path for project",47 args: []string{dir},48 pkgName: "github.com/spf13/testproject",49 expectErr: true,50 },51 {52 name: "returns error when passing an relative path for project",53 args: []string{"github.com/spf13/testproject"},54 pkgName: "github.com/spf13/testproject",55 expectErr: true,56 },57 }5859 for _, tt := range tests {60 t.Run(tt.name, func(t *testing.T) {6162 initCmd.Flags().Set("pkg-name", tt.pkgName)63 viper.Set("useViper", true)64 projectPath, err := initializeProject(tt.args)65 defer func() {66 if projectPath != "" {67 os.RemoveAll(projectPath)68 }69 }()7071 if !tt.expectErr && err != nil {72 t.Fatalf("did not expect an error, got %s", err)73 }74 if tt.expectErr {75 if err == nil {76 t.Fatal("expected an error but got none")77 } else {78 // got an expected error nothing more to do79 return80 }81 }8283 expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}84 for _, f := range expectedFiles {85 generatedFile := fmt.Sprintf("%s/%s", projectPath, f)86 goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))87 err := compareFiles(generatedFile, goldenFile)88 if err != nil {89 t.Fatal(err)90 }91 }92 })93 }94}
modified:cobra/cmd/init.go+33 added-30 removed
30 unmodified lines
31 Short: "Initialize a Cobra Application",32 Long: `Initialize (cobra init) will create a new application, with a license33and the appropriate structure for a Cobra-based CLI application.3435 * If a name is provided, it will be created in the current directory;35 * If a name is provided, a directory with that name will be created in the current directory;36 * If no name is provided, the current directory will be assumed;37 * If a relative path is provided, it will be created inside $GOPATH38 (e.g. github.com/spf13/hugo);39 * If an absolute path is provided, it will be created;40 * If the directory already exists but is empty, it will be used.37`,3842Init will not use an existing directory with contents.`,39 Run: func(_ *cobra.Command, args []string) {4044 Run: func(cmd *cobra.Command, args []string) {4546 wd, err := os.Getwd()41 projectPath, err := initializeProject(args)42 if err != nil {43 er(err)44 }5051 if len(args) > 0 {52 if args[0] != "." {53 wd = fmt.Sprintf("%s/%s", wd, args[0])54 }55 }5657 project := &Project{58 AbsolutePath: wd,59 PkgName: pkgName,60 Legal: getLicense(),61 Copyright: copyrightLine(),62 Viper: viper.GetBool("useViper"),63 AppName: path.Base(pkgName),64 }6566 if err := project.Create(); err != nil {67 er(err)68 }6970 fmt.Printf("Your Cobra application is ready at\n%s\n", project.AbsolutePath)45 fmt.Printf("Your Cobra application is ready at\n%s\n", projectPath)46 },47 }48)4950func init() {51 initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")52 initCmd.MarkFlagRequired("pkg-name")53}5455func initializeProject(args []string) (string, error) {56 wd, err := os.Getwd()57 if err != nil {58 return "", err59 }6061 if len(args) > 0 {62 if args[0] != "." {63 wd = fmt.Sprintf("%s/%s", wd, args[0])64 }65 }6667 project := &Project{68 AbsolutePath: wd,69 PkgName: pkgName,70 Legal: getLicense(),71 Copyright: copyrightLine(),72 Viper: viper.GetBool("useViper"),73 AppName: path.Base(pkgName),74 }7576 if err := project.Create(); err != nil {77 return "", err78 }7980 return project.AbsolutePath, nil81}