-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to toggle feature flags in batch.
- Loading branch information
Showing
2 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { WhitelistableFeature } from "@dust-tt/types"; | ||
import { WHITELISTABLE_FEATURES } from "@dust-tt/types"; | ||
|
||
import { FeatureFlag, Workspace } from "@app/lib/models"; | ||
import { makeScript } from "@app/scripts/helpers"; | ||
|
||
async function enableFeatureFlag( | ||
workspace: Workspace, | ||
featureFlag: WhitelistableFeature, | ||
execute: boolean | ||
) { | ||
const { id: workspaceId, name } = workspace; | ||
|
||
if (execute) { | ||
try { | ||
await FeatureFlag.create({ | ||
workspaceId, | ||
name: featureFlag as WhitelistableFeature, | ||
}); | ||
} catch (err) { | ||
console.log( | ||
`Workspace ${name}(${workspaceId}) already has ${featureFlag} enabled -- Skipping.` | ||
); | ||
|
||
return; | ||
} | ||
} | ||
|
||
console.log( | ||
`${ | ||
execute ? "" : "[DRYRUN]:" | ||
} Feature flag ${featureFlag} enabled for workspace: ${name}(${workspaceId}).` | ||
); | ||
} | ||
|
||
async function disableFeatureFlag( | ||
workspace: Workspace, | ||
featureFlag: WhitelistableFeature, | ||
execute: boolean | ||
) { | ||
const { id: workspaceId, name } = workspace; | ||
|
||
if (execute) { | ||
const existingFlag = await FeatureFlag.findOne({ | ||
where: { | ||
workspaceId, | ||
name: featureFlag, | ||
}, | ||
}); | ||
if (!existingFlag) { | ||
console.log( | ||
`Workspace ${name}(${workspaceId}) does not have ${featureFlag} enabled -- Skipping.` | ||
); | ||
return; | ||
} | ||
|
||
await existingFlag.destroy(); | ||
} | ||
|
||
console.log( | ||
`${ | ||
execute ? "" : "[DRYRUN]:" | ||
} Feature flag ${featureFlag} disabled for workspace: ${name}(${workspaceId}).` | ||
); | ||
} | ||
|
||
makeScript( | ||
{ | ||
enable: { | ||
type: "boolean", | ||
default: false, | ||
}, | ||
featureFlag: { | ||
type: "string", | ||
choices: WHITELISTABLE_FEATURES, | ||
demandOption: true, | ||
}, | ||
workspaceIds: { | ||
type: "array", | ||
demandOption: true, | ||
description: | ||
"List of workspace identifiers, separated by a space, for which the feature flag should be toggled.", | ||
}, | ||
}, | ||
async ({ enable, featureFlag, workspaceIds, execute }) => { | ||
for (const wId of workspaceIds) { | ||
const workspace = await Workspace.findOne({ | ||
where: { | ||
sId: wId, | ||
}, | ||
}); | ||
if (!workspace) { | ||
console.log(`Workspace ${wId} not found -- Skipping.`); | ||
continue; | ||
} | ||
|
||
if (enable) { | ||
await enableFeatureFlag( | ||
workspace, | ||
featureFlag as WhitelistableFeature, | ||
execute | ||
); | ||
} else { | ||
await disableFeatureFlag( | ||
workspace, | ||
featureFlag as WhitelistableFeature, | ||
execute | ||
); | ||
} | ||
} | ||
} | ||
); |