Skip to content

Commit 43a731d

Browse files
committed
feat: allow array deletion in the values changes
1 parent 22e82c0 commit 43a731d

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

src/cmd/migrate.test.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ describe('Upgrading values', () => {
1313
teamConfig: {
1414
teamA: {
1515
services: [
16-
{ name: 'svc1', prop: 'replaceMe', bla: [{ ok: 'replaceMe' }] },
17-
{ name: 'svc1', prop: 'replaceMe', di: [{ ok: 'replaceMeNot' }] },
16+
{ name: 'svc1', prop: 'replaceMe', bla: [{ ok: 'replaceMe' }], type: 'cluster' },
17+
{ name: 'svc1', prop: 'replaceMe', di: [{ ok: 'replaceMeNot' }], type: 'public' },
1818
],
1919
},
2020
},
@@ -40,11 +40,19 @@ describe('Upgrading values', () => {
4040
],
4141
renamings: [{ 'somefile.yaml': 'newloc.yaml' }],
4242
},
43+
{
44+
version: 4,
45+
deletions: [
46+
// { 'some.k8sVersion': 'printf "v%s" .prev' },
47+
'teamConfig.{team}.services[].type',
48+
// { 'teamConfig.{team}.services[].bla[].ok': 'print .prev "ee"' },
49+
],
50+
},
4351
]
4452

4553
describe('Filter changes', () => {
4654
it('should only select changes whose version >= current version', () => {
47-
expect(filterChanges(oldVersion, mockChanges)).toEqual(mockChanges.slice(1, 3))
55+
expect(filterChanges(oldVersion, mockChanges)).toEqual(mockChanges.slice(1, 4))
4856
})
4957
})
5058
describe('Apply changes to values', () => {
@@ -68,7 +76,7 @@ describe('Upgrading values', () => {
6876
},
6977
},
7078
some: { bla: {}, k8sVersion: '1.18' },
71-
version: 3,
79+
version: 4,
7280
},
7381
true,
7482
)

src/cmd/migrate.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { randomUUID } from 'crypto'
55
import { diff } from 'deep-diff'
66
import { copy, createFileSync, move, pathExists, renameSync, rm } from 'fs-extra'
77
import { mkdir, readFile, writeFile } from 'fs/promises'
8+
import { glob } from 'glob'
89
import { cloneDeep, each, get, isObject, mapKeys, mapValues, omit, pick, pull, set, unset } from 'lodash'
910
import { basename, dirname, join } from 'path'
1011
import { prepareEnvironment } from 'src/common/cli'
@@ -20,7 +21,6 @@ import { v4 as uuidv4 } from 'uuid'
2021
import { parse } from 'yaml'
2122
import { Argv } from 'yargs'
2223
import { $, cd } from 'zx'
23-
import { glob } from 'glob'
2424
const cmdName = getFilename(__filename)
2525

2626
interface Arguments extends BasicArguments {
@@ -372,14 +372,40 @@ export const applyChanges = async (
372372

373373
export const unparsePaths = (path: string, values: Record<string, any>): Array<string> => {
374374
if (path.includes('{team}')) {
375-
const paths: Array<string> = []
375+
let paths: Array<string> = []
376376
const teams: Array<string> = Object.keys(values?.teamConfig as Record<string, any>)
377377
teams.forEach((teamName) => paths.push(path.replace('{team}', teamName)))
378+
paths = isArray(paths, values)
378379
return paths.sort()
379380
} else {
380-
return [path]
381+
const paths = isArray([path], values)
382+
return paths
381383
}
382384
}
385+
386+
function isArray(paths: string[], values: Record<string, any>): string[] {
387+
const transformedPaths: string[] = []
388+
389+
paths.forEach((path) => {
390+
const match = path.match(/^(.*)\.(\w+)\[\](.*)$/)
391+
if (!match) {
392+
transformedPaths.push(path)
393+
return
394+
}
395+
396+
const [, beforeArrayPath, arrayKey, afterArrayPath] = match
397+
398+
const objectPath = beforeArrayPath.split('.').reduce((obj, key) => obj?.[key], values)
399+
400+
if (objectPath && objectPath[arrayKey]) {
401+
objectPath[arrayKey].forEach((_item: any, index: number) => {
402+
transformedPaths.push(`${beforeArrayPath}.${arrayKey}[${index}]${afterArrayPath}`)
403+
})
404+
}
405+
})
406+
407+
return transformedPaths
408+
}
383409
export const unsetAtPath = (path: string, values: Record<string, any>): void => {
384410
const paths = unparsePaths(path, values)
385411
paths.forEach((p) => unset(values, p))

values-changes.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,4 @@ changes:
343343
- 'databases.gitea.useOtomiDB'
344344
- version: 34
345345
deletions:
346-
- 'teamConfig.{team}.services.{service}.type'
346+
- 'teamConfig.{team}.services[].type'

0 commit comments

Comments
 (0)