Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from teamdigitale/fix-cci-build
Browse files Browse the repository at this point in the history
Fix build on circle CI
  • Loading branch information
cloudify authored Jun 20, 2019
2 parents 7343177 + 5ce36b5 commit c81d960
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 82 deletions.
35 changes: 17 additions & 18 deletions src/commands/hello.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import {Command, flags} from '@oclif/command'
import { Command, flags } from "@oclif/command";

export default class Hello extends Command {
static description = 'describe the command here'
public static description = "describe the command here";

static examples = [
// tslint:disable-next-line: readonly-array
public static examples = [
`$ io-ops hello
hello world from ./src/hello.ts!
`,
]
`
];

static flags = {
help: flags.help({char: 'h'}),
public static flags = {
help: flags.help({ char: "h" }),
// flag with a value (-n, --name=VALUE)
name: flags.string({char: 'n', description: 'name to print'}),
name: flags.string({ char: "n", description: "name to print" }),
// flag with no value (-f, --force)
force: flags.boolean({char: 'f'}),
}

static args = [{name: 'file'}]
force: flags.boolean({ char: "f" })
};

async run() {
const {args, flags} = this.parse(Hello)
public async run(): Promise<void> {
const { args, flags: parsedFlags } = this.parse(Hello);

const name = flags.name || 'world'
this.log(`hello ${name} from ./src/commands/hello.ts`)
if (args.file && flags.force) {
this.log(`you input --force and --file: ${args.file}`)
const name = parsedFlags.name || "world";
this.log(`hello ${name} from ./src/commands/hello.ts`);
if (args.file && parsedFlags.force) {
this.log(`you input --force and --file: ${args.file}`);
}
}
}
11 changes: 5 additions & 6 deletions src/commands/messages/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { Command, flags } from "@oclif/command";
import cli from "cli-ux";
import * as parse from "csv-parse";
import * as fs from "fs";
import * as t from "io-ts";
import * as transform from "stream-transform";

import {
config,
getCosmosEndpoint,
getCosmosReadonlyKey,
getCosmosWriteKey
} from "../../utils/azure";
import { parseMessagePath } from "../../utils/parser";
Expand All @@ -35,13 +33,14 @@ export default class MessagesAttributes extends Command {
};

public run = async () => {
const { args, flags: parsedFlags } = this.parse(MessagesAttributes);
const { flags: parsedFlags } = this.parse(MessagesAttributes);

if (parsedFlags.isPending === undefined) {
this.error("At least one attribute must be changed");
this.exit();
}

// tslint:disable-next-line: no-inferred-empty-object-type
const messageDelta = [
{
key: "isPending",
Expand Down Expand Up @@ -95,7 +94,7 @@ export default class MessagesAttributes extends Command {
cli.action.stop();

const client = new cosmos.CosmosClient({ endpoint, auth: { key } });
const database = await client.database(config.cosmosDatabaseName);
const database = client.database(config.cosmosDatabaseName);
const container = database.container(config.cosmosMessagesContainer);

const updateMessage = async (fiscalCode: string, messageId: string) => {
Expand Down Expand Up @@ -132,8 +131,8 @@ export default class MessagesAttributes extends Command {
.pipe(parser)
.pipe(transformer)
.pipe(process.stdout);

await new Promise((res, rej) => parser.on("end", res));
// tslint:disable-next-line: no-inferred-empty-object-type
await new Promise((res, _) => parser.on("end", res));
} catch (e) {
this.error(e);
}
Expand Down
25 changes: 5 additions & 20 deletions src/commands/messages/check-content.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import * as cosmos from "@azure/cosmos";
import { Command, flags } from "@oclif/command";
import * as storage from "azure-storage";
import cli from "cli-ux";
import * as parse from "csv-parse";
import * as fs from "fs";
import * as t from "io-ts";
import { FiscalCode } from "italia-ts-commons/lib/strings";
import * as transform from "stream-transform";

import {
config,
getCosmosEndpoint,
getCosmosReadonlyKey,
getStorageConnection
} from "../../utils/azure";
import { config, getStorageConnection } from "../../utils/azure";
import { parseMessagePath } from "../../utils/parser";

export default class MessagesCheckContent extends Command {
Expand All @@ -36,7 +28,7 @@ export default class MessagesCheckContent extends Command {
};

public run = async () => {
const { args, flags: parsedFlags } = this.parse(MessagesCheckContent);
const { flags: parsedFlags } = this.parse(MessagesCheckContent);

const inputStream = parsedFlags.input
? fs.createReadStream(parsedFlags.input)
Expand All @@ -49,11 +41,7 @@ export default class MessagesCheckContent extends Command {

try {
cli.action.start("Retrieving credentials");
const [endpoint, key, storageConnection] = await Promise.all([
getCosmosEndpoint(config.resourceGroup, config.cosmosName),
getCosmosReadonlyKey(config.resourceGroup, config.cosmosName),
getStorageConnection(config.storageName)
]);
const storageConnection = await getStorageConnection(config.storageName);
cli.action.stop();

const blobService = storage.createBlobService(storageConnection);
Expand All @@ -66,10 +54,6 @@ export default class MessagesCheckContent extends Command {
(err, blobResult) => (err ? reject(err) : resolve(blobResult))
)
);
// const client = new cosmos.CosmosClient({ endpoint, auth: { key } });
// const database = await client.database(config.cosmosDatabaseName);
// const container = database.container(config.cosmosMessagesContainer);

const transformer = transform(
{
parallel: parsedFlags.parallel
Expand All @@ -93,7 +77,8 @@ export default class MessagesCheckContent extends Command {
.pipe(transformer)
.pipe(process.stdout);

await new Promise((res, rej) => parser.on("end", res));
// tslint:disable-next-line: no-inferred-empty-object-type
await new Promise((res, _) => parser.on("end", res));
} catch (e) {
this.error(e);
}
Expand Down
7 changes: 3 additions & 4 deletions src/commands/messages/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as cosmos from "@azure/cosmos";
import { Command, flags } from "@oclif/command";
import * as storage from "azure-storage";
import { Command } from "@oclif/command";
import cli from "cli-ux";
import { FiscalCode } from "italia-ts-commons/lib/strings";

Expand Down Expand Up @@ -46,11 +45,11 @@ export default class MessagesList extends Command {
cli.action.stop();

const client = new cosmos.CosmosClient({ endpoint, auth: { key } });
const database = await client.database(config.cosmosDatabaseName);
const database = client.database(config.cosmosDatabaseName);
const container = database.container(config.cosmosMessagesContainer);

cli.action.start("Querying messages...");
const response = await container.items.query({
const response = container.items.query({
parameters: [{ name: "@fiscalCode", value: fiscalCode }],
query:
"SELECT c.id, c.createdAt, c.isPending FROM c WHERE c.fiscalCode = @fiscalCode"
Expand Down
15 changes: 8 additions & 7 deletions src/commands/profiles/list.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as cosmos from "@azure/cosmos";
import { Command, flags } from "@oclif/command";
import { Command } from "@oclif/command";
import cli from "cli-ux";

import { getCosmosConnection } from "../../utils/azure";

export default class ProfilesList extends Command {
static description = "Lists all profiles";
public static description = "Lists all profiles";

static flags = {
public static flags = {
...cli.table.flags()
};

async run() {
const { args, flags } = this.parse(ProfilesList);
public async run(): Promise<void> {
const { flags } = this.parse(ProfilesList);

try {
cli.action.start("Retrieving cosmosdb credentials");
Expand All @@ -24,9 +24,9 @@ export default class ProfilesList extends Command {

cli.action.start("Querying profiles...");
const client = new cosmos.CosmosClient({ endpoint, auth: { key } });
const database = await client.database("agid-documentdb-test");
const database = client.database("agid-documentdb-test");
const container = database.container("profiles");
const response = await container.items.query(
const response = container.items.query(
"SELECT c.fiscalCode, c._ts FROM c WHERE c.version = 0",
{
enableCrossPartitionQuery: true
Expand All @@ -48,6 +48,7 @@ export default class ProfilesList extends Command {
},
createdAt: {
header: "createdAt",
// tslint:disable-next-line: no-any
get: (row: any) =>
row._ts && new Date(row._ts * 1000).toISOString(),
extended: true
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {run} from '@oclif/command'
export { run } from "@oclif/command";
28 changes: 28 additions & 0 deletions src/utils/azure.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cli from "cli-ux";
import * as execa from "execa";

export const config = {
Expand Down Expand Up @@ -42,3 +43,30 @@ export const getStorageConnection = async (name: string) =>
(await execa(
`az storage account show-connection-string --name ${name} --output tsv`
)).stdout;

/**
* hasCosmosConnection checks if the host has az cli installed and resouceGroup and name
* are valid inputs for cosmosdb
*/
export const hasCosmosConnection = async (
resourceGroup: string,
name: string
) => {
try {
const successExitCode = "SUCCESS";
const documentEndpoint = await execa(
`az cosmosdb show -g ${resourceGroup} -n ${name} --query documentEndpoint -o tsv`
);
const primaryReadonlyMasterKey = await execa(
`az cosmosdb list-keys -g ${resourceGroup} -n ${name} --query primaryReadonlyMasterKey -o tsv`
);

return (
documentEndpoint.exitCodeName === successExitCode &&
primaryReadonlyMasterKey.exitCodeName === successExitCode
);
} catch (e) {
cli.log(e);
return false;
}
};
1 change: 0 additions & 1 deletion src/utils/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as t from "io-ts";
import { FiscalCode } from "italia-ts-commons/lib/strings";

export const parseMessagePath = (
value: unknown
Expand Down
22 changes: 11 additions & 11 deletions test/commands/hello.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {expect, test} from '@oclif/test'
import { expect, test } from "@oclif/test";

describe('hello', () => {
describe("hello", () => {
test
.stdout()
.command(['hello'])
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world')
})
.command(["hello"])
.it("runs hello world", ctx => {
expect(ctx.stdout).to.contain("hello world");
});

test
.stdout()
.command(['hello', '--name', 'jeff'])
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff')
})
})
.command(["hello", "--name", "jeff"])
.it("runs hello --name jeff", ctx => {
expect(ctx.stdout).to.contain("hello jeff");
});
});
36 changes: 23 additions & 13 deletions test/commands/users/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import {expect, test} from '@oclif/test'
import { expect, test } from "@oclif/test";
import { hasCosmosConnection } from "../../../src/utils/azure";

describe('users:list', () => {
test
.stdout()
.command(['users:list'])
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world')
})
describe("list all profiles", () => {
/**
* if host has the az client installed and valid cosmos credentials we test command first output line matches
* the header row (fiscalCode column name). Otherwise the test will be skipped
*/

before(async function(): Promise<void> {
const isCosmosConnectionAvailable = await hasCosmosConnection(
"agid-rg-test",
"agid-cosmosdb-test"
);
// az cli not installed or bad cosmos credential, test no needed
if (!isCosmosConnectionAvailable) {
// tslint:disable-next-line: no-invalid-this
this.skip();
}
});
test
.stdout()
.command(['users:list', '--name', 'jeff'])
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff')
})
})
.command(["profiles:list"])
.it("runs profiles command to list all users", ctx => {
expect(ctx.stdout).match(/^fiscalCode/);
});
});
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
--watch-extensions ts
--recursive
--reporter spec
--timeout 5000
--timeout 15000

0 comments on commit c81d960

Please sign in to comment.