Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

feat: use env var UNS_NETWORK to select network #8

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Command, flags as oFlags } from "@oclif/command";
import { FlagInvalidOptionError } from "@oclif/parser/lib/errors";
import { Client, configManager } from "@uns/crypto";
import { cli } from "cli-ux";
import { UNSCLIAPI } from "./api";
Expand All @@ -14,6 +15,7 @@ export abstract class BaseCommand extends Command {
description: "Network used to create UNIK nft token",
required: true,
options: UTILS.getNetworksList(),
env: "UNS_NETWORK",
}),
verbose: oFlags.boolean({
char: "v",
Expand Down Expand Up @@ -49,6 +51,14 @@ export abstract class BaseCommand extends Command {
/**
* Configuration
*/

// This happen when providing network through env var
// see Oclif PR: https://github.com/oclif/parser/pull/64
// will be removed if PR is accepted
if (!UTILS.getNetworksList().includes(flags.network)) {
peterjah marked this conversation as resolved.
Show resolved Hide resolved
throw new FlagInvalidOptionError(BaseCommand.baseFlags.network, flags.network);
}

const networkName = flags.network === "local" ? "testnet" : flags.network;

const networkPreset = configManager.getPreset(networkName);
Expand Down
12 changes: 12 additions & 0 deletions test/__fixtures__/commands/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ export const outputCases = [
args: ["status", "--network", "devnet", "--format", "table", "--verbose"],
expected: infoNode + statusResultTable,
},
{
description: "Should use env var and return devnet status json",
args: ["status"],
expected: statusResultJson,
UNS_NETWORK: "devnet",
},
{
description: "Should not use env var and return devnet status json",
args: ["status", "--network", "devnet"],
expected: statusResultJson,
UNS_NETWORK: "customnetwork",
},
];
8 changes: 8 additions & 0 deletions test/commands/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const applyTestCase = (testCase: any) => {
},
}),
)
.env({ UNS_NETWORK: testCase.UNS_NETWORK })
.stdout()
.command(testCase.args)
.it(testCase.description, ctx => {
Expand All @@ -49,5 +50,12 @@ describe("status command", () => {
// tslint:disable-next-line:no-empty
.it("Should exit with code 2 if network is not known", _ => {});

const network = "customNetwork";
test.env({ UNS_NETWORK: network })
.command(["status"])
.catch(err => expect(err.message.match(`Expected --network=${network} to be one of`)))
// tslint:disable-next-line:no-empty
.it("Should use UNS_NETWORK env var then throw error", _ => {});

outputCases.forEach(testCase => applyTestCase(testCase));
});