Skip to content

Commit

Permalink
Rename and extend command 'job' to 'task'
Browse files Browse the repository at this point in the history
  • Loading branch information
2can committed Oct 6, 2023
1 parent 29ce4d9 commit c9fd59b
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 23 deletions.
69 changes: 62 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Commands:
saleor organization [command] [aliases: org]
saleor environment [command] [aliases: env]
saleor backup [command]
saleor task [command]
saleor job [command]
saleor project [command]
saleor storefront [command] [aliases: store]
Expand Down Expand Up @@ -96,6 +97,8 @@ for more information, find the documentation at https://saleor.io
* [backup show](#backup-show)
* [backup remove](#backup-remove)
* [backup restore](#backup-restore)
* [task](#task)
* [task list](#task-list)
* [job](#job)
* [job list](#job-list)
* [project](#project)
Expand Down Expand Up @@ -937,6 +940,54 @@ Options:
-h, --help Show help [boolean]
```

### task

```sh
$ saleor task --help
```

Help output:

```
saleor task [command]
Commands:
saleor task list List tasks
Options:
--json Output the data as JSON [boolean] [default: false]
--short Output data as text [boolean] [default: false]
-u, --instance, --url Saleor instance to work with [string]
-V, --version Show version number [boolean]
-h, --help Show help [boolean]
```

#### task list

```sh
$ saleor task list --help
```

Help output:

```
saleor task list
List tasks
Options:
--json Output the data as JSON [boolean] [default: false]
--short Output data as text [boolean] [default: false]
-u, --instance, --url Saleor instance to work with [string]
--env [string]
--page A page number within the paginated result set [number]
--page-size, --page_size Number of results to return per page [number]
--is-blocking, --is_blocking Filter by non/blocking tasks [boolean]
--status Filter by status: active, completed, failed, successful [string]
-V, --version Show version number [boolean]
-h, --help Show help [boolean]
```

### job

```sh
Expand All @@ -949,7 +1000,7 @@ Help output:
saleor job [command]
Commands:
saleor job list List jobs
saleor job list List tasks
Options:
--json Output the data as JSON [boolean] [default: false]
Expand All @@ -970,15 +1021,19 @@ Help output:
```
saleor job list
List jobs
List tasks
Options:
--json Output the data as JSON [boolean] [default: false]
--short Output data as text [boolean] [default: false]
-u, --instance, --url Saleor instance to work with [string]
--json Output the data as JSON [boolean] [default: false]
--short Output data as text [boolean] [default: false]
-u, --instance, --url Saleor instance to work with [string]
--env [string]
-V, --version Show version number [boolean]
-h, --help Show help [boolean]
--page A page number within the paginated result set [number]
--page-size, --page_size Number of results to return per page [number]
--is-blocking, --is_blocking Filter by non/blocking tasks [boolean]
--status Filter by status: active, completed, failed, successful [string]
-V, --version Show version number [boolean]
-h, --help Show help [boolean]
```

### project
Expand Down
5 changes: 3 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import environment from './cli/env/index.js';
import * as example from './cli/example.js';
import github from './cli/github/index.js';
import * as info from './cli/info.js';
import job from './cli/job/index.js';
import task from './cli/task/index.js';
import * as login from './cli/login.js';
import * as logout from './cli/logout.js';
import * as open from './cli/open.js';
Expand Down Expand Up @@ -123,7 +123,8 @@ const parser = yargs(hideBin(process.argv))
.command(['organization [command]', 'org'], '', organization)
.command(['environment [command]', 'env'], '', environment)
.command(['backup [command]'], '', backup)
.command(['job [command]'], '', job)
.command(['task [command]'], '', task)
.command(['job [command]'], '', task)
.command(['project [command]'], '', project)
.command(['storefront [command]', 'store'], '', storefront)
.command(['telemetry [command]', 'tele'], '', telemetry)
Expand Down
File renamed without changes.
67 changes: 59 additions & 8 deletions src/cli/job/list.ts → src/cli/task/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,86 @@ import Debug from 'debug';
import { Arguments, CommandBuilder } from 'yargs';

import { API, GET } from '../../lib/index.js';
import { formatDateTime, obfuscateArgv } from '../../lib/util.js';
import { Job, Options } from '../../types.js';
import { contentBox, formatDateTime, obfuscateArgv } from '../../lib/util.js';
import { Options, Tasks } from '../../types.js';

const parseJobName = (name: string) => {
const [_, type, env, id] = /(\w{3})-(.+)-(\w{32})/g.exec(name) || [];

return { type, env, id };
};

const debug = Debug('saleor-cli:job:list');
const debug = Debug('saleor-cli:task:list');

export const command = 'list';
export const desc = 'List jobs';
export const desc = 'List tasks';

export const builder: CommandBuilder = (_) =>
_.option('env', { type: 'string' });
_.option('env', { type: 'string' })
.option('page', {
type: 'number',
demandOption: false,
desc: 'A page number within the paginated result set',
})
.option('page-size', {
alias: 'page_size',
type: 'number',
demandOption: false,
desc: 'Number of results to return per page',
})
.option('is-blocking', {
alias: 'is_blocking',
type: 'boolean',
demandOption: false,
desc: 'Filter by non/blocking tasks',
})
.option('status', {
type: 'string',
demandOption: false,
desc: 'Filter by status: active, completed, failed, successful',
});

export const handler = async (argv: Arguments<Options>) => {
debug('command arguments: %O', obfuscateArgv(argv));
const result = (await GET(API.Job, argv)) as Job[];
const _argv = argv;

const params: string[] = [];

['page', 'page_size', 'is_blocking', 'status'].forEach((key: string) => {
if (argv[key]) {
params.push(`${key}=${argv[key]}`);
}
});

if (params.length > 0) {
_argv.params = `?${params.join('&')}`;
}

const result = (await GET(API.Task, _argv)) as Tasks;

if (argv.json) {
console.log(JSON.stringify(result, null, 2));
console.log(JSON.stringify(result.results, null, 2));
return;
}

const {
_: [name],
} = argv;

if (name === 'job') {
contentBox(
chalk(
chalk.red('DEPRECATED'),
'please use',
chalk.green('saleor task list'),
'command',
),
);
}

const { ux: cli } = CliUx;

cli.table(result, {
cli.table(result.results, {
type: {
header: 'Type',
minWidth: 2,
Expand Down
8 changes: 5 additions & 3 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import amplifyStagingConfig from '../aws-exports-staging.js'; // esl
import { ConfigMap, Options } from '../types.js';
import { Config } from './config.js';

const debug = Debug('lib:index');
const debug = Debug('saleor-cli:lib:index');

export const configs: ConfigMap = {
staging: {
Expand Down Expand Up @@ -97,8 +97,10 @@ export const API: Record<string, DefaultURLPath> = {
`organizations/${_.organization}/environments/${_.environment}/populate-database`,
ClearDatabase: (_) =>
`organizations/${_.organization}/environments/${_.environment}/clear-database`,
Job: (_) =>
`organizations/${_.organization}/environments/${_.environment}/jobs`,
Task: (_) =>
`organizations/${_.organization}/environments/${_.environment}/tasks${
_.params || ''
}`,
TaskStatus: (_) => `service/task-status/${_.task}`,
Backup: (_) =>
`organizations/${_.organization}/environments/${_.environment}/backups/${
Expand Down
12 changes: 9 additions & 3 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ import {
promptSaleorApp,
promptWebhook,
} from '../lib/util.js';
import { CreatePromptResult, Environment, Job, Options } from '../types.js';
import {
CreatePromptResult,
Environment,
Job,

Check warning on line 38 in src/middleware/index.ts

View workflow job for this annotation

GitHub Actions / lint

'Job' is defined but never used. Allowed unused vars must match /^_/u
Options,
Tasks,
} from '../types.js';

const debug = Debug('saleor-cli:middleware');

Expand Down Expand Up @@ -499,8 +505,8 @@ export const useBlockingTasksChecker = async (argv: Options) => {
);

if (blockingTasksInProgress) {
const jobs = (await GET(API.Job, argv)) as Job[];
const pending = jobs
const jobs = (await GET(API.Task, argv)) as Tasks;
const pending = jobs.results
.filter(
({ status, is_blocking: isBlocking }) =>
isBlocking && ['IN_PROGRESS', 'PENDING'].includes(status),
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Options extends BaseOptions {
saleorApiUrl?: string;
appId?: string;
permissions?: string[];
params?: string;
}

export interface CreatePromptResult {
Expand Down Expand Up @@ -131,6 +132,13 @@ export type ConfigMap = {
[key: string]: Config;
};

export interface Tasks {
count: number;
next: string | null;
previous: string | null;
results: Job[];
}

export type Job = {
job_name: string;
created_at: string;
Expand Down

0 comments on commit c9fd59b

Please sign in to comment.