diff --git a/src/commands/__snapshots__/branches.test.ts.snap b/src/commands/__snapshots__/branches.test.ts.snap index 6c8970c..632a845 100644 --- a/src/commands/__snapshots__/branches.test.ts.snap +++ b/src/commands/__snapshots__/branches.test.ts.snap @@ -263,4 +263,3 @@ created_at: 2019-01-01T00:00:00Z updated_at: 2019-01-01T00:00:00Z " `; - diff --git a/src/commands/branches.ts b/src/commands/branches.ts index f5b18e5..43bfdda 100644 --- a/src/commands/branches.ts +++ b/src/commands/branches.ts @@ -342,18 +342,21 @@ const create = async ( out.write(data.branch, { fields: BRANCH_FIELDS, title: 'branch', + emptyMessage: 'No branches have been found.', }); if (data.endpoints?.length > 0) { out.write(data.endpoints, { fields: ['id', 'created_at'], title: 'endpoints', + emptyMessage: 'No endpoints have been found.', }); } if (data.connection_uris?.length) { out.write(data.connection_uris, { fields: ['connection_uri'], title: 'connection_uris', + emptyMessage: 'No connection uris have been found', }); } out.end(); @@ -509,6 +512,7 @@ const restore = async ( const writeInst = writer(props).write(data.branch, { title: 'Restored branch', fields: ['id', 'name', 'last_reset_at'], + emptyMessage: 'No branches have been restored.', }); const parentId = data.branch.parent_id; if (props.preserveUnderName && parentId) { @@ -519,6 +523,7 @@ const restore = async ( writeInst.write(data.branch, { title: 'Backup branch', fields: ['id', 'name'], + emptyMessage: 'Backup branch has not been found.', }); } writeInst.end(); diff --git a/src/commands/orgs.ts b/src/commands/orgs.ts index 907d136..805c66c 100644 --- a/src/commands/orgs.ts +++ b/src/commands/orgs.ts @@ -33,6 +33,7 @@ const list = async (props: CommonProps) => { out.write(organizations, { fields: ORG_FIELDS, title: 'Organizations', + emptyMessage: 'You are not a member of any organization.', }); out.end(); }; diff --git a/src/commands/projects.ts b/src/commands/projects.ts index 3df1882..f21b7ca 100644 --- a/src/commands/projects.ts +++ b/src/commands/projects.ts @@ -169,22 +169,25 @@ const list = async (props: CommonProps & { orgId?: string }) => { return result; }; - const ownedProjects = getList(props.apiClient.listProjects); + const ownedProjects = await getList(props.apiClient.listProjects); const sharedProjects = props.orgId - ? undefined - : getList(props.apiClient.listSharedProjects); + ? [] + : await getList(props.apiClient.listSharedProjects); const out = writer(props); - out.write(await ownedProjects, { + out.write(ownedProjects, { fields: PROJECT_FIELDS, title: 'Projects', + emptyMessage: + "You don't have any projects yet. See how to create a new project:\n> neonctl projects create --help", }); - if (sharedProjects) { - out.write(await sharedProjects, { + if (!props.orgId) { + out.write(sharedProjects, { fields: PROJECT_FIELDS, title: 'Shared with you', + emptyMessage: 'No projects have been shared with you', }); } diff --git a/src/writer.ts b/src/writer.ts index 575c6ff..b174e9e 100644 --- a/src/writer.ts +++ b/src/writer.ts @@ -14,9 +14,14 @@ type WriteOutConfig = { fields: readonly OnlyStrings>[]; // Title of the output title?: string; + // Display message if data is empty + // does not apply to json and yaml output + emptyMessage?: string; }; -const writeYaml = (chunks: { data: any; config: WriteOutConfig }[]) => { +type Chunk = { data: any; config: WriteOutConfig }; + +const writeYaml = (chunks: Chunk[]) => { return YAML.stringify( chunks.length === 1 ? chunks[0].data @@ -31,7 +36,7 @@ const writeYaml = (chunks: { data: any; config: WriteOutConfig }[]) => { ); }; -const writeJson = (chunks: { data: any; config: WriteOutConfig }[]) => { +const writeJson = (chunks: Chunk[]) => { return JSON.stringify( chunks.length === 1 ? chunks[0].data @@ -52,6 +57,11 @@ const writeTable = ( ) => { chunks.forEach(({ data, config }) => { const arrayData = Array.isArray(data) ? data : [data]; + if (!arrayData.length && config.emptyMessage) { + out.write('\n' + config.emptyMessage + '\n'); + return; + } + const fields = config.fields.filter((field) => arrayData.some((item) => item[field] !== undefined && item[field] !== ''), );