Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): go.mod parse and init command template #416

Merged
merged 2 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 24 additions & 3 deletions cli/serverless/golang/serverless.go
Expand Up @@ -2,6 +2,7 @@ package golang

import (
"bytes"
"errors"
"fmt"
"go/ast"
"go/parser"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/yomorun/yomo/cli/serverless"
"github.com/yomorun/yomo/pkg/file"
"github.com/yomorun/yomo/pkg/log"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/imports"
)
Expand Down Expand Up @@ -143,10 +145,29 @@ func (s *GolangServerless) Build(clean bool) error {
}
// go.mod
log.WarningStatusEvent(os.Stdout, "Use custom go.mod: %s", mfile)
modContent := file.GetBinContents(mfile)
if len(modContent) == 0 {
return errors.New("go.mod is empty")
}
f, err := modfile.Parse("go.mod", modContent, nil)
if err != nil {
return err
}
for _, r := range f.Replace {
if strings.HasPrefix(r.New.Path, ".") {
abs, err := filepath.Abs(r.New.Path)
if err != nil {
return err
}
modContent = bytes.Replace(modContent, []byte(r.New.Path), []byte(abs), 1)
}
}
// fmt.Println(string(modContent))
// wirte to temp go.mod
tempMod := filepath.Join(s.tempDir, "go.mod")
file.Copy(mfile, tempMod)
// source := file.GetContents(tempMod)
// log.InfoStatusEvent(os.Stdout, "go.mod: %s", source)
if err := file.PutContents(tempMod, modContent); err != nil {
return fmt.Errorf("write go.mod err %s", err)
}
// mod download
cmd := exec.Command("go", "mod", "tidy")
cmd.Env = env
Expand Down
5 changes: 3 additions & 2 deletions cli/serverless/golang/templates/init.tmpl
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/yomorun/yomo/core/frame"
"github.com/yomorun/yomo/rx"
)

Expand Down Expand Up @@ -35,6 +36,6 @@ func Handler(rxstream rx.Stream) rx.Stream {
return stream
}

func DataTags() []byte {
return []byte{0x33}
func DataTags() []frame.Tag {
return []frame.Tag{0x33}
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -19,6 +19,7 @@ require (
github.com/stretchr/testify v1.8.1
github.com/yomorun/y3 v1.0.5
golang.org/x/exp v0.0.0-20221212164502-fae10dda9338
golang.org/x/mod v0.7.0
golang.org/x/tools v0.4.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -50,7 +51,6 @@ require (
github.com/teivah/onecontext v1.3.0 // indirect
go.uber.org/goleak v1.1.11 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
Expand Down