refactor file|path (#1409)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2022-01-03 21:32:40 +08:00
committed by GitHub
parent 290de6aa96
commit 89ce5e492b
81 changed files with 279 additions and 245 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -57,14 +58,14 @@ func DockerCommand(c *cli.Context) (err error) {
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
if len(goFile) == 0 {
return errors.New("-go can't be empty")
}
if !util.FileExists(goFile) {
if !pathx.FileExists(goFile) {
return fmt.Errorf("file %q not found", goFile)
}
@@ -82,7 +83,7 @@ func DockerCommand(c *cli.Context) (err error) {
return err
}
projDir, ok := util.FindProjectPath(goFile)
projDir, ok := pathx.FindProjectPath(goFile)
if ok {
fmt.Printf("Hint: run \"docker build ...\" command in dir:\n %s\n", projDir)
}
@@ -129,13 +130,13 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
projPath = "."
}
out, err := util.CreateIfNotExist(dockerfileName)
out, err := pathx.CreateIfNotExist(dockerfileName)
if err != nil {
return err
}
defer out.Close()
text, err := util.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
text, err := pathx.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
if err != nil {
return err
}
@@ -151,7 +152,7 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
Chinese: offset == cstOffset,
GoRelPath: projPath,
GoFile: goFile,
ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)),
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
HasPort: port > 0,
Port: port,
Argument: builder.String(),
@@ -165,9 +166,9 @@ func getFilePath(file string) (string, error) {
return "", err
}
projPath, ok := util.FindGoModPath(filepath.Join(wd, file))
projPath, ok := pathx.FindGoModPath(filepath.Join(wd, file))
if !ok {
projPath, err = util.PathFromGoSrc()
projPath, err = pathx.PathFromGoSrc()
if err != nil {
return "", errors.New("no go.mod found, or not in GOPATH")
}

View File

@@ -1,7 +1,7 @@
package docker
import (
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -43,7 +43,7 @@ CMD ["./{{.ExeFile}}"{{.Argument}}]
// Clean deletes all templates files
func Clean() error {
return util.Clean(category)
return pathx.Clean(category)
}
// GenTemplates creates docker template files
@@ -58,7 +58,7 @@ func Category() string {
// RevertTemplate recovers the deleted template files
func RevertTemplate(name string) error {
return util.CreateTemplate(category, name, dockerTemplate)
return pathx.CreateTemplate(category, name, dockerTemplate)
}
// Update deletes and creates new template files
@@ -72,7 +72,7 @@ func Update() error {
}
func initTemplate() error {
return util.InitTemplates(category, map[string]string{
return pathx.InitTemplates(category, map[string]string{
dockerTemplateFile: dockerTemplate,
})
}