Skip to content

Commit 2ee5950

Browse files
committed
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into npm-login
# Conflicts: # artifactory/commands/python/python.go # artifactory/commands/python/utils_test.go # common/cliutils/utils.go
2 parents e8f884c + 59ac976 commit 2ee5950

File tree

33 files changed

+443
-797
lines changed

33 files changed

+443
-797
lines changed

artifactory/commands/dotnet/dotnetcommand.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,8 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
159159
return newWorkingDir, errorutils.CheckError(err)
160160
}
161161

162-
// Set Artifactory repo as source using the toolchain's `add source` command
163-
func (dc *DotnetCommand) AddNugetAuthToConfig(cmdType dotnet.ToolchainType, configFile *os.File, sourceUrl, user, password string) error {
164-
content := dotnet.ConfigFileTemplate
165-
_, err := configFile.WriteString(content)
166-
if err != nil {
167-
return errorutils.CheckError(err)
168-
}
169-
// We need to close the config file to let the toolchain modify it.
170-
err = configFile.Close()
171-
if err != nil {
172-
return errorutils.CheckError(err)
173-
}
174-
return addSourceToNugetConfig(cmdType, configFile.Name(), sourceUrl, user, password)
175-
}
176-
177162
// Runs nuget sources add command
178-
func addSourceToNugetConfig(cmdType dotnet.ToolchainType, configFileName, sourceUrl, user, password string) error {
163+
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, configFileName, sourceUrl, user, password string) error {
179164
cmd, err := dotnet.CreateDotnetAddSourceCmd(cmdType, sourceUrl)
180165
if err != nil {
181166
return err

artifactory/commands/golang/go.go

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"github.com/jfrog/jfrog-cli-core/v2/common/project"
1111
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1212
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
13-
goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang"
1413
"github.com/jfrog/jfrog-client-go/auth"
1514
"github.com/jfrog/jfrog-client-go/http/httpclient"
1615
rtutils "github.com/jfrog/jfrog-client-go/utils"
1716
"github.com/jfrog/jfrog-client-go/utils/errorutils"
1817
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
1918
"github.com/jfrog/jfrog-client-go/utils/log"
2019
"net/http"
20+
"net/url"
2121
"os"
2222
"path"
2323
"path/filepath"
@@ -135,7 +135,7 @@ func (gc *GoCommand) extractNoFallbackFromArgs() (cleanArgs []string, err error)
135135
}
136136

137137
func (gc *GoCommand) run() (err error) {
138-
err = goutils.LogGoVersion()
138+
err = logGoVersion()
139139
if err != nil {
140140
return
141141
}
@@ -154,7 +154,7 @@ func (gc *GoCommand) run() (err error) {
154154
return
155155
}
156156
// If noFallback=false, missing packages will be fetched directly from VCS
157-
repoUrl, err := goutils.GetArtifactoryRemoteRepoUrl(resolverDetails, gc.resolverParams.TargetRepo(), goutils.GoProxyUrlParams{Direct: !gc.noFallback})
157+
repoUrl, err := getArtifactoryRemoteRepoUrl(resolverDetails, gc.resolverParams.TargetRepo(), GoProxyUrlParams{Direct: !gc.noFallback})
158158
if err != nil {
159159
return
160160
}
@@ -328,15 +328,15 @@ func buildPackageVersionRequest(name, branchName string) string {
328328
return path.Join(packageVersionRequest, "latest.info")
329329
}
330330

331-
func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsRepo string, goProxyParams goutils.GoProxyUrlParams) (err error) {
331+
func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsRepo string, goProxyParams GoProxyUrlParams) (err error) {
332332
if err = setGoProxy(serverDetails, depsRepo, goProxyParams); err != nil {
333333
err = fmt.Errorf("failed while setting Artifactory as a dependencies resolution registry: %s", err.Error())
334334
}
335335
return
336336
}
337337

338-
func setGoProxy(server *config.ServerDetails, remoteGoRepo string, goProxyParams goutils.GoProxyUrlParams) error {
339-
repoUrl, err := goutils.GetArtifactoryRemoteRepoUrl(server, remoteGoRepo, goProxyParams)
338+
func setGoProxy(server *config.ServerDetails, remoteGoRepo string, goProxyParams GoProxyUrlParams) error {
339+
repoUrl, err := getArtifactoryRemoteRepoUrl(server, remoteGoRepo, goProxyParams)
340340
if err != nil {
341341
return err
342342
}
@@ -346,3 +346,78 @@ func setGoProxy(server *config.ServerDetails, remoteGoRepo string, goProxyParams
346346
func SetGoModCache(cacheFolder string) error {
347347
return os.Setenv("GOMODCACHE", cacheFolder)
348348
}
349+
350+
func logGoVersion() error {
351+
version, err := biutils.GetParsedGoVersion()
352+
if err != nil {
353+
return errorutils.CheckError(err)
354+
}
355+
log.Info("Using go:", version.GetVersion())
356+
return nil
357+
}
358+
359+
type GoProxyUrlParams struct {
360+
// Fallback to retrieve the modules directly from the source if
361+
// the module failed to be retrieved from the proxy.
362+
// add |direct to the end of the url.
363+
// example: https://gocenter.io|direct
364+
Direct bool
365+
// The path from baseUrl to the standard Go repository path
366+
// URL structure: <baseUrl>/<EndpointPrefix>/api/go/<repoName>
367+
EndpointPrefix string
368+
}
369+
370+
func (gdu *GoProxyUrlParams) BuildUrl(url *url.URL, repoName string) string {
371+
url.Path = path.Join(url.Path, gdu.EndpointPrefix, "api/go/", repoName)
372+
373+
return gdu.addDirect(url.String())
374+
}
375+
376+
func (gdu *GoProxyUrlParams) addDirect(url string) string {
377+
if gdu.Direct && !strings.HasSuffix(url, "|direct") {
378+
return url + "|direct"
379+
}
380+
return url
381+
}
382+
383+
func getArtifactoryRemoteRepoUrl(serverDetails *config.ServerDetails, repo string, goProxyParams GoProxyUrlParams) (string, error) {
384+
authServerDetails, err := serverDetails.CreateArtAuthConfig()
385+
if err != nil {
386+
return "", err
387+
}
388+
return getArtifactoryApiUrl(repo, authServerDetails, goProxyParams)
389+
}
390+
391+
// Gets the URL of the specified repository Go API in Artifactory.
392+
// The URL contains credentials (username and access token or password).
393+
func getArtifactoryApiUrl(repoName string, details auth.ServiceDetails, goProxyParams GoProxyUrlParams) (string, error) {
394+
rtUrl, err := url.Parse(details.GetUrl())
395+
if err != nil {
396+
return "", errorutils.CheckError(err)
397+
}
398+
399+
username := details.GetUser()
400+
password := details.GetPassword()
401+
402+
// Get credentials from access-token if exists.
403+
if details.GetAccessToken() != "" {
404+
log.Debug("Using proxy with access-token.")
405+
if username == "" {
406+
username = auth.ExtractUsernameFromAccessToken(details.GetAccessToken())
407+
}
408+
password = details.GetAccessToken()
409+
}
410+
if password != "" {
411+
rtUrl.User = url.UserPassword(username, password)
412+
}
413+
414+
return goProxyParams.BuildUrl(rtUrl, repoName), nil
415+
}
416+
417+
func GetModuleName(projectDir string) (string, error) {
418+
path, err := biutils.GetModuleNameByDir(projectDir, log.Logger)
419+
if err != nil {
420+
return "", errorutils.CheckError(err)
421+
}
422+
return path, nil
423+
}

artifactory/commands/golang/go_test.go

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package golang
22

33
import (
44
"fmt"
5+
biutils "github.com/jfrog/build-info-go/utils"
56
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
67
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
7-
goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang"
8+
"github.com/jfrog/jfrog-client-go/artifactory/auth"
89
testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
910
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"net/url"
1013
"os"
1114
"path/filepath"
1215
"strings"
@@ -34,7 +37,7 @@ func TestBuildPackageVersionRequest(t *testing.T) {
3437
}
3538

3639
func TestGetPackageFilesPath(t *testing.T) {
37-
packageCachePath, err := goutils.GetGoModCachePath()
40+
packageCachePath, err := biutils.GetGoModCachePath()
3841
assert.NoError(t, err)
3942
packageName := "github.com/golang/mock/mockgen"
4043
version := "v1.4.1"
@@ -61,7 +64,7 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) {
6164
cleanup := testsutils.SetEnvWithCallbackAndAssert(t, "GOPROXY", "")
6265
defer cleanup()
6366

64-
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, goutils.GoProxyUrlParams{Direct: true}))
67+
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, GoProxyUrlParams{Direct: true}))
6568

6669
serverUrlWithoutHttp := strings.TrimPrefix(server.ArtifactoryUrl, "http://")
6770
expectedGoProxy := fmt.Sprintf("http://%s:%s@%sapi/go/%s|direct", server.User, server.Password, serverUrlWithoutHttp, repo)
@@ -70,9 +73,95 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) {
7073
// Verify that the EndpointPrefix value is correctly added to the GOPROXY.
7174
// In this test case, the endpoint prefix is set to api/curation/audit/.
7275
// This parameter allows downloading dependencies from a custom API instead of the default one.
73-
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, goutils.GoProxyUrlParams{Direct: true, EndpointPrefix: coreutils.CurationPassThroughApi}))
76+
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, GoProxyUrlParams{Direct: true, EndpointPrefix: coreutils.CurationPassThroughApi}))
7477

7578
serverUrlWithoutHttp = strings.TrimPrefix(server.ArtifactoryUrl, "http://")
7679
expectedGoProxy = fmt.Sprintf("http://%s:%s@%sapi/curation/audit/api/go/%s|direct", server.User, server.Password, serverUrlWithoutHttp, repo)
7780
assert.Equal(t, expectedGoProxy, os.Getenv("GOPROXY"))
7881
}
82+
83+
func TestGetArtifactoryRemoteRepoUrl(t *testing.T) {
84+
server := &config.ServerDetails{
85+
ArtifactoryUrl: "https://server.com/artifactory",
86+
AccessToken: "eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA",
87+
}
88+
repoName := "test-repo"
89+
repoUrl, err := getArtifactoryRemoteRepoUrl(server, repoName, GoProxyUrlParams{})
90+
assert.NoError(t, err)
91+
assert.Equal(t, "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@server.com/artifactory/api/go/test-repo", repoUrl)
92+
}
93+
94+
func TestGetArtifactoryApiUrl(t *testing.T) {
95+
details := auth.NewArtifactoryDetails()
96+
details.SetUrl("https://test.com/artifactory/")
97+
98+
// Test username and password
99+
details.SetUser("frog")
100+
details.SetPassword("passfrog")
101+
url, err := getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{})
102+
assert.NoError(t, err)
103+
assert.Equal(t, "https://frog:[email protected]/artifactory/api/go/test-repo", url)
104+
105+
// Test username and password with EndpointPrefix and direct
106+
details.SetUser("frog")
107+
details.SetPassword("passfrog")
108+
url, err = getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{EndpointPrefix: "test", Direct: true})
109+
assert.NoError(t, err)
110+
assert.Equal(t, "https://frog:[email protected]/artifactory/test/api/go/test-repo|direct", url)
111+
112+
// Test access token
113+
// Set fake access token with username "test"
114+
details.SetUser("")
115+
details.SetAccessToken("eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA")
116+
url, err = getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{})
117+
assert.NoError(t, err)
118+
assert.Equal(t, "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@test.com/artifactory/api/go/test-repo", url)
119+
120+
// Test access token with username
121+
// Set fake access token with username "test"
122+
// Expect username to be "frog"
123+
details.SetUser("frog")
124+
details.SetAccessToken("eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA")
125+
url, err = getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{})
126+
assert.NoError(t, err)
127+
assert.Equal(t, "https://frog:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@test.com/artifactory/api/go/test-repo", url)
128+
}
129+
130+
func TestGoProxyUrlParams_BuildUrl(t *testing.T) {
131+
tests := []struct {
132+
name string
133+
RepoName string
134+
Direct bool
135+
EndpointPrefix string
136+
ExpectedUrl string
137+
}{
138+
{
139+
name: "Url Without direct or Prefix",
140+
RepoName: "go",
141+
ExpectedUrl: "https://test/api/go/go",
142+
},
143+
{
144+
name: "Url With direct",
145+
RepoName: "go",
146+
Direct: true,
147+
ExpectedUrl: "https://test/api/go/go|direct",
148+
},
149+
{
150+
name: "Url With Prefix",
151+
RepoName: "go",
152+
EndpointPrefix: "prefix",
153+
ExpectedUrl: "https://test/prefix/api/go/go",
154+
},
155+
}
156+
for _, tt := range tests {
157+
t.Run(tt.name, func(t *testing.T) {
158+
remoteUrl, err := url.Parse("https://test")
159+
require.NoError(t, err)
160+
gdu := &GoProxyUrlParams{
161+
Direct: tt.Direct,
162+
EndpointPrefix: tt.EndpointPrefix,
163+
}
164+
assert.Equalf(t, tt.ExpectedUrl, gdu.BuildUrl(remoteUrl, tt.RepoName), "BuildUrl(%v, %v)", remoteUrl, tt.RepoName)
165+
})
166+
}
167+
}

artifactory/commands/golang/gopublish.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
99
buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build"
1010
"github.com/jfrog/jfrog-cli-core/v2/common/project"
11-
goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang"
1211
clientutils "github.com/jfrog/jfrog-client-go/utils"
1312
"github.com/jfrog/jfrog-client-go/utils/errorutils"
1413
)
@@ -58,7 +57,7 @@ func (gpc *GoPublishCommand) Run() error {
5857
return err
5958
}
6059

61-
err = goutils.LogGoVersion()
60+
err = logGoVersion()
6261
if err != nil {
6362
return err
6463
}

artifactory/commands/golang/publish.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
buildinfo "github.com/jfrog/build-info-go/entities"
10+
"github.com/jfrog/build-info-go/utils"
1011
"github.com/jfrog/gofrog/crypto"
1112
"github.com/jfrog/gofrog/version"
1213
"io"
@@ -17,7 +18,6 @@ import (
1718
"time"
1819

1920
"github.com/jfrog/jfrog-cli-core/v2/common/build"
20-
goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang"
2121
"github.com/jfrog/jfrog-client-go/artifactory"
2222
_go "github.com/jfrog/jfrog-client-go/artifactory/services/go"
2323
servicesutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
@@ -28,13 +28,13 @@ import (
2828

2929
// Publish go project to Artifactory.
3030
func publishPackage(packageVersion, targetRepo, buildName, buildNumber, projectKey string, excludedPatterns []string, servicesManager artifactory.ArtifactoryServicesManager) (summary *servicesutils.OperationSummary, artifacts []buildinfo.Artifact, err error) {
31-
projectPath, err := goutils.GetProjectRoot()
31+
projectPath, err := getProjectRoot()
3232
if err != nil {
3333
return nil, nil, errorutils.CheckError(err)
3434
}
3535

3636
// Read module name
37-
moduleName, err := goutils.GetModuleName(projectPath)
37+
moduleName, err := GetModuleName(projectPath)
3838
if err != nil {
3939
return nil, nil, err
4040
}
@@ -238,3 +238,11 @@ type goInfo struct {
238238
Version string `json:"Version"`
239239
Time string `json:"Time"`
240240
}
241+
242+
func getProjectRoot() (string, error) {
243+
path, err := utils.GetProjectRoot()
244+
if err != nil {
245+
return "", errorutils.CheckError(err)
246+
}
247+
return path, nil
248+
}

0 commit comments

Comments
 (0)