Skip to content

Commit

Permalink
feat: add wgc feature-flag list command (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
JivusAyrus authored Aug 22, 2024
1 parent 9d7812a commit 3800237
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
88 changes: 88 additions & 0 deletions cli/src/commands/feature-flag/commands/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { writeFile } from 'node:fs/promises';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { Command, program } from 'commander';
import pc from 'picocolors';
import Table from 'cli-table3';
import { joinLabel } from '@wundergraph/cosmo-shared';
import { resolve } from 'pathe';
import { getBaseHeaders } from '../../../core/config.js';
import { BaseCommandOptions } from '../../../core/types/types.js';

type OutputFile = {
name: string;
namespace: string;
labels: string[];
isEnabled: boolean;
lastUpdatedAt: string;
}[];

export default (opts: BaseCommandOptions) => {
const command = new Command('list');
command.description('Lists the feature flags.');
command.option(
'-n, --namespace [string]',
'The namespace of the feature flags. If not provided, it will list all feature flags.',
);
command.option('-o, --out [string]', 'Destination file for the json output.');
command.option('-j, --json', 'Prints to the console in json format instead of table');
command.action(async (options) => {
const resp = await opts.client.platform.getFeatureFlags(
{
namespace: options.namespace,
limit: 0,
offset: 0,
},
{
headers: getBaseHeaders(),
},
);

if (resp.response?.code !== EnumStatusCode.OK) {
program.error(pc.red(`Could not fetch feature flags. ${resp.response?.details ?? ''}`));
}

if (options.out) {
const output = resp.featureFlags.map(
(f) =>
({
name: f.name,
labels: f.labels.map((l) => joinLabel(l)),
isEnabled: f.isEnabled,
lastUpdatedAt: f.updatedAt,
namespace: f.namespace,
}) as OutputFile[number],
);
await writeFile(resolve(options.out), JSON.stringify(output));
return;
}
if (options.json) {
console.log(JSON.stringify(resp.featureFlags));
return;
}

const featureFlagsTable = new Table({
head: [
pc.bold(pc.white('NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('LABELS')),
pc.bold(pc.white('ENABLED')),
pc.bold(pc.white('UPDATED_AT')),
],
colWidths: [20, 20, 30, 15, 30],
wordWrap: true,
});

for (const ff of resp.featureFlags) {
featureFlagsTable.push([
ff.name,
ff.namespace,
ff.labels.map((l) => joinLabel(l)).join(', ') || '-',
ff.isEnabled,
ff.updatedAt || '-',
]);
}
console.log(featureFlagsTable.toString());
});

return command;
};
2 changes: 2 additions & 0 deletions cli/src/commands/feature-flag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DeleteFeatureFlagCommand from './commands/delete.js';
import EnableFeatureFlagCommand from './commands/enable.js';
import DisableFeatureFlagCommand from './commands/disable.js';
import UpdateFeatureFlagCommand from './commands/update.js';
import ListFeatureFlagCommand from './commands/list.js';

export default (opts: BaseCommandOptions) => {
const command = new Command('feature-flag').alias('ff');
Expand All @@ -16,6 +17,7 @@ export default (opts: BaseCommandOptions) => {
command.addCommand(EnableFeatureFlagCommand(opts));
command.addCommand(DisableFeatureFlagCommand(opts));
command.addCommand(UpdateFeatureFlagCommand(opts));
command.addCommand(ListFeatureFlagCommand(opts));

command.hook('preAction', async () => {
await checkAuth();
Expand Down

0 comments on commit 3800237

Please sign in to comment.