fix: create modules

This commit is contained in:
Rogee
2024-12-20 23:35:00 +08:00
parent 0916fedfce
commit 401ed9a5bd
2 changed files with 16 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
@@ -8,6 +9,7 @@ import (
"strings" "strings"
"text/template" "text/template"
"git.ipao.vip/rogeecn/atomctl/pkg/utils/gomod"
"git.ipao.vip/rogeecn/atomctl/templates" "git.ipao.vip/rogeecn/atomctl/templates"
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -30,6 +32,16 @@ func commandNewModuleE(cmd *cobra.Command, args []string) error {
return s != "" return s != ""
}) })
pwd, err := os.Getwd()
if err != nil {
return err
}
modFile := filepath.Join(pwd, "go.mod")
if err := gomod.Parse(modFile); err != nil {
return errors.New("parse go.mod file failed")
}
modulePath := module[0] modulePath := module[0]
if len(module) > 1 { if len(module) > 1 {
modulePath = strings.Join(module, "/modules/") modulePath = strings.Join(module, "/modules/")
@@ -49,7 +61,7 @@ func commandNewModuleE(cmd *cobra.Command, args []string) error {
} }
} }
err := os.MkdirAll(modulePath, os.ModePerm) err = os.MkdirAll(modulePath, os.ModePerm)
if err != nil { if err != nil {
return err return err
} }
@@ -87,6 +99,7 @@ func commandNewModuleE(cmd *cobra.Command, args []string) error {
return tmpl.Execute(destFile, map[string]string{ return tmpl.Execute(destFile, map[string]string{
"ModuleName": moduleName, "ModuleName": moduleName,
"ProjectModule": gomod.GetModuleName(),
}) })
}) })

View File

@@ -3,7 +3,7 @@ package {{.ModuleName}}
import ( import (
"testing" "testing"
"backend/pkg/service/testx" "{{.ProjectModule}}/pkg/service/testx"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"