Skip to content
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
1 change: 1 addition & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -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,
},
}
Expand All @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/testdata/main.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions cmd/testdata/root.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down