Skip to content

Commit 637845b

Browse files
authored
Fix the inject not supporting GitHub SHA mode version (#84)
1 parent 6d953bd commit 637845b

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

tools/go-agent/cmd/injector.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"os/exec"
2727
"path/filepath"
28+
"regexp"
2829
"strings"
2930

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

36-
var projectBaseImportPath = "github.com/apache/skywalking-go"
37-
var goModFileName = "go.mod"
37+
const (
38+
projectBaseImportPath = "github.com/apache/skywalking-go"
39+
goModFileName = "go.mod"
3840

39-
var swImportFileName = "skywalking_inject.go"
40-
var swImportFileContent = fmt.Sprintf(`// Code generated by skywalking-go-agent. DO NOT EDIT.
41+
swImportFileName = "skywalking_inject.go"
42+
)
43+
44+
var (
45+
swImportFileContent = fmt.Sprintf(`// Code generated by skywalking-go-agent. DO NOT EDIT.
4146
4247
package main
4348
4449
import _ "%s"`, projectBaseImportPath)
4550

51+
gitSHARegex = regexp.MustCompile(`^[0-9a-fA-F]{40}$|^[0-9a-fA-F]{7}$`)
52+
)
53+
4654
type projectInjector struct {
4755
}
4856

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

135143
func (i *projectInjector) injectLibraryInRoot(dir string) error {
136-
fmt.Printf("injecting skywalking-go@v%s depenedency into %s\n", version, dir)
137-
command := exec.Command("go", "get", "github.com/apache/skywalking-go@v"+version)
144+
v := version
145+
if !gitSHARegex.MatchString(version) {
146+
v = "v" + version
147+
}
148+
fmt.Printf("injecting skywalking-go@%s depenedency into %s\n", v, dir)
149+
command := exec.Command("go", "get", "github.com/apache/skywalking-go@"+v)
138150
command.Dir = dir
139151
command.Stdin = os.Stdin
140152
command.Stdout = os.Stdout

tools/go-agent/cmd/injector_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ func TestContainsMainPackage(t *testing.T) {
3939
assert.Nil(t, err, "should not return an error")
4040
assert.True(t, mainPackage, "should contain main package")
4141
}
42+
43+
func TestGithubSHA(t *testing.T) {
44+
assert.True(t, gitSHARegex.MatchString("f7a33a6d91a74a3e8b524f9395b0457ea64c02b8"), "should be GitHub SHA")
45+
assert.True(t, gitSHARegex.MatchString("f7a33a6"), "should be GitHub short SHA")
46+
assert.False(t, gitSHARegex.MatchString("0.1.0"), "should not be GitHub SHA")
47+
}

0 commit comments

Comments
 (0)