Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Add --url option to the truffle migrate command #5739

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
21 changes: 19 additions & 2 deletions packages/core/lib/commands/migrate/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ module.exports = {
type: "boolean",
default: true,
hidden: true
},
"url": {
describe:
"Creates a provider using the given url and connects to the network",
type: "string"
}
},
help: {
Expand All @@ -62,7 +67,9 @@ module.exports = {
" " + // spacing to align with previous line
"[--compile-all] [--compile-none] [--verbose-rpc] [--interactive]\n" +
" " + // spacing to align with previous line
"[--skip-dry-run] [--describe-json] [--dry-run]",
"[--skip-dry-run] [--describe-json] [--dry-run]\n" +
" " + // spacing to align with previous line
"[--network <network>|--url <provider_url>]",
options: [
{
option: "--reset",
Expand Down Expand Up @@ -118,8 +125,18 @@ module.exports = {
option: "--save",
description: "Specify whether the migration will save on chain",
hidden: true
},
{
option: "--url",
description:
"Creates a provider using the given url and connects to the network."
},
{
option: "--network",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about why you prefer --network lives here vs with the other global options.

Actually, can url be a global option as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global options generally appends the options at the end of the usage. The reason behind keeping --network and --url options here rather than in the global options, is to show that they are mutually exclusive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okie. thank you for the explanation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a note in each of these letting the user know that these options are mutually exclusive? do you think that would be helpful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage section shows that they are mutually exclusive but if adding a note in the descriptions is preferable, then we can add it here too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whatever you think, I don't feel strongly about it :)

description:
"The network to connect to, as specified in the Truffle config."
}
],
allowedGlobalOptions: ["network", "config", "quiet"]
allowedGlobalOptions: ["config", "quiet"]
}
};
18 changes: 16 additions & 2 deletions packages/core/lib/commands/migrate/run.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
module.exports = async function (options) {
const WorkflowCompile = require("@truffle/workflow-compile").default;
const { Environment } = require("@truffle/environment");
const Config = require("@truffle/config");
const determineDryRunSettings = require("./determineDryRunSettings");
const prepareConfigForRealMigrations = require("./prepareConfigForRealMigrations");
const runMigrations = require("./runMigrations");
const setUpDryRunEnvironmentThenRunMigrations = require("./setUpDryRunEnvironmentThenRunMigrations");
const loadConfig = require("../../loadConfig");
const OS = require("os");
const TruffleError = require("@truffle/error");
const tmp = require("tmp");
tmp.setGracefulCleanup();

const config = Config.detect(options);
if (options.url && options.network) {
const message =
"" +
"Mutually exclusive options, --url and --network detected!" +
OS.EOL +
"Please use either --url or --network and try again." +
OS.EOL +
"See: https://trufflesuite.com/docs/truffle/reference/truffle-commands/#migrate" +
OS.EOL;
throw new TruffleError(message);
}
cds-amal marked this conversation as resolved.
Show resolved Hide resolved

const config = loadConfig(options);
if (config.compileNone || config["compile-none"]) {
config.compiler = "none";
}
Expand Down
12 changes: 11 additions & 1 deletion packages/core/lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const processInput = (input, allowedCommands) => {

if (cmd === undefined) {
return makeIIFE(
`ℹ️ : 'Missing truffle command. Please include a valid truffle command.`
`ℹ️ : Missing truffle command. Please include a valid truffle command.`
);
}

Expand All @@ -46,6 +46,16 @@ const processInput = (input, allowedCommands) => {
return makeIIFE(`ℹ️ : '${cmd}' is not a valid Truffle command`);
}

if (words.includes("--url")) {
const message = "url option is not supported within Truffle REPL";
return makeIIFE(`ℹ️ : ${message}`);
}

if (words.includes("--network")) {
const message = "network option is not supported within Truffle REPL";
return makeIIFE(`ℹ️ : ${message}`);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add tests for these two

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the tests. Thanks @cds-amal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests should cover both logic branches; with and without the truffle prefix.

// an expression
return input.trim();
};
Expand Down
2 changes: 1 addition & 1 deletion packages/truffle/test/scenarios/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("truffle develop", function () {
},
{
cmd: "",
expectedError: `ℹ️ : 'Missing truffle command. Please include a valid truffle command.`
expectedError: `ℹ️ : Missing truffle command. Please include a valid truffle command.`
}
].forEach(({ cmd, expectedError }) => {
it(`alerts on 'truffle ${cmd}'`, async function () {
Expand Down
10 changes: 10 additions & 0 deletions packages/truffle/test/scenarios/commands/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@ describe("truffle migrate", () => {
assert.fail();
}
}).timeout(20000);

it("doesn't throw when --url option is passed", async () => {
try {
await CommandRunner.run("migrate --url http://127.0.0.1:8545", config);
} catch (error) {
console.log("the logger contents -- %o", config.logger.loggedStuff);
console.log("the following error occurred -- %o", error.message);
assert.fail();
}
}).timeout(20000);
});
});