@@ -2,9 +2,11 @@ package lifecycle
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
6
7
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
7
8
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
9
+ speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec"
8
10
coreCommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
9
11
"github.com/jfrog/jfrog-cli-core/v2/lifecycle"
10
12
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
@@ -24,6 +26,7 @@ import (
24
26
"github.com/jfrog/jfrog-client-go/utils"
25
27
"github.com/jfrog/jfrog-client-go/utils/errorutils"
26
28
"github.com/urfave/cli"
29
+ "os"
27
30
"strings"
28
31
)
29
32
@@ -131,21 +134,56 @@ func validateCreateReleaseBundleContext(c *cli.Context) error {
131
134
}
132
135
133
136
func assertValidCreationMethod (c * cli.Context ) error {
137
+ // Determine the methods provided
134
138
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 ,
139
162
"spec" , cliutils .Builds , cliutils .ReleaseBundles ,
140
- "spec" , cliutils . Builds , cliutils . ReleaseBundles )
163
+ )
141
164
}
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
+ }
145
173
}
146
174
return nil
147
175
}
148
176
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
+
149
187
func create (c * cli.Context ) (err error ) {
150
188
if err = validateCreateReleaseBundleContext (c ); err != nil {
151
189
return err
@@ -169,10 +207,34 @@ func create(c *cli.Context) (err error) {
169
207
}
170
208
171
209
func 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
172
216
if c .IsSet ("spec" ) {
173
217
return cliutils .GetSpec (c , true , false )
174
218
}
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 )
176
238
}
177
239
178
240
func promote (c * cli.Context ) error {
0 commit comments