Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snapshots option to cli pull command #732

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/long-clocks-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@openfn/cli': minor
'@openfn/deploy': minor
---

Add snapshots option to cli pull command
9 changes: 9 additions & 0 deletions packages/cli/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type Opts = {
repoDir?: string;
sanitize: 'none' | 'remove' | 'summarize' | 'obfuscate';
skipAdaptorValidation?: boolean;
snapshots?: string[];
specifier?: string; // docgen
start?: string; // workflow start step
statePath?: string;
Expand Down Expand Up @@ -402,6 +403,14 @@ export const skipAdaptorValidation: CLIOption = {
},
};

export const snapshots: CLIOption = {
name: 'snapshots',
yargs: {
description: 'List of snapshot ids to pull',
array: true,
},
};

export const statePath: CLIOption = {
name: 'state-path',
yargs: {
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/pull/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ export type PullOptions = Required<
| 'configPath'
| 'projectId'
| 'confirm'
| 'snapshots'
>
>;

const options = [o.statePath, o.projectPath, o.configPath, o.log, o.logJson];
const options = [
o.statePath,
o.projectPath,
o.configPath,
o.log,
o.logJson,
o.snapshots,
];

const pullCommand: yargs.CommandModule<PullOptions> = {
command: 'pull [projectId]',
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/pull/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ async function pullHandler(options: PullOptions, logger: Logger) {
);

// Get the project.json from Lightning
const { data: project } = await getProject(config, options.projectId);
const { data: project } = await getProject(
config,
options.projectId,
options.snapshots
);

if (!project) {
logger.error('ERROR: Project not found.');
Expand Down
19 changes: 15 additions & 4 deletions packages/deploy/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { DeployConfig, ProjectPayload } from './types';
import { DeployError } from './deployError';

const getLightningUrl = (config: DeployConfig, path: string = '') =>
new URL(`/api/provision/${path}`, config.endpoint);
export const getLightningUrl = (
config: DeployConfig,
path: string = '',
snapshots?: string[]
) => {
const params = new URLSearchParams();
snapshots?.forEach((snapshot) => params.append('snapshots[]', snapshot));
return new URL(
`/api/provision/${path}?${params.toString()}`,
config.endpoint
);
};

export async function getProject(
config: DeployConfig,
projectId: string
projectId: string,
snapshots?: string[]
): Promise<{ data: ProjectPayload | null }> {
const url = getLightningUrl(config, projectId);
const url = getLightningUrl(config, projectId, snapshots);
console.log(`Checking ${url} for existing project.`);

try {
Expand Down
36 changes: 36 additions & 0 deletions packages/deploy/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { render } from '@inquirer/testing';
import { input } from '@inquirer/prompts';
import test from 'ava';
import { DeployConfig } from '../src/types';
import { getLightningUrl } from '../src/client';

test('renders a confirmation', async (t) => {
const { answer, events, getScreen } = await render(input, {
Expand All @@ -14,3 +16,37 @@ test('renders a confirmation', async (t) => {

t.is(await answer, 'John');
});

test('getLightningUrl adds snapshots correctly to the URL', async (t) => {
const config: DeployConfig = {
endpoint: 'http://localhost',
apiKey: 'test-api-key',
specPath: './project.yaml',
statePath: './state.json',
requireConfirmation: false,
dryRun: false,
};

const projectId = 'test-project';
const snapshots = ['snapshot1', 'snapshot2'];

const expectedUrl =
'http://localhost/api/provision/test-project?snapshots%5B%5D=snapshot1&snapshots%5B%5D=snapshot2';
t.is(getLightningUrl(config, projectId, snapshots).toString(), expectedUrl);
});

test('getLightningUrl returns the correct URL when no snapshot is provided', async (t) => {
const config: DeployConfig = {
endpoint: 'http://localhost',
apiKey: 'test-api-key',
specPath: './project.yaml',
statePath: './state.json',
requireConfirmation: false,
dryRun: false,
};

const projectId = 'test-project';

const expectedUrl = 'http://localhost/api/provision/test-project?';
t.is(getLightningUrl(config, projectId).toString(), expectedUrl);
});