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

Commit

Permalink
feat: use env var UNS_NETWORK to select network
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Oct 9, 2019
1 parent c1e8650 commit d0091e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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)) {
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));
});

0 comments on commit d0091e8

Please sign in to comment.