Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ DESCRIPTION
SUBCOMMANDS
secrets add Adds a new secret to '~/.apify' for use in Actor
environment variables.
secrets ls Lists all secret keys stored in your local
configuration.
secrets rm Permanently deletes a secret from your stored
credentials.
```
Expand All @@ -169,6 +171,19 @@ ARGUMENTS
value Value of the secret
```

##### `apify secrets ls`

```sh
DESCRIPTION
Lists all secret keys stored in your local configuration.

USAGE
$ apify secrets ls [--json]

FLAGS
--json Format the command output as JSON
```

##### `apify secrets rm`

```sh
Expand Down
1 change: 1 addition & 0 deletions scripts/generate-cli-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const categories: Record<string, CommandsInCategory[]> = {
{ command: Commands.info },
{ command: Commands.secrets },
{ command: Commands.secretsAdd },
{ command: Commands.secretsLs },
{ command: Commands.secretsRm },
],
'actor-dev': [
Expand Down
3 changes: 2 additions & 1 deletion src/commands/secrets/_index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { LOCAL_CONFIG_PATH } from '../../lib/consts.js';
import { SecretsAddCommand } from './add.js';
import { SecretsLsCommand } from './ls.js';
import { SecretsRmCommand } from './rm.js';

export class SecretsIndexCommand extends ApifyCommand<typeof SecretsIndexCommand> {
Expand All @@ -19,7 +20,7 @@ export class SecretsIndexCommand extends ApifyCommand<typeof SecretsIndexCommand
`}\n\n` +
`When the Actor is pushed to Apify cloud, the "SECRET_ENV_VAR" and its value is stored as a secret environment variable of the Actor.`;

static override subcommands = [SecretsAddCommand, SecretsRmCommand];
static override subcommands = [SecretsAddCommand, SecretsLsCommand, SecretsRmCommand];

async run() {
this.printHelp();
Expand Down
55 changes: 55 additions & 0 deletions src/commands/secrets/ls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import chalk from 'chalk';

import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { CompactMode, ResponsiveTable } from '../../lib/commands/responsive-table.js';
import { info, simpleLog } from '../../lib/outputs.js';
import { getSecretsFile } from '../../lib/secrets.js';
import { printJsonToStdout } from '../../lib/utils.js';

const table = new ResponsiveTable({
allColumns: ['Secret Name'],
mandatoryColumns: ['Secret Name'],
columnAlignments: {
'Secret Name': 'left',
},
});

export class SecretsLsCommand extends ApifyCommand<typeof SecretsLsCommand> {
static override name = 'ls' as const;

static override description = 'Lists all secret keys stored in your local configuration.';

static override enableJsonFlag = true;

async run() {
const { json } = this.flags;

const secrets = getSecretsFile();
const secretKeys = Object.keys(secrets);

if (json) {
printJsonToStdout({ keys: secretKeys });
return;
}

if (secretKeys.length === 0) {
info({
message: "You don't have any secrets stored locally. Use 'apify secrets add' to add a secret.",
stdout: true,
});

return;
}

for (const key of secretKeys) {
table.pushRow({
'Secret Name': chalk.cyan(key),
});
}

simpleLog({
message: table.render(CompactMode.WebLikeCompact),
stdout: true,
});
}
}
64 changes: 64 additions & 0 deletions test/local/commands/secrets/ls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { SecretsAddCommand } from '../../../../src/commands/secrets/add.js';
import { SecretsLsCommand } from '../../../../src/commands/secrets/ls.js';
import { SecretsRmCommand } from '../../../../src/commands/secrets/rm.js';
import { testRunCommand } from '../../../../src/lib/command-framework/apify-command.js';
import { getSecretsFile } from '../../../../src/lib/secrets.js';

const SECRET_KEY_1 = 'testSecret1';
const SECRET_KEY_2 = 'testSecret2';
const SECRET_VALUE = 'testSecretValue';

describe('apify secrets ls', () => {
beforeAll(async () => {
// Clean up any existing test secrets
const secrets = getSecretsFile();
if (secrets[SECRET_KEY_1]) {
await testRunCommand(SecretsRmCommand, {
args_name: SECRET_KEY_1,
});
}
if (secrets[SECRET_KEY_2]) {
await testRunCommand(SecretsRmCommand, {
args_name: SECRET_KEY_2,
});
}

// Add test secrets
await testRunCommand(SecretsAddCommand, {
args_name: SECRET_KEY_1,
args_value: SECRET_VALUE,
});
await testRunCommand(SecretsAddCommand, {
args_name: SECRET_KEY_2,
args_value: SECRET_VALUE,
});
});

it('should list all secrets', async () => {
const spy = vitest.spyOn(console, 'log');

await testRunCommand(SecretsLsCommand, {});

// Verify the command outputs our test secrets
const output = spy.mock.calls.map((call) => call.join(' ')).join('\n');
expect(output).to.include(SECRET_KEY_1);
expect(output).to.include(SECRET_KEY_2);

spy.mockRestore();
});

afterAll(async () => {
// Clean up test secrets
const secrets = getSecretsFile();
if (secrets[SECRET_KEY_1]) {
await testRunCommand(SecretsRmCommand, {
args_name: SECRET_KEY_1,
});
}
if (secrets[SECRET_KEY_2]) {
await testRunCommand(SecretsRmCommand, {
args_name: SECRET_KEY_2,
});
}
});
});