Skip to content

Commit 693f884

Browse files
committed
align
1 parent ba014ae commit 693f884

File tree

11 files changed

+405
-252
lines changed

11 files changed

+405
-252
lines changed

apptrust/commands/flags.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ const (
5353
BuildsFlag = "builds"
5454
ReleaseBundlesFlag = "release-bundles"
5555
SourceVersionFlag = "source-version"
56-
ExcludeFlag = "exclude"
57-
PromoteFlag = "promote"
5856
)
5957

6058
// Flag keys mapped to their corresponding components.Flag definition.
@@ -92,8 +90,6 @@ var flagsMap = map[string]components.Flag{
9290
BuildsFlag: components.NewStringFlag(BuildsFlag, "List of builds in format 'name1:number1[:timestamp1];name2:number2[:timestamp2]'", func(f *components.StringFlag) { f.Mandatory = false }),
9391
ReleaseBundlesFlag: components.NewStringFlag(ReleaseBundlesFlag, "List of release bundles in format 'name1:version1;name2:version2'", func(f *components.StringFlag) { f.Mandatory = false }),
9492
SourceVersionFlag: components.NewStringFlag(SourceVersionFlag, "Source versions in format 'app1:version1;app2:version2'", func(f *components.StringFlag) { f.Mandatory = false }),
95-
ExcludeFlag: components.NewStringFlag(ExcludeFlag, "Excluded packages in format 'package1:version1;package2:version2'", func(f *components.StringFlag) { f.Mandatory = false }),
96-
PromoteFlag: components.NewStringFlag(PromoteFlag, "Promotion stage for immediate promotion after creation", func(f *components.StringFlag) { f.Mandatory = false }),
9793
}
9894

9995
var commandFlags = map[string][]string{
@@ -111,9 +107,8 @@ var commandFlags = map[string][]string{
111107
BuildsFlag,
112108
ReleaseBundlesFlag,
113109
SourceVersionFlag,
114-
ExcludeFlag,
115-
PromoteFlag,
116110
SpecFlag,
111+
SpecVarsFlag,
117112
},
118113
VersionPromote: {
119114
url,

apptrust/commands/version/create_app_version_cmd.go

Lines changed: 94 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type createVersionSpec struct {
3333
Builds []model.CreateVersionBuild `json:"builds,omitempty"`
3434
ReleaseBundles []model.CreateVersionReleaseBundle `json:"release_bundles,omitempty"`
3535
Versions []model.CreateVersionReference `json:"versions,omitempty"`
36-
Exclude []model.ExcludePackage `json:"exclude,omitempty"`
3736
}
3837

3938
func (cv *createAppVersionCommand) Run() error {
@@ -70,67 +69,71 @@ func (cv *createAppVersionCommand) prepareAndRunCommand(ctx *components.Context)
7069
}
7170

7271
func (cv *createAppVersionCommand) buildRequestPayload(ctx *components.Context) (*model.CreateAppVersionRequest, error) {
73-
sources := &model.CreateVersionSources{}
74-
var err error
72+
var (
73+
sources *model.CreateVersionSources
74+
err error
75+
)
76+
77+
if err = validateNoSpecAndFlagsTogether(ctx); err != nil {
78+
return nil, err
79+
}
80+
7581
if ctx.IsFlagSet(commands.SpecFlag) {
76-
sources, err = cv.loadFromSpec(ctx)
77-
if errorutils.CheckError(err) != nil {
78-
return nil, err
79-
}
82+
sources, err = cv.buildSourcesFromSpec(ctx)
8083
} else {
81-
if ctx.IsFlagSet(commands.PackageNameFlag) {
82-
sources.Packages = append(sources.Packages, model.CreateVersionPackage{
83-
Type: ctx.GetStringFlagValue(commands.PackageTypeFlag),
84-
Name: ctx.GetStringFlagValue(commands.PackageNameFlag),
85-
Version: ctx.GetStringFlagValue(commands.PackageVersionFlag),
86-
Repository: ctx.GetStringFlagValue(commands.PackageRepositoryFlag),
87-
})
88-
}
89-
if buildsStr := ctx.GetStringFlagValue(commands.BuildsFlag); buildsStr != "" {
90-
builds, err := cv.parseBuilds(buildsStr)
91-
if err != nil {
92-
return nil, err
93-
}
94-
sources.Builds = builds
95-
}
96-
if rbStr := ctx.GetStringFlagValue(commands.ReleaseBundlesFlag); rbStr != "" {
97-
releaseBundles, err := cv.parseReleaseBundles(rbStr)
98-
if err != nil {
99-
return nil, err
100-
}
101-
sources.ReleaseBundles = releaseBundles
102-
}
103-
if srcVersionsStr := ctx.GetStringFlagValue(commands.SourceVersionFlag); srcVersionsStr != "" {
104-
sourceVersions, err := cv.parseSourceVersions(srcVersionsStr)
105-
if err != nil {
106-
return nil, err
107-
}
108-
sources.Versions = sourceVersions
109-
}
110-
if excludeStr := ctx.GetStringFlagValue(commands.ExcludeFlag); excludeStr != "" {
111-
excludedPackages, err := cv.parseExcludedPackages(excludeStr)
112-
if err != nil {
113-
return nil, err
114-
}
115-
sources.Exclude = excludedPackages
116-
}
84+
sources, err = cv.buildSourcesFromFlags(ctx)
11785
}
11886

119-
// Only include sources in the request if any sources were provided
120-
var sourcesPointer *model.CreateVersionSources
121-
if len(sources.Packages) > 0 || len(sources.Builds) > 0 ||
122-
len(sources.ReleaseBundles) > 0 || len(sources.Versions) > 0 || len(sources.Exclude) > 0 {
123-
sourcesPointer = sources
87+
if err != nil {
88+
return nil, err
12489
}
12590

12691
return &model.CreateAppVersionRequest{
127-
ApplicationKey: ctx.GetStringFlagValue(commands.ApplicationKeyFlag),
92+
ApplicationKey: ctx.Arguments[0],
12893
Version: ctx.Arguments[1],
129-
Sources: sourcesPointer,
94+
Sources: appendSourceIfExists(sources),
13095
Tag: ctx.GetStringFlagValue(commands.TagFlag),
13196
}, nil
13297
}
13398

99+
func (cv *createAppVersionCommand) buildSourcesFromSpec(ctx *components.Context) (*model.CreateVersionSources, error) {
100+
return cv.loadFromSpec(ctx)
101+
}
102+
103+
func (cv *createAppVersionCommand) buildSourcesFromFlags(ctx *components.Context) (*model.CreateVersionSources, error) {
104+
sources := &model.CreateVersionSources{}
105+
if ctx.IsFlagSet(commands.PackageNameFlag) {
106+
sources.Packages = append(sources.Packages, model.CreateVersionPackage{
107+
Type: ctx.GetStringFlagValue(commands.PackageTypeFlag),
108+
Name: ctx.GetStringFlagValue(commands.PackageNameFlag),
109+
Version: ctx.GetStringFlagValue(commands.PackageVersionFlag),
110+
Repository: ctx.GetStringFlagValue(commands.PackageRepositoryFlag),
111+
})
112+
}
113+
if buildsStr := ctx.GetStringFlagValue(commands.BuildsFlag); buildsStr != "" {
114+
builds, err := cv.parseBuilds(buildsStr)
115+
if err != nil {
116+
return nil, err
117+
}
118+
sources.Builds = builds
119+
}
120+
if rbStr := ctx.GetStringFlagValue(commands.ReleaseBundlesFlag); rbStr != "" {
121+
releaseBundles, err := cv.parseReleaseBundles(rbStr)
122+
if err != nil {
123+
return nil, err
124+
}
125+
sources.ReleaseBundles = releaseBundles
126+
}
127+
if srcVersionsStr := ctx.GetStringFlagValue(commands.SourceVersionFlag); srcVersionsStr != "" {
128+
sourceVersions, err := cv.parseSourceVersions(srcVersionsStr)
129+
if err != nil {
130+
return nil, err
131+
}
132+
sources.Versions = sourceVersions
133+
}
134+
return sources, nil
135+
}
136+
134137
func (cv *createAppVersionCommand) loadFromSpec(ctx *components.Context) (*model.CreateVersionSources, error) {
135138
specFilePath := ctx.GetStringFlagValue(commands.SpecFlag)
136139
spec := new(createVersionSpec)
@@ -149,12 +152,16 @@ func (cv *createAppVersionCommand) loadFromSpec(ctx *components.Context) (*model
149152
return nil, err
150153
}
151154

155+
// Validation: if all sources are empty, return error
156+
if (len(spec.Packages) == 0) && (len(spec.Builds) == 0) && (len(spec.ReleaseBundles) == 0) && (len(spec.Versions) == 0) {
157+
return nil, errorutils.CheckErrorf("Spec file is empty: must provide at least one source (packages, builds, release_bundles, or versions)")
158+
}
159+
152160
sources := &model.CreateVersionSources{
153161
Packages: spec.Packages,
154162
Builds: spec.Builds,
155163
ReleaseBundles: spec.ReleaseBundles,
156164
Versions: spec.Versions,
157-
Exclude: spec.Exclude,
158165
}
159166

160167
return sources, nil
@@ -237,30 +244,11 @@ func (cv *createAppVersionCommand) parseSourceVersions(sourceVersionsStr string)
237244
return sourceVersions, nil
238245
}
239246

240-
// Helper method to parse excluded packages string format: "name1:version1;name2:version2"
241-
func (cv *createAppVersionCommand) parseExcludedPackages(excludeStr string) ([]model.ExcludePackage, error) {
242-
var excludedPackages []model.ExcludePackage
243-
packageEntries := strings.Split(excludeStr, ";")
244-
245-
for _, entry := range packageEntries {
246-
parts := strings.Split(entry, ":")
247-
if len(parts) != 2 {
248-
return nil, errorutils.CheckErrorf("invalid exclude package format: '%s'. Expected format: name:version", entry)
249-
}
250-
251-
pkg := model.ExcludePackage{
252-
Name: parts[0],
253-
Version: parts[1],
254-
}
255-
256-
excludedPackages = append(excludedPackages, pkg)
257-
}
258-
259-
return excludedPackages, nil
260-
}
261-
262247
// Updated validation logic to support multiple source types
263248
func validateCreateAppVersionContext(ctx *components.Context) error {
249+
if err := validateNoSpecAndFlagsTogether(ctx); err != nil {
250+
return err
251+
}
264252
if len(ctx.Arguments) != 2 {
265253
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
266254
}
@@ -290,18 +278,18 @@ func GetCreateAppVersionCommand(appContext app.Context) components.Command {
290278
cmd := &createAppVersionCommand{versionService: appContext.GetVersionService()}
291279
return components.Command{
292280
Name: commands.VersionCreate,
293-
Description: "Create application version.",
281+
Description: "Creates a new version of the specified application with the given version number, tag, and associated packages, builds, release bundles, or source versions.",
294282
Category: common.CategoryVersion,
295283
Aliases: []string{"vc"},
296284
Arguments: []components.Argument{
297285
{
298-
Name: "application-key",
299-
Description: "The application key.",
286+
Name: "app-key",
287+
Description: "The application key of the application for which the version is being created.",
300288
Optional: false,
301289
},
302290
{
303291
Name: "version",
304-
Description: "The version to create (must follow SemVer format).",
292+
Description: "The version number (in SemVer format) for the new application version.",
305293
Optional: false,
306294
},
307295
},
@@ -314,3 +302,32 @@ func handleMissingPackageDetailsError() error {
314302
return errorutils.CheckErrorf("Missing packages information. Please provide the following flags --%s or the set of: --%s, --%s, --%s, --%s",
315303
commands.SpecFlag, commands.PackageTypeFlag, commands.PackageNameFlag, commands.PackageVersionFlag, commands.PackageRepositoryFlag)
316304
}
305+
306+
// Helper to return a pointer only if the object has any sources, otherwise returns nil
307+
func appendSourceIfExists(s *model.CreateVersionSources) *model.CreateVersionSources {
308+
if s == nil {
309+
return nil
310+
}
311+
if len(s.Packages) > 0 || len(s.Builds) > 0 || len(s.ReleaseBundles) > 0 || len(s.Versions) > 0 {
312+
return s
313+
}
314+
return nil
315+
}
316+
317+
// Returns error if both --spec and any other source flag are set
318+
func validateNoSpecAndFlagsTogether(ctx *components.Context) error {
319+
if ctx.IsFlagSet(commands.SpecFlag) {
320+
otherSourceFlags := []string{
321+
commands.PackageNameFlag,
322+
commands.BuildsFlag,
323+
commands.ReleaseBundlesFlag,
324+
commands.SourceVersionFlag,
325+
}
326+
for _, flag := range otherSourceFlags {
327+
if ctx.IsFlagSet(flag) {
328+
return errorutils.CheckErrorf("--spec provided: all other source flags (e.g., --%s) are not allowed.", flag)
329+
}
330+
}
331+
}
332+
return nil
333+
}

0 commit comments

Comments
 (0)