Skip to content

Commit 671e5da

Browse files
committed
APP-1540 - Added incremental application update support by remove-labels and add-labels flags
1 parent 5190506 commit 671e5da

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

apptrust/commands/application/application_utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@ func populateApplicationFromFlags(ctx *components.Context, descriptor *model.App
4949
descriptor.Labels = &labelsMap
5050
}
5151

52+
// Only set LabelUpdates if at least one of add-labels or remove-labels flags is set
53+
if ctx.IsFlagSet(commands.AddLabelsFlag) || ctx.IsFlagSet(commands.RemoveLabelsFlag) {
54+
labelUpdates := &model.LabelUpdates{}
55+
56+
if ctx.IsFlagSet(commands.AddLabelsFlag) {
57+
addLabels, err := utils.ParseLabelKeyValuePairs(ctx.GetStringFlagValue(commands.AddLabelsFlag))
58+
if err != nil {
59+
return err
60+
}
61+
labelUpdates.Add = addLabels
62+
}
63+
64+
if ctx.IsFlagSet(commands.RemoveLabelsFlag) {
65+
removeLabels, err := utils.ParseLabelKeyValuePairs(ctx.GetStringFlagValue(commands.RemoveLabelsFlag))
66+
if err != nil {
67+
return err
68+
}
69+
labelUpdates.Remove = removeLabels
70+
}
71+
72+
descriptor.LabelUpdates = labelUpdates
73+
}
74+
5275
if ctx.IsFlagSet(commands.UserOwnersFlag) {
5376
userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag))
5477
descriptor.UserOwners = &userOwners

apptrust/commands/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const (
3737
BusinessCriticalityFlag = "business-criticality"
3838
MaturityLevelFlag = "maturity-level"
3939
LabelsFlag = "labels"
40+
AddLabelsFlag = "add-labels"
41+
RemoveLabelsFlag = "remove-labels"
4042
UserOwnersFlag = "user-owners"
4143
GroupOwnersFlag = "group-owners"
4244
SyncFlag = "sync"
@@ -74,6 +76,8 @@ var flagsMap = map[string]components.Flag{
7476
BusinessCriticalityFlag: components.NewStringFlag(BusinessCriticalityFlag, "The business criticality level. The following values are supported: "+coreutils.ListToText(model.BusinessCriticalityValues), func(f *components.StringFlag) { f.Mandatory = false }),
7577
MaturityLevelFlag: components.NewStringFlag(MaturityLevelFlag, "The maturity level. The following values are supported: "+coreutils.ListToText(model.MaturityLevelValues), func(f *components.StringFlag) { f.Mandatory = false }),
7678
LabelsFlag: components.NewStringFlag(LabelsFlag, "List of semicolon-separated (;) labels in the form of \"key1=value1;key2=value2;...\" (wrapped by quotes).", func(f *components.StringFlag) { f.Mandatory = false }),
79+
AddLabelsFlag: components.NewStringFlag(AddLabelsFlag, "List of semicolon-separated (;) labels to add in the form of \"key1=value1;key1=value2;key2=value3;...\" (wrapped by quotes)..", func(f *components.StringFlag) { f.Mandatory = false }),
80+
RemoveLabelsFlag: components.NewStringFlag(RemoveLabelsFlag, "List of semicolon-separated (;) labels to remove in the form of \"key1=value1;key2=value2;...\" (wrapped by quotes).", func(f *components.StringFlag) { f.Mandatory = false }),
7781
UserOwnersFlag: components.NewStringFlag(UserOwnersFlag, "semicolon-separated (;) list of user owners in the form of \"user1;user2;...\" (wrapped by quotes).", func(f *components.StringFlag) { f.Mandatory = false }),
7882
GroupOwnersFlag: components.NewStringFlag(GroupOwnersFlag, "semicolon-separated (;) list of group owners in the form of \"group1;group2;...\" (wrapped by quotes).", func(f *components.StringFlag) { f.Mandatory = false }),
7983
SyncFlag: components.NewBoolFlag(SyncFlag, "Whether to synchronize the operation.", components.WithBoolDefaultValueTrue()),
@@ -204,6 +208,8 @@ var commandFlags = map[string][]string{
204208
BusinessCriticalityFlag,
205209
MaturityLevelFlag,
206210
LabelsFlag,
211+
AddLabelsFlag,
212+
RemoveLabelsFlag,
207213
UserOwnersFlag,
208214
GroupOwnersFlag,
209215
},

apptrust/commands/utils/utils.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"slices"
66
"strings"
77

8+
"github.com/jfrog/jfrog-cli-application/apptrust/model"
89
commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
910
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
1011
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
@@ -166,3 +167,27 @@ func ParseListPropertiesFlag(propertiesStr string) (map[string][]string, error)
166167

167168
return result, nil
168169
}
170+
171+
func ParseLabelKeyValuePairs(flagValue string) ([]model.LabelKeyValue, error) {
172+
if flagValue == "" {
173+
return []model.LabelKeyValue{}, nil
174+
}
175+
176+
var result []model.LabelKeyValue
177+
pairs := strings.Split(flagValue, ";")
178+
for _, pair := range pairs {
179+
trimmedPair := strings.TrimSpace(pair)
180+
if trimmedPair == "" {
181+
continue
182+
}
183+
keyValue := strings.SplitN(trimmedPair, "=", 2)
184+
if len(keyValue) != 2 {
185+
return nil, errorutils.CheckErrorf("invalid key-value pair: '%s' (expected format key=value)", pair)
186+
}
187+
result = append(result, model.LabelKeyValue{
188+
Key: strings.TrimSpace(keyValue[0]),
189+
Value: strings.TrimSpace(keyValue[1]),
190+
})
191+
}
192+
return result, nil
193+
}

apptrust/model/app_descriptor.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ var (
3030
}
3131
)
3232

33+
type LabelKeyValue struct {
34+
Key string `json:"key"`
35+
Value string `json:"value"`
36+
}
37+
38+
type LabelUpdates struct {
39+
Remove []LabelKeyValue `json:"remove,omitempty"`
40+
Add []LabelKeyValue `json:"add,omitempty"`
41+
}
42+
3343
type AppDescriptor struct {
3444
ApplicationKey string `json:"application_key"`
3545
ApplicationName string `json:"application_name,omitempty"`
@@ -38,6 +48,7 @@ type AppDescriptor struct {
3848
MaturityLevel *string `json:"maturity_level,omitempty"`
3949
BusinessCriticality *string `json:"criticality,omitempty"`
4050
Labels *map[string]string `json:"labels,omitempty"`
51+
LabelUpdates *LabelUpdates `json:"label_updates,omitempty"`
4152
UserOwners *[]string `json:"user_owners,omitempty"`
4253
GroupOwners *[]string `json:"group_owners,omitempty"`
4354
}

0 commit comments

Comments
 (0)