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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved Golang remote registry setting logic from security-cli to cli-core #1128

Merged
18 changes: 18 additions & 0 deletions artifactory/commands/golang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -328,3 +329,20 @@ func buildPackageVersionRequest(name, branchName string) string {
// No version was given to "go get" command, so the latest version should be requested
return path.Join(packageVersionRequest, "latest.info")
}

func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsRepo string) (err error) {
err = setGoProxy(serverDetails, depsRepo)
if err != nil {
err = fmt.Errorf("failed while setting Artifactory as a dependencies resolution registry: %s", err.Error())
}
return
}

func setGoProxy(server *config.ServerDetails, remoteGoRepo string) error {
repoUrl, err := goutils.GetArtifactoryRemoteRepoUrl(server, remoteGoRepo)
if err != nil {
return err
}
repoUrl += "|direct"
return os.Setenv("GOPROXY", repoUrl)
}
24 changes: 24 additions & 0 deletions artifactory/commands/golang/go_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package golang

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang"
testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -42,3 +45,24 @@ func TestGetPackageFilesPath(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedPackagePath, actualPackagePath)
}

func TestSetArtifactoryAsResolutionServer(t *testing.T) {
server := &config.ServerDetails{
Url: "http://localhost:8080/",
ArtifactoryUrl: "http://localhost:8080/artifactory/",
User: "myUser",
Password: "myPassword",
ServerId: "myServer",
}
repo := "myRepo"

// Setting the GOPROXY value to "" to ensure that the new value set in SetArtifactoryAsResolutionServer is correctly validated.
cleanup := testsutils.SetEnvWithCallbackAndAssert(t, "GOPROXY", "")
defer cleanup()

assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo))

serverUrlWithoutHttp := strings.TrimPrefix(server.ArtifactoryUrl, "http://")
expectedGoProxy := fmt.Sprintf("http://%s:%s@%sapi/go/%s|direct", server.User, server.Password, serverUrlWithoutHttp, repo)
assert.Equal(t, expectedGoProxy, os.Getenv("GOPROXY"))
}
Loading