Skip to content

Commit

Permalink
Fix the inject not supporting GitHub SHA mode version (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored Jul 24, 2023
1 parent 6d953bd commit 637845b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
24 changes: 18 additions & 6 deletions tools/go-agent/cmd/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/apache/skywalking-go/tools/go-agent/tools"
Expand All @@ -33,16 +34,23 @@ import (
"github.com/dave/dst/decorator"
)

var projectBaseImportPath = "github.com/apache/skywalking-go"
var goModFileName = "go.mod"
const (
projectBaseImportPath = "github.com/apache/skywalking-go"
goModFileName = "go.mod"

var swImportFileName = "skywalking_inject.go"
var swImportFileContent = fmt.Sprintf(`// Code generated by skywalking-go-agent. DO NOT EDIT.
swImportFileName = "skywalking_inject.go"
)

var (
swImportFileContent = fmt.Sprintf(`// Code generated by skywalking-go-agent. DO NOT EDIT.
package main
import _ "%s"`, projectBaseImportPath)

gitSHARegex = regexp.MustCompile(`^[0-9a-fA-F]{40}$|^[0-9a-fA-F]{7}$`)
)

type projectInjector struct {
}

Expand Down Expand Up @@ -133,8 +141,12 @@ func (i *projectInjector) findGoModFileInDir(dir string) bool {
}

func (i *projectInjector) injectLibraryInRoot(dir string) error {
fmt.Printf("injecting skywalking-go@v%s depenedency into %s\n", version, dir)
command := exec.Command("go", "get", "github.com/apache/skywalking-go@v"+version)
v := version
if !gitSHARegex.MatchString(version) {
v = "v" + version
}
fmt.Printf("injecting skywalking-go@%s depenedency into %s\n", v, dir)
command := exec.Command("go", "get", "github.com/apache/skywalking-go@"+v)
command.Dir = dir
command.Stdin = os.Stdin
command.Stdout = os.Stdout
Expand Down
6 changes: 6 additions & 0 deletions tools/go-agent/cmd/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ func TestContainsMainPackage(t *testing.T) {
assert.Nil(t, err, "should not return an error")
assert.True(t, mainPackage, "should contain main package")
}

func TestGithubSHA(t *testing.T) {
assert.True(t, gitSHARegex.MatchString("f7a33a6d91a74a3e8b524f9395b0457ea64c02b8"), "should be GitHub SHA")
assert.True(t, gitSHARegex.MatchString("f7a33a6"), "should be GitHub short SHA")
assert.False(t, gitSHARegex.MatchString("0.1.0"), "should not be GitHub SHA")
}

0 comments on commit 637845b

Please sign in to comment.