@@ -2,9 +2,11 @@ package lifecycle
22
33import (
44 "errors"
5+ "fmt"
56 commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
67 "github.com/jfrog/jfrog-cli-core/v2/common/commands"
78 "github.com/jfrog/jfrog-cli-core/v2/common/spec"
9+ speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec"
810 coreCommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
911 "github.com/jfrog/jfrog-cli-core/v2/lifecycle"
1012 coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
@@ -24,6 +26,7 @@ import (
2426 "github.com/jfrog/jfrog-client-go/utils"
2527 "github.com/jfrog/jfrog-client-go/utils/errorutils"
2628 "github.com/urfave/cli"
29+ "os"
2730 "strings"
2831)
2932
@@ -131,21 +134,56 @@ func validateCreateReleaseBundleContext(c *cli.Context) error {
131134}
132135
133136func assertValidCreationMethod (c * cli.Context ) error {
137+ // Determine the methods provided
134138 methods := []bool {
135- c .IsSet ("spec" ), c .IsSet (cliutils .Builds ), c .IsSet (cliutils .ReleaseBundles )}
136- if coreutils .SumTrueValues (methods ) > 1 {
137- return errorutils .CheckErrorf ("exactly one creation source must be supplied: --%s, --%s or --%s.\n " +
138- "Opt to use the --%s option as the --%s and --%s are deprecated" ,
139+ c .IsSet ("spec" ),
140+ c .IsSet (cliutils .Builds ),
141+ c .IsSet (cliutils .ReleaseBundles ),
142+ }
143+ methodCount := coreutils .SumTrueValues (methods )
144+
145+ // Validate that only one creation method is provided
146+ if err := validateSingleCreationMethod (methodCount ); err != nil {
147+ return err
148+ }
149+
150+ if err := validateCreationValuesPresence (c , methodCount ); err != nil {
151+ return err
152+ }
153+ return nil
154+ }
155+
156+ func validateSingleCreationMethod (methodCount int ) error {
157+ if methodCount > 1 {
158+ return errorutils .CheckErrorf (
159+ "exactly one creation source must be supplied: --%s, --%s, or --%s.\n " +
160+ "Opt to use the --%s option as the --%s and --%s are deprecated" ,
161+ "spec" , cliutils .Builds , cliutils .ReleaseBundles ,
139162 "spec" , cliutils .Builds , cliutils .ReleaseBundles ,
140- "spec" , cliutils . Builds , cliutils . ReleaseBundles )
163+ )
141164 }
142- // If the user did not provide a source, we suggest only the recommended spec approach.
143- if coreutils .SumTrueValues (methods ) == 0 {
144- return errorutils .CheckErrorf ("the --spec option is mandatory" )
165+ return nil
166+ }
167+
168+ func validateCreationValuesPresence (c * cli.Context , methodCount int ) error {
169+ if methodCount == 0 {
170+ if ! areBuildFlagsSet (c ) && ! areBuildEnvVarsSet () {
171+ return errorutils .CheckErrorf ("Either --build-name or JFROG_CLI_BUILD_NAME, and --build-number or JFROG_CLI_BUILD_NUMBER must be defined" )
172+ }
145173 }
146174 return nil
147175}
148176
177+ // areBuildFlagsSet checks if build-name or build-number flags are set.
178+ func areBuildFlagsSet (c * cli.Context ) bool {
179+ return c .IsSet (cliutils .BuildName ) || c .IsSet (cliutils .BuildNumber )
180+ }
181+
182+ // areBuildEnvVarsSet checks if build environment variables are set.
183+ func areBuildEnvVarsSet () bool {
184+ return os .Getenv ("JFROG_CLI_BUILD_NUMBER" ) != "" && os .Getenv ("JFROG_CLI_BUILD_NAME" ) != ""
185+ }
186+
149187func create (c * cli.Context ) (err error ) {
150188 if err = validateCreateReleaseBundleContext (c ); err != nil {
151189 return err
@@ -169,10 +207,34 @@ func create(c *cli.Context) (err error) {
169207}
170208
171209func getReleaseBundleCreationSpec (c * cli.Context ) (* spec.SpecFiles , error ) {
210+ // Checking if the "builds" or "release-bundles" flags are set - if so, the spec flag should be ignored
211+ if c .IsSet (cliutils .Builds ) || c .IsSet (cliutils .ReleaseBundles ) {
212+ return nil , nil
213+ }
214+
215+ // Check if the "spec" flag is set - if so, return the spec
172216 if c .IsSet ("spec" ) {
173217 return cliutils .GetSpec (c , true , false )
174218 }
175- return nil , nil
219+
220+ // Else - create a spec from the buildName and buildnumber flags or env vars
221+ buildName := getStringFlagOrEnv (c , cliutils .BuildName , coreutils .BuildName )
222+ buildNumber := getStringFlagOrEnv (c , cliutils .BuildNumber , coreutils .BuildNumber )
223+
224+ if buildName != "" && buildNumber != "" {
225+ return speccore .CreateSpecFromBuildNameAndNumber (buildName , buildNumber )
226+ }
227+
228+ return nil , fmt .Errorf ("either the --spec flag must be provided, " +
229+ "or both --build-name and --build-number flags (or their corresponding environment variables " +
230+ "JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER) must be set" )
231+ }
232+
233+ func getStringFlagOrEnv (c * cli.Context , flag string , envVar string ) string {
234+ if c .IsSet (flag ) {
235+ return c .String (flag )
236+ }
237+ return os .Getenv (envVar )
176238}
177239
178240func promote (c * cli.Context ) error {
0 commit comments