From c9fd59b82d3a43cb7b9fd1a2c44f25b5b7e31fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Wiaduch?= Date: Fri, 6 Oct 2023 16:24:57 +0200 Subject: [PATCH] Rename and extend command 'job' to 'task' --- docs/README.md | 69 ++++++++++++++++++++++++++++++---- src/cli.ts | 5 ++- src/cli/{job => task}/index.ts | 0 src/cli/{job => task}/list.ts | 67 +++++++++++++++++++++++++++++---- src/lib/index.ts | 8 ++-- src/middleware/index.ts | 12 ++++-- src/types.ts | 8 ++++ 7 files changed, 146 insertions(+), 23 deletions(-) rename src/cli/{job => task}/index.ts (100%) rename src/cli/{job => task}/list.ts (53%) diff --git a/docs/README.md b/docs/README.md index b8b3a434..f76e9b8c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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] @@ -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) @@ -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 @@ -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] @@ -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 diff --git a/src/cli.ts b/src/cli.ts index 6e092b1c..8be407e9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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'; @@ -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) diff --git a/src/cli/job/index.ts b/src/cli/task/index.ts similarity index 100% rename from src/cli/job/index.ts rename to src/cli/task/index.ts diff --git a/src/cli/job/list.ts b/src/cli/task/list.ts similarity index 53% rename from src/cli/job/list.ts rename to src/cli/task/list.ts index a44617d5..3ef68e79 100644 --- a/src/cli/job/list.ts +++ b/src/cli/task/list.ts @@ -4,8 +4,8 @@ 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) || []; @@ -13,26 +13,77 @@ const parseJobName = (name: string) => { 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) => { 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, diff --git a/src/lib/index.ts b/src/lib/index.ts index 2fddbed2..dbb4f4da 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -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: { @@ -97,8 +97,10 @@ export const API: Record = { `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/${ diff --git a/src/middleware/index.ts b/src/middleware/index.ts index 8b186550..99f089ed 100644 --- a/src/middleware/index.ts +++ b/src/middleware/index.ts @@ -32,7 +32,13 @@ import { promptSaleorApp, promptWebhook, } from '../lib/util.js'; -import { CreatePromptResult, Environment, Job, Options } from '../types.js'; +import { + CreatePromptResult, + Environment, + Job, + Options, + Tasks, +} from '../types.js'; const debug = Debug('saleor-cli:middleware'); @@ -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), diff --git a/src/types.ts b/src/types.ts index 79983787..2138a925 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,6 +30,7 @@ export interface Options extends BaseOptions { saleorApiUrl?: string; appId?: string; permissions?: string[]; + params?: string; } export interface CreatePromptResult { @@ -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;