Skip to content

Commit

Permalink
Improve repositores code
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sverdlov <[email protected]>
  • Loading branch information
sverdlov93 committed Nov 7, 2024
1 parent 14fe886 commit d345a0d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
17 changes: 1 addition & 16 deletions artifactory/commands/dotnet/dotnetcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,8 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
return newWorkingDir, errorutils.CheckError(err)
}

// Set Artifactory repo as source using the toolchain's `add source` command
func (dc *DotnetCommand) AddNugetAuthToConfig(cmdType dotnet.ToolchainType, configFile *os.File, sourceUrl, user, password string) error {
content := dotnet.ConfigFileTemplate
_, err := configFile.WriteString(content)
if err != nil {
return errorutils.CheckError(err)
}
// We need to close the config file to let the toolchain modify it.
err = configFile.Close()
if err != nil {
return errorutils.CheckError(err)
}
return addSourceToNugetConfig(cmdType, configFile.Name(), sourceUrl, user, password)
}

// Runs nuget sources add command
func addSourceToNugetConfig(cmdType dotnet.ToolchainType, configFileName, sourceUrl, user, password string) error {
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, configFileName, sourceUrl, user, password string) error {
cmd, err := dotnet.CreateDotnetAddSourceCmd(cmdType, sourceUrl)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion artifactory/utils/container/containermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (versionCmd *VersionCmd) RunCmd() (string, error) {

func ValidateClientApiVersion() error {
cmd := &VersionCmd{}
// 'docker version' may return 1 in case of errors from daemon. We should ignore this kind of errors.
// 'docker version' may return 1 in case of errors from daemon. We should ignore this kind of error.
content, err := cmd.RunCmd()
content = strings.TrimSpace(content)
if !ApiVersionRegex.Match([]byte(content)) {
Expand Down
20 changes: 10 additions & 10 deletions artifactory/utils/container/localagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
)

// Build-info builder for local agents tools such as: Docker or Podman.
type localAgentbuildInfoBuilder struct {
type localAgentBuildInfoBuilder struct {
buildInfoBuilder *buildInfoBuilder
// Name of the container CLI tool e.g. docker
containerManager ContainerManager
commandType CommandType
}

// Create new build info builder container CLI tool
func NewLocalAgentBuildInfoBuilder(image *Image, repository, buildName, buildNumber, project string, serviceManager artifactory.ArtifactoryServicesManager, commandType CommandType, containerManager ContainerManager) (*localAgentbuildInfoBuilder, error) {
func NewLocalAgentBuildInfoBuilder(image *Image, repository, buildName, buildNumber, project string, serviceManager artifactory.ArtifactoryServicesManager, commandType CommandType, containerManager ContainerManager) (*localAgentBuildInfoBuilder, error) {
imageSha2, err := containerManager.Id(image)
if err != nil {
return nil, err
Expand All @@ -32,23 +32,23 @@ func NewLocalAgentBuildInfoBuilder(image *Image, repository, buildName, buildNum
return nil, err
}
builder.setImageSha2(imageSha2)
return &localAgentbuildInfoBuilder{
return &localAgentBuildInfoBuilder{
buildInfoBuilder: builder,
containerManager: containerManager,
commandType: commandType,
}, err
}

func (labib *localAgentbuildInfoBuilder) GetLayers() *[]utils.ResultItem {
func (labib *localAgentBuildInfoBuilder) GetLayers() *[]utils.ResultItem {
return &labib.buildInfoBuilder.imageLayers
}

func (labib *localAgentbuildInfoBuilder) SetSkipTaggingLayers(skipTaggingLayers bool) {
func (labib *localAgentBuildInfoBuilder) SetSkipTaggingLayers(skipTaggingLayers bool) {
labib.buildInfoBuilder.skipTaggingLayers = skipTaggingLayers
}

// Create build-info for a docker image.
func (labib *localAgentbuildInfoBuilder) Build(module string) (*buildinfo.BuildInfo, error) {
func (labib *localAgentBuildInfoBuilder) Build(module string) (*buildinfo.BuildInfo, error) {
// Search for image build-info.
candidateLayers, manifest, err := labib.searchImage()
if err != nil {
Expand All @@ -63,7 +63,7 @@ func (labib *localAgentbuildInfoBuilder) Build(module string) (*buildinfo.BuildI
}

// Search an image in Artifactory and validate its sha2 with local image.
func (labib *localAgentbuildInfoBuilder) searchImage() (map[string]*utils.ResultItem, *manifest, error) {
func (labib *localAgentBuildInfoBuilder) searchImage() (map[string]*utils.ResultItem, *manifest, error) {
longImageName, err := labib.buildInfoBuilder.image.GetImageLongNameWithTag()
if err != nil {
return nil, nil, err
Expand All @@ -90,7 +90,7 @@ func (labib *localAgentbuildInfoBuilder) searchImage() (map[string]*utils.Result

// Search image layers in artifactory by the provided image path in artifactory.
// If fat-manifest is found, use it to find our image in Artifactory.
func (labib *localAgentbuildInfoBuilder) search(imagePathPattern string) (resultMap map[string]*utils.ResultItem, err error) {
func (labib *localAgentBuildInfoBuilder) search(imagePathPattern string) (resultMap map[string]*utils.ResultItem, err error) {
resultMap, err = performSearch(imagePathPattern, labib.buildInfoBuilder.serviceManager)
if err != nil {
log.Debug("Failed to search marker layer. Error:", err.Error())
Expand Down Expand Up @@ -130,15 +130,15 @@ func (labib *localAgentbuildInfoBuilder) search(imagePathPattern string) (result
}

// Verify manifest by comparing sha256, which references to the image digest. If there is no match, return nil.
func (labib *localAgentbuildInfoBuilder) isVerifiedManifest(imageManifest *manifest) bool {
func (labib *localAgentBuildInfoBuilder) isVerifiedManifest(imageManifest *manifest) bool {
if imageManifest.Config.Digest != labib.buildInfoBuilder.imageSha2 {
log.Debug(`Found incorrect manifest.json file. Expects digest "` + labib.buildInfoBuilder.imageSha2 + `" found "` + imageManifest.Config.Digest)
return false
}
return true
}

func (labib *localAgentbuildInfoBuilder) getImageDigestFromFatManifest(fatManifest utils.ResultItem) (string, error) {
func (labib *localAgentBuildInfoBuilder) getImageDigestFromFatManifest(fatManifest utils.ResultItem) (string, error) {
var fatManifestContent *FatManifest
if err := downloadLayer(fatManifest, &fatManifestContent, labib.buildInfoBuilder.serviceManager, labib.buildInfoBuilder.repositoryDetails.key); err != nil {
log.Debug(`failed to unmarshal fat-manifest`)
Expand Down

0 comments on commit d345a0d

Please sign in to comment.