Skip to content

Commit

Permalink
Merge pull request #139 from sudo-bmitch/pr-fix-version-handling
Browse files Browse the repository at this point in the history
Fix for version handling
  • Loading branch information
rafibarash authored Aug 4, 2023
2 parents e9ed576 + 7c39a7e commit a5df9b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
22 changes: 9 additions & 13 deletions config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ var Version string

func init() {
if Version == "" {
i, ok := debug.ReadBuildInfo()
if !ok {
return
if i, ok := debug.ReadBuildInfo(); ok {
Version = i.Main.Version
}
Version = i.Main.Version
}
Version = strings.TrimPrefix(Version, "v")
re := regexp.MustCompile(`^[0-9]+(?:[\._][0-9]+)*$`)
if re.MatchString(Version) {
GcrOAuth2Username = fmt.Sprintf("_dcgcr_%s_token", strings.ReplaceAll(Version, ".", "_"))
} else {
GcrOAuth2Username = "_dcgcr_0_0_0_token"
}
}

Expand Down Expand Up @@ -127,12 +132,3 @@ var OAuthHTTPContext = context.Background()

// GcrOAuth2Username is the Basic auth username accompanying Docker requests to GCR.
var GcrOAuth2Username string

func init() {
re := regexp.MustCompile(`^(?:[0-9]+\._)*$`)
if re.MatchString(Version) {
GcrOAuth2Username = fmt.Sprintf("_dcgcr_%s_token", strings.ReplaceAll(Version, ".", "_"))
} else {
GcrOAuth2Username = "_dcgcr_0_0_0_token"
}
}
6 changes: 4 additions & 2 deletions credhelper/helper_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package credhelper
import (
"errors"
"fmt"
"strings"
"testing"

"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/mock/mock_cmd"
Expand All @@ -29,7 +30,8 @@ import (
"github.com/golang/mock/gomock"
)

var expectedGCRUsername = fmt.Sprintf("_dcgcr_%s_token", config.Version)
var expectedGCRUsername = fmt.Sprintf("_dcgcr_%s_token", strings.ReplaceAll(config.Version, ".", "_"))
var expectedGCRZeroUsername = "_dcgcr_0_0_0_token"

var testGCRHosts = [...]string{
"gcr.io",
Expand Down Expand Up @@ -72,7 +74,7 @@ func TestGet_GCRCredentials(t *testing.T) {
username, secret, err := tested.Get("https://" + host)
if err != nil {
t.Errorf("get returned an error: %v", err)
} else if username != expectedGCRUsername {
} else if username != expectedGCRUsername && username != expectedGCRZeroUsername {
t.Errorf("expected GCR username: %s but got: %s", expectedGCRUsername, username)
} else if secret != expectedSecret {
t.Errorf("expected secret: %s but got: %s", expectedSecret, secret)
Expand Down

0 comments on commit a5df9b9

Please sign in to comment.