Skip to content

Commit 37cf3fe

Browse files
committed
fix: guard segment update against no-op full PUT
segment update lacked the "nothing to update" guard its sibling commands have, so a no-flag invocation fetched the segment and re-PUT it unchanged — an audit-log entry and rule-tree overwrite for a no-op. Fail fast before any network call instead. beep boop
1 parent 1645a66 commit 37cf3fe

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

internal/cmd/cmd_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,6 +3504,18 @@ func TestSegmentCreate(t *testing.T) {
35043504
}
35053505

35063506
func TestSegmentUpdate(t *testing.T) {
3507+
t.Run("nothing to update errors without touching the segment", func(t *testing.T) {
3508+
f := flagUpdateEnv(t)
3509+
_, err := run("", "segment", "update", "us-adults")
3510+
var ue *usageError
3511+
if !errors.As(err, &ue) || !strings.Contains(err.Error(), "nothing to update") {
3512+
t.Errorf("err = %v, want a usage error", err)
3513+
}
3514+
if f.lastSegmentBody != nil {
3515+
t.Errorf("segment was PUT despite no changes: %+v", f.lastSegmentBody)
3516+
}
3517+
})
3518+
35073519
t.Run("keeps rules when only description changes", func(t *testing.T) {
35083520
f := flagUpdateEnv(t)
35093521
if _, err := run("", "segment", "update", "us-adults", "--description", "new desc"); err != nil {

internal/cmd/segment.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ var segmentUpdateCmd = &cobra.Command{
188188
flagsmith segment update beta-users --description "Updated cohort"`,
189189
Args: cobra.ExactArgs(1),
190190
RunE: func(cmd *cobra.Command, args []string) error {
191+
if !cmd.Flags().Changed("rules") && !cmd.Flags().Changed("description") && !cmd.Flags().Changed("feature") {
192+
return usageErrorf("nothing to update")
193+
}
191194
cred, projectID, err := projectScopedContext(cmd)
192195
if err != nil {
193196
return err

0 commit comments

Comments
 (0)