diff --git a/cmd/add_test.go b/cmd/add_test.go index 20bc34f..545b75b 100644 --- a/cmd/add_test.go +++ b/cmd/add_test.go @@ -11,6 +11,7 @@ import ( func TestGoldenAddCmd(t *testing.T) { viper.Set("useViper", true) viper.Set("license", "apache") + viper.Set("year", "2022") command := &Command{ CmdName: "test", CmdParent: parentName, diff --git a/cmd/init.go b/cmd/init.go index aeda40a..61aec1d 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -69,13 +69,15 @@ func initializeProject(args []string) (string, error) { return "", err } + var subPath string if len(args) > 0 { if args[0] != "." { wd = fmt.Sprintf("%s/%s", wd, args[0]) + subPath = args[0] } } - modName := getModImportPath() + modName := getModImportPath(subPath) project := &Project{ AbsolutePath: wd, @@ -93,9 +95,9 @@ func initializeProject(args []string) (string, error) { return project.AbsolutePath, nil } -func getModImportPath() string { +func getModImportPath(subPath string) string { mod, cd := parseModInfo() - return path.Join(mod.Path, fileToURL(strings.TrimPrefix(cd.Dir, mod.Dir))) + return path.Join(mod.Path, fileToURL(strings.TrimPrefix(cd.Dir, mod.Dir)), fileToURL(subPath)) } func fileToURL(in string) string { diff --git a/cmd/init_test.go b/cmd/init_test.go index 0461552..21d0369 100644 --- a/cmd/init_test.go +++ b/cmd/init_test.go @@ -16,8 +16,8 @@ func getProject() *Project { AbsolutePath: fmt.Sprintf("%s/testproject", wd), Legal: getLicense(), Copyright: copyrightLine(), - AppName: "cmd", - PkgName: "github.com/spf13/cobra-cli/cmd/cmd", + AppName: "testproject", + PkgName: "github.com/spf13/cobra-cli/cmd/testproject", Viper: true, } } @@ -39,7 +39,7 @@ func TestGoldenInitCmd(t *testing.T) { { name: "successfully creates a project based on module", args: []string{"testproject"}, - pkgName: "github.com/spf13/testproject", + pkgName: "github.com/spf13/cobra-cli/cmd/testproject", expectErr: false, }, } @@ -49,6 +49,7 @@ func TestGoldenInitCmd(t *testing.T) { viper.Set("useViper", true) viper.Set("license", "apache") + viper.Set("year", "2022") projectPath, err := initializeProject(tt.args) defer func() { if projectPath != "" { diff --git a/cmd/testdata/main.go.golden b/cmd/testdata/main.go.golden index 714c24f..ba4a9c7 100644 --- a/cmd/testdata/main.go.golden +++ b/cmd/testdata/main.go.golden @@ -15,7 +15,7 @@ limitations under the License. */ package main -import "github.com/spf13/cobra-cli/cmd/cmd" +import "github.com/spf13/cobra-cli/cmd/testproject/cmd" func main() { cmd.Execute() diff --git a/cmd/testdata/root.go.golden b/cmd/testdata/root.go.golden index f23bb6d..c6c3e4a 100644 --- a/cmd/testdata/root.go.golden +++ b/cmd/testdata/root.go.golden @@ -27,7 +27,7 @@ var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "cmd", + Use: "testproject", Short: "A brief description of your application", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your application. For example: @@ -56,7 +56,7 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cmd.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)") // Cobra also supports local flags, which will only run // when this action is called directly. @@ -73,10 +73,10 @@ func initConfig() { home, err := os.UserHomeDir() cobra.CheckErr(err) - // Search config in home directory with name ".cmd" (without extension). + // Search config in home directory with name ".testproject" (without extension). viper.AddConfigPath(home) viper.SetConfigType("yaml") - viper.SetConfigName(".cmd") + viper.SetConfigName(".testproject") } viper.AutomaticEnv() // read in environment variables that match