You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/tool/kratos-gen-project/new.go

62 lines
1.3 KiB

package main
import (
"fmt"
5 years ago
"io/ioutil"
"os"
"path/filepath"
"strings"
common "github.com/go-kratos/kratos/tool/pkg"
5 years ago
"github.com/urfave/cli/v2"
)
func runNew(ctx *cli.Context) (err error) {
if p.onlyGRPC && p.onlyHTTP {
p.onlyGRPC = false
p.onlyHTTP = false
}
if p.path != "" {
if p.path, err = filepath.Abs(p.path); err != nil {
return
}
p.path = filepath.Join(p.path, p.Name)
} else {
pwd, _ := os.Getwd()
p.path = filepath.Join(pwd, p.Name)
}
p.ModPrefix = modPath(p.path)
// creata a project
if err := create(); err != nil {
return err
}
fmt.Printf("Project: %s\n", p.Name)
fmt.Printf("OnlyGRPC: %t\n", p.onlyGRPC)
fmt.Printf("OnlyHTTP: %t\n", p.onlyHTTP)
fmt.Printf("Directory: %s\n\n", p.path)
fmt.Println("项目创建成功.")
return nil
}
func modPath(p string) string {
dir := filepath.Dir(p)
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
5 years ago
content, _ := ioutil.ReadFile(filepath.Join(dir, "go.mod"))
mod := common.RegexpReplace(`module\s+(?P<name>[\S]+)`, string(content), "$name")
name := strings.TrimPrefix(filepath.Dir(p), dir)
name = strings.TrimPrefix(name, string(os.PathSeparator))
if name == "" {
return fmt.Sprintf("%s/", mod)
}
return fmt.Sprintf("%s/%s/", mod, name)
}
parent := filepath.Dir(dir)
if dir == parent {
return ""
}
dir = parent
}
}