Skip to content

Commit

Permalink
modpath
Browse files Browse the repository at this point in the history
  • Loading branch information
urtho committed Sep 5, 2022
1 parent 245ec44 commit b758787
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion pl.go
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
60 changes: 53 additions & 7 deletions plgo/modulewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"os/exec"
"path/filepath"
"strings"

"golang.org/x/mod/modfile"
)

//ToUnexported changes Exported function name to unexported
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b758787

Please sign in to comment.