diff --git a/common/spec/specfiles.go b/common/spec/specfiles.go index 74efc8b88..c1e88880b 100644 --- a/common/spec/specfiles.go +++ b/common/spec/specfiles.go @@ -2,7 +2,6 @@ package spec import ( "encoding/json" - "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" "github.com/jfrog/jfrog-client-go/artifactory/services/utils" clientutils "github.com/jfrog/jfrog-client-go/utils" @@ -39,6 +38,23 @@ func CreateSpecFromFile(specFilePath string, specVars map[string]string) (spec * return } +func CreateSpecFromBuildNameAndNumber(buildName, buildNumber string) (*SpecFiles, error) { + if buildName == "" || buildNumber == "" { + return nil, errorutils.CheckErrorf("build name and build number must be provided") + } + + buildString := buildName + "/" + buildNumber + specFile := &SpecFiles{ + Files: []File{ + { + Build: buildString, + }, + }, + } + + return specFile, nil +} + type File struct { Aql utils.Aql PathMapping utils.PathMapping diff --git a/common/spec/specfiles_test.go b/common/spec/specfiles_test.go new file mode 100644 index 000000000..d3f704611 --- /dev/null +++ b/common/spec/specfiles_test.go @@ -0,0 +1,40 @@ +package spec + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCreateSpecFromBuildNameAndNumber(t *testing.T) { + t.Run("Valid Inputs", func(t *testing.T) { + spec, err := CreateSpecFromBuildNameAndNumber("Common-builds", "1.2.0") + + assert.NoError(t, err) + assert.NotNil(t, spec) + assert.Equal(t, "Common-builds/1.2.0", spec.Files[0].Build) + }) + + t.Run("Missing Build Name", func(t *testing.T) { + spec, err := CreateSpecFromBuildNameAndNumber("", "1.2.0") + + assert.Error(t, err) + assert.Nil(t, spec) + assert.EqualError(t, err, "build name and build number must be provided") + }) + + t.Run("Missing Build Number", func(t *testing.T) { + spec, err := CreateSpecFromBuildNameAndNumber("Common-builds", "") + + assert.Error(t, err) + assert.Nil(t, spec) + assert.EqualError(t, err, "build name and build number must be provided") + }) + + t.Run("Empty Build Name and Build Number", func(t *testing.T) { + spec, err := CreateSpecFromBuildNameAndNumber("", "") + + assert.Error(t, err) + assert.Nil(t, spec) + assert.EqualError(t, err, "build name and build number must be provided") + }) +}