Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
add debian support
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren committed Jul 17, 2020
1 parent 924faa3 commit e396e68
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 11 deletions.
5 changes: 4 additions & 1 deletion jfrog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/postfinance/mage/jfrog

go 1.12

require github.com/jfrog/jfrog-client-go v0.12.0
require (
github.com/jfrog/jfrog-client-go v0.12.0
github.com/stretchr/testify v1.2.2
)
67 changes: 57 additions & 10 deletions jfrog/jfrog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jfrog

import (
"errors"
"strings"

"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
Expand All @@ -9,6 +12,10 @@ import (
"github.com/jfrog/jfrog-client-go/utils/log"
)

var (
ErrWrongDebianFormat = errors.New("target has to be of the form '<destination>:<distribution>/<component>/<architecture>' for debian packages")
)

// Uploader is responsible for uploading artifacts to an artifactory repository.
type Uploader struct {
rtManager *artifactory.ArtifactoryServicesManager
Expand All @@ -32,20 +39,60 @@ func New(rtDetails auth.ServiceDetails) (*Uploader, error) {
}

// Upload uploads all sources matching srcPattern to target.
//
// If package ends with '.deb' the target has to be of the following
// form: '<destination>:<distribution>/<component>/<architecture>'
//
// The following is a valid debian target: 'local-deb-repo/pool:buster/main/amd64'.
func (u Uploader) Upload(srcPattern, target string) error {
params := services.UploadParams{
ArtifactoryCommonParams: &utils.ArtifactoryCommonParams{},
Deb: "",
Symlink: false,
ExplodeArchive: false,
Flat: false,
Retries: 1,
params, err := createParams(srcPattern, target)
if err != nil {
return err
}
params.Pattern = srcPattern
params.Target = target
_, _, _, err := u.rtManager.UploadFiles(params)

_, _, _, err = u.rtManager.UploadFiles(params)
if err != nil {
return err
}
return nil
}

func createParams(srcPattern, target string) (services.UploadParams, error) {
params := services.UploadParams{
ArtifactoryCommonParams: &utils.ArtifactoryCommonParams{
Pattern: srcPattern,
Target: target,
},
Deb: "",
Symlink: false,
ExplodeArchive: false,
Flat: false,
Retries: 1,
}

if strings.HasSuffix(srcPattern, ".deb") {
splitted := strings.Split(target, ":")

if len(splitted) != 2 {
return params, ErrWrongDebianFormat
}

count := 0

for _, item := range strings.Split(splitted[1], "/") {
count++
if item == "" {
return params, ErrWrongDebianFormat
}
}

if count != 3 {
return params, ErrWrongDebianFormat
}

params.Target = splitted[0]
params.Deb = splitted[1]
}

return params, nil
}
76 changes: 76 additions & 0 deletions jfrog/jfrog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package jfrog

import (
"testing"

"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCreateParams(t *testing.T) {
var tt = []struct {
sourcePattern string
targetString string
expectsError bool
expectedParams services.UploadParams
}{
{
"*.deb",
"repo/dir:/buster/main/amd64",
true,
services.UploadParams{},
},
{
"*.deb",
"repo/dir:/buster/main/",
true,
services.UploadParams{},
},
{
"*.deb",
"repo/dir:buster/main",
true,
services.UploadParams{},
},
{
"*.deb",
"repo/dir:buster/main/amd64",
false,
services.UploadParams{
ArtifactoryCommonParams: &utils.ArtifactoryCommonParams{
Pattern: "*.deb",
Target: "repo/dir",
},
Deb: "buster/main/amd64",
Retries: 1,
},
},
{
"*.rpm",
"repo/dir",
false,
services.UploadParams{
ArtifactoryCommonParams: &utils.ArtifactoryCommonParams{
Pattern: "*.rpm",
Target: "repo/dir",
},
Retries: 1,
},
},
}

for _, tc := range tt {
t.Run(tc.sourcePattern, func(t *testing.T) {
p, err := createParams(tc.sourcePattern, tc.targetString)
if tc.expectsError {
require.Error(t, err)
return
}

require.NoError(t, err)
assert.EqualValues(t, tc.expectedParams, p)
})
}
}

0 comments on commit e396e68

Please sign in to comment.