diff --git a/common/spec/specfiles.go b/common/spec/specfiles.go index 74efc8b88..b44ac3906 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 CreateSpecFromVariables(buildName string, 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..170a83770 --- /dev/null +++ b/common/spec/specfiles_test.go @@ -0,0 +1,40 @@ +package spec + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCreateSpecFromVariables(t *testing.T) { + t.Run("Valid Inputs", func(t *testing.T) { + spec, err := CreateSpecFromVariables("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 := CreateSpecFromVariables("", "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 := CreateSpecFromVariables("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 := CreateSpecFromVariables("", "") + + assert.Error(t, err) + assert.Nil(t, spec) + assert.EqualError(t, err, "build name and build number must be provided") + }) +}