Skip to content

Commit

Permalink
feat(tool): use go.mod; deprecate GOPATH
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost committed Jan 14, 2025
1 parent 7306cc8 commit bc7793d
Showing 1 changed file with 18 additions and 35 deletions.
53 changes: 18 additions & 35 deletions tool/cmd/kitex/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ type Arguments struct {
extends []*ExtraFlag
}

const cmdExample = ` # Generate client codes or update kitex_gen codes when a project is in $GOPATH:
const cmdExample = ` # Generate client codes or update kitex_gen codes when a project has go.mod:
kitex {{path/to/IDL_file.thrift}}
# Generate client codes or update kitex_gen codes when a project is not in $GOPATH:
# Generate client codes or update kitex_gen codes when a project doesn't have go.mod:
kitex -module {{github.com/xxx_org/xxx_name}} {{path/to/IDL_file.thrift}}
# Generate server codes:
Expand Down Expand Up @@ -260,46 +260,27 @@ func (a *Arguments) checkStreamX() error {
}

func (a *Arguments) checkPath(curpath string) error {
pathToGo, err := exec.LookPath("go")
if err != nil {
return err
}

gopath, err := util.GetGOPATH()
if err != nil {
return err
}
gosrc := util.JoinPath(gopath, "src")
gosrc, err = filepath.Abs(gosrc)
if err != nil {
return fmt.Errorf("get GOPATH/src path failed: %s", err.Error())
}

if strings.HasPrefix(curpath, gosrc) {
if a.PackagePrefix, err = filepath.Rel(gosrc, curpath); err != nil {
return fmt.Errorf("get GOPATH/src relpath failed: %s", err.Error())
}
a.PackagePrefix = util.JoinPath(a.PackagePrefix, a.GenPath)
} else {
if a.ModuleName == "" {
return fmt.Errorf("outside of $GOPATH. Please specify a module name with the '-module' flag")
goMod, goModPath, hasGoMod := util.SearchGoMod(curpath)
if a.ModuleName == "" {
if !hasGoMod {
return fmt.Errorf("go.mod not found. Please specify a module name with the '-module' flag")
}
a.ModuleName = goMod
}

if a.ModuleName != "" {
module, path, ok := util.SearchGoMod(curpath)
if ok {
// go.mod exists
if module != a.ModuleName {
var err error
if hasGoMod {
if goMod != a.ModuleName {
return fmt.Errorf("the module name given by the '-module' option ('%s') is not consist with the name defined in go.mod ('%s' from %s)",
a.ModuleName, module, path)
a.ModuleName, goMod, goModPath)
}
if a.PackagePrefix, err = filepath.Rel(path, curpath); err != nil {
if a.PackagePrefix, err = filepath.Rel(goModPath, curpath); err != nil {
return fmt.Errorf("get package prefix failed: %s", err.Error())
}
a.PackagePrefix = util.JoinPath(a.ModuleName, a.PackagePrefix, a.GenPath)
} else {
if err = initGoMod(pathToGo, a.ModuleName); err != nil {
if err = initGoMod(a.ModuleName); err != nil {
return fmt.Errorf("init go mod failed: %s", err.Error())
}
a.PackagePrefix = util.JoinPath(a.ModuleName, a.GenPath)
Expand Down Expand Up @@ -520,7 +501,6 @@ func LookupTool(idlType, compilerPath string) string {

path, err := exec.LookPath(tool)
if err != nil {
// log.Warnf("Failed to find %q from $PATH: %s.\nTry $GOPATH/bin/%s instead\n", path, err.Error(), tool)
gopath, er := util.GetGOPATH()
if er != nil {
return ""
Expand All @@ -530,11 +510,14 @@ func LookupTool(idlType, compilerPath string) string {
return path
}

func initGoMod(pathToGo, module string) error {
func initGoMod(module string) error {
if util.Exists("go.mod") {
return nil
}

pathToGo, err := exec.LookPath("go")
if err != nil {
return err
}
cmd := &exec.Cmd{
Path: pathToGo,
Args: []string{"go", "mod", "init", module},
Expand Down

0 comments on commit bc7793d

Please sign in to comment.