From b75878755c31606464392f272681507f01894ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pier=C5=9Bcionek?= Date: Mon, 5 Sep 2022 09:34:50 +0200 Subject: [PATCH] modpath --- go.mod | 4 +-- go.sum | 2 ++ pl.go | 2 +- plgo/modulewriter.go | 60 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 62131cd..bcfd1a3 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/algonode/plgo -go 1.17 +go 1.18 -require golang.org/x/sys v0.0.0-20220209214540-3681064d5158 +require golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 diff --git a/go.sum b/go.sum index 8fc3f79..fad2975 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c= golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY= +golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/pl.go b/pl.go index d98264f..edce05e 100644 --- a/pl.go +++ b/pl.go @@ -1,7 +1,7 @@ package plgo /* -#cgo CFLAGS: -I"/usr/include/postgresql/server" -fpic +#cgo CFLAGS: -I"/usr/include/postgresql/14/server" -fpic #cgo LDFLAGS: -shared //{windowsCFLAGS} diff --git a/plgo/modulewriter.go b/plgo/modulewriter.go index 3203e14..61e988f 100644 --- a/plgo/modulewriter.go +++ b/plgo/modulewriter.go @@ -13,6 +13,8 @@ import ( "os/exec" "path/filepath" "strings" + + "golang.org/x/mod/modfile" ) //ToUnexported changes Exported function name to unexported @@ -106,26 +108,70 @@ func (mw *ModuleWriter) writeUserPackage(tempPackagePath string) error { return nil } +func versionInfo(mod string) (string, error) { + gomod, err := ioutil.ReadFile("go.mod") + if os.IsNotExist(err) { + return "", fmt.Errorf("go.mod is missing. Please run go mod init") + } else if err != nil { + return "", err + } + moddata, err := modfile.Parse("go.mod", gomod, nil) + if err != nil { + return "", err + } + for _, req := range moddata.Require { + if req.Mod.Path == mod { + return req.Mod.Version, nil + } + } + return "", fmt.Errorf("Cannot find %s in go.mod", mod) +} + func readPlGoSource() ([]byte, error) { + var found string goPath := os.Getenv("GOPATH") if goPath == "" { goPath = build.Default.GOPATH // Go 1.8 and later have a default GOPATH } for _, goPathElement := range filepath.SplitList(goPath) { - matches, err := filepath.Glob(filepath.Join(goPathElement, "pkg", "mod", "github.com", "algonode", "plgo*", "pl.go")) - if err != nil || len(matches) == 0 { - continue + path := filepath.Join(goPathElement, "src", "gitlab.com", "microo8", "plgo", "pl.go") + if _, err := os.Stat(path); err == nil { + found = path + break + } + } + if found == "" { + version, err := versionInfo("gitlab.com/microo8/plgo") + if err != nil { + return nil, err } - rv, err := ioutil.ReadFile(matches[0]) + pathEnd := filepath.Join("pkg", "mod", "gitlab.com", "microo8", "plgo@"+version, "pl.go") + cache, ok := os.LookupEnv("GOMODCACHE") + if ok { + path := filepath.Join(cache, pathEnd) + if _, err := os.Stat(path); err == nil { + found = path + } + } + if found == "" { + for _, goPathElement := range filepath.SplitList(goPath) { + path := filepath.Join(goPathElement, pathEnd) + if _, err := os.Stat(path); err == nil { + found = path + break + } + } + } + } + if found != "" { + rv, err := ioutil.ReadFile(found) if err == nil { return rv, nil - } else if os.IsNotExist(err) { - continue // try the next } else { return nil, fmt.Errorf("Cannot read plgo package: %w", err) } } - return nil, fmt.Errorf("Package github.com/algonode/plgo not installed\nplease install it with: go install github.com/algonode/plgo/plgo@latest") + return nil, fmt.Errorf("Package github.com/algonode/plgo not installed\nplease install it with: go get -u github.com/algonode/plgo/plgo") } func (mw *ModuleWriter) writeplgo(tempPackagePath string) error {