Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add project config override cli options #1454

Merged
merged 8 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
310 changes: 209 additions & 101 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,6 @@ const includeDependentsOption: INamedOption<yargs.Options> = {
}
};

const schemaSuffixOverrideOption: INamedOption<yargs.Options> = {
name: "schema-suffix",
option: {
describe: "A suffix to be appended to output schema names."
},
check: (argv: yargs.Arguments<any>) => {
if (
argv[schemaSuffixOverrideOption.name] &&
!/^[a-zA-Z_0-9]+$/.test(argv[schemaSuffixOverrideOption.name])
) {
throw new Error(
`--${schemaSuffixOverrideOption.name} should contain only alphanumeric characters and/or underscores.`
);
}
}
};

const credentialsOption: INamedOption<yargs.Options> = {
name: "credentials",
option: {
Expand Down Expand Up @@ -177,24 +160,6 @@ const jsonOutputOption: INamedOption<yargs.Options> = {
}
};

const varsOptionName = "vars";
const varsOption: INamedOption<yargs.Options> = {
name: varsOptionName,
option: {
describe: `Variables to inject via '--${varsOptionName}=someKey=someValue,a=b', referenced by \`dataform.projectConfig.vars.someValue\`.`,
type: "string",
default: null,
coerce: (rawVarsString: string | null) => {
const variables: { [key: string]: string } = {};
rawVarsString?.split(",").forEach(keyValueStr => {
const [key, value] = keyValueStr.split("=");
variables[key] = value;
});
return variables;
}
}
};

const timeoutOption: INamedOption<yargs.Options> = {
name: "timeout",
option: {
Expand All @@ -206,8 +171,53 @@ const timeoutOption: INamedOption<yargs.Options> = {
}
};

const defaultDatabaseOptionName = "default-database";
const defaultLocationOptionName = "default-location";
const defaultDatabaseOption: INamedOption<yargs.Options> = {
name: "default-database",
option: {
describe: "The default database to use. For BigQuery, this is a Google Cloud Project ID.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should clarify (for all of these) that they override any settings in dataform.json

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thread above - this is for init, so does not override anything, and is a required flag in this case.

type: "string"
},
check: (argv: yargs.Arguments<any>) => {
if (!argv[warehouseOption.name]) {
return;
}
if (
argv[defaultDatabaseOption.name] &&
Ekrekr marked this conversation as resolved.
Show resolved Hide resolved
!["bigquery", "snowflake"].includes(argv[warehouseOption.name])
) {
throw new Error(
`The --${defaultDatabaseOption.name} flag is only used for BigQuery and Snowflake projects.`
);
}
if (!argv[defaultDatabaseOption.name] && argv[warehouseOption.name] === "bigquery") {
throw new Error(
`The --${defaultDatabaseOption.name} flag is required for BigQuery projects.`
);
}
Ekrekr marked this conversation as resolved.
Show resolved Hide resolved
}
};

const defaultLocationOption: INamedOption<yargs.Options> = {
name: "default-location",
option: {
describe:
"The default BigQuery location to use. See https://cloud.google.com/bigquery/docs/locations for supported values."
},
check: (argv: yargs.Arguments<any>) => {
if (!argv[warehouseOption.name]) {
return;
}
if (argv[defaultLocationOption.name] && !["bigquery"].includes(argv[warehouseOption.name])) {
throw new Error(`The --${defaultLocationOption.name} flag is only used for BigQuery.`);
}
if (!argv[defaultLocationOption.name] && argv[warehouseOption.name] === "bigquery") {
throw new Error(
`The --${defaultLocationOption.name} flag is required for BigQuery projects. Please run 'dataform help init' for more information.`
);
}
}
};

const skipInstallOptionName = "skip-install";

const testConnectionOptionName = "test-connection";
Expand Down Expand Up @@ -242,50 +252,8 @@ export function runCli() {
description: "Create a new dataform project.",
positionalOptions: [warehouseOption, projectDirOption],
options: [
{
name: defaultDatabaseOptionName,
option: {
describe:
"The default database to use. For BigQuery, this is a Google Cloud Project ID."
},
check: (argv: yargs.Arguments<any>) => {
if (
argv[defaultDatabaseOptionName] &&
!["bigquery", "snowflake"].includes(argv[warehouseOption.name])
) {
throw new Error(
`The --${defaultDatabaseOptionName} flag is only used for BigQuery and Snowflake projects.`
);
}
if (!argv[defaultDatabaseOptionName] && argv[warehouseOption.name] === "bigquery") {
throw new Error(
`The --${defaultDatabaseOptionName} flag is required for BigQuery projects. Please run 'dataform help init' for more information.`
);
}
}
},
{
name: defaultLocationOptionName,
option: {
describe:
"The default BigQuery location to use. See https://cloud.google.com/bigquery/docs/locations for supported values."
},
check: (argv: yargs.Arguments<any>) => {
if (
argv[defaultLocationOptionName] &&
!["bigquery"].includes(argv[warehouseOption.name])
) {
throw new Error(
`The --${defaultLocationOptionName} flag is only used for BigQuery.`
);
}
if (!argv[defaultLocationOptionName] && argv[warehouseOption.name] === "bigquery") {
throw new Error(
`The --${defaultLocationOptionName} flag is required for BigQuery projects. Please run 'dataform help init' for more information.`
);
}
}
},
defaultDatabaseOption,
defaultLocationOption,
{
name: skipInstallOptionName,
option: {
Expand All @@ -300,8 +268,8 @@ export function runCli() {
argv[projectDirOption.name],
{
warehouse: argv[warehouseOption.name],
defaultDatabase: argv[defaultDatabaseOptionName],
defaultLocation: argv[defaultLocationOptionName],
defaultDatabase: argv[defaultDatabaseOption.name],
defaultLocation: argv[defaultLocationOption.name],
useRunCache: false
},
{
Expand Down Expand Up @@ -411,23 +379,20 @@ export function runCli() {
default: false
}
},
schemaSuffixOverrideOption,
jsonOutputOption,
varsOption,
timeoutOption
timeoutOption,
...ProjectConfigOverride.allYargsOptions
],
processFn: async argv => {
const projectDir = argv[projectDirMustExistOption.name];
const schemaSuffixOverride = argv[schemaSuffixOverrideOption.name];
const vars = argv[varsOption.name];

const compileAndPrint = async () => {
if (!argv[jsonOutputOption.name]) {
print("Compiling...\n");
}
const compiledGraph = await compile({
projectDir,
projectConfigOverride: { vars, schemaSuffix: schemaSuffixOverride },
projectConfigOverride: ProjectConfigOverride.construct(argv),
timeoutMillis: argv[timeoutOption.name] || undefined
});
printCompiledGraph(compiledGraph, argv[jsonOutputOption.name]);
Expand Down Expand Up @@ -503,15 +468,12 @@ export function runCli() {
format: `test [${projectDirMustExistOption.name}]`,
description: "Run the dataform project's unit tests on the configured data warehouse.",
positionalOptions: [projectDirMustExistOption],
options: [credentialsOption, varsOption, timeoutOption],
options: [credentialsOption, timeoutOption, ...ProjectConfigOverride.allYargsOptions],
processFn: async argv => {
print("Compiling...\n");
const compiledGraph = await compile({
projectDir: argv[projectDirMustExistOption.name],
projectConfigOverride: {
vars: argv[varsOption.name],
schemaSuffix: argv[schemaSuffixOverrideOption.name]
},
projectConfigOverride: ProjectConfigOverride.construct(argv),
timeoutMillis: argv[timeoutOption.name] || undefined
});
if (compiledGraphHasErrors(compiledGraph)) {
Expand Down Expand Up @@ -570,22 +532,18 @@ export function runCli() {
tagsOption,
includeDepsOption,
includeDependentsOption,
schemaSuffixOverrideOption,
credentialsOption,
jsonOutputOption,
varsOption,
timeoutOption
timeoutOption,
...ProjectConfigOverride.allYargsOptions
],
processFn: async argv => {
if (!argv[jsonOutputOption.name]) {
print("Compiling...\n");
}
const compiledGraph = await compile({
projectDir: argv[projectDirOption.name],
projectConfigOverride: {
vars: argv[varsOption.name],
schemaSuffix: argv[schemaSuffixOverrideOption.name]
},
projectConfigOverride: ProjectConfigOverride.construct(argv),
timeoutMillis: argv[timeoutOption.name] || undefined
});
if (compiledGraphHasErrors(compiledGraph)) {
Expand Down Expand Up @@ -802,3 +760,153 @@ export function runCli() {
yargs.showHelp();
}
}

class ProjectConfigOverride {
Ekrekr marked this conversation as resolved.
Show resolved Hide resolved
public static warehouseOverrideOption: INamedOption<yargs.Options> = {
...warehouseOption,
option: {
describe:
"Override for the project's data warehouse type. If unset, the value from dataform.json is used.",
type: "string"
}
};

public static defaultDatabaseOverrideOption: INamedOption<yargs.Options> = {
...defaultDatabaseOption,
option: {
describe:
"Override for the default database to use. For BigQuery, this is a " +
"Google Cloud Project ID. If unset, the value from dataform.json is used.",
type: "string"
}
};

public static defaultSchemaOverrideOption: INamedOption<yargs.Options> = {
name: "default-schema",
option: {
describe:
"Override for the default schema name. If unset, the value from dataform.json is used."
}
};

public static defaultLocationOverrideOption: INamedOption<yargs.Options> = {
...defaultLocationOption,
option: {
describe:
"Override for the default BigQuery location to use. See " +
"https://cloud.google.com/bigquery/docs/locations for supported values.If unset, the " +
"value from dataform.json is used."
}
};

public static assertionSchemaOverrideOption: INamedOption<yargs.Options> = {
name: "assertion-schema",
option: {
describe: "Default assertion schema. If unset, the value from dataform.json is used."
}
};

public static databaseSuffixOverrideOption: INamedOption<yargs.Options> = {
name: "database-suffix",
option: {
describe: "Default assertion schema. If unset, the value from dataform.json is used."
}
};

public static varsOverrideOption: INamedOption<yargs.Options> = {
name: "vars",
option: {
describe:
"Override for variables to inject via '--vars=someKey=someValue,a=b', referenced by " +
"`dataform.projectConfig.vars.someValue`. If unset, the value from dataform.json is used.",
type: "string",
default: null,
coerce: (rawVarsString: string | null) => {
const variables: { [key: string]: string } = {};
rawVarsString?.split(",").forEach(keyValueStr => {
const [key, value] = keyValueStr.split("=");
variables[key] = value;
});
return variables;
}
}
};

public static schemaSuffixOverrideOption: INamedOption<yargs.Options> = {
name: "schema-suffix",
option: {
describe:
"A suffix to be appended to output schema names. If unset, the value from dataform.json is used."
},
check: (argv: yargs.Arguments<any>) => {
if (
argv[ProjectConfigOverride.schemaSuffixOverrideOption.name] &&
!/^[a-zA-Z_0-9]+$/.test(argv[ProjectConfigOverride.schemaSuffixOverrideOption.name])
) {
throw new Error(
`--${ProjectConfigOverride.schemaSuffixOverrideOption.name} should contain only ` +
`alphanumeric characters and/or underscores.`
);
}
}
};

public static tablePrefixOverrideOption: INamedOption<yargs.Options> = {
name: "table-prefix",
option: {
describe: "Adds a prefix for all table names. If unset, the value from dataform.json is used."
}
};

public static allYargsOptions = [
ProjectConfigOverride.warehouseOverrideOption,
ProjectConfigOverride.defaultDatabaseOverrideOption,
ProjectConfigOverride.defaultSchemaOverrideOption,
ProjectConfigOverride.defaultLocationOverrideOption,
ProjectConfigOverride.assertionSchemaOverrideOption,
ProjectConfigOverride.varsOverrideOption,
ProjectConfigOverride.databaseSuffixOverrideOption,
ProjectConfigOverride.schemaSuffixOverrideOption,
ProjectConfigOverride.tablePrefixOverrideOption
];

public static construct(argv: yargs.Arguments<any>): dataform.IProjectConfig {
const projectConfigOverride: dataform.IProjectConfig = {};

if (argv[ProjectConfigOverride.warehouseOverrideOption.name]) {
projectConfigOverride.warehouse = argv[ProjectConfigOverride.warehouseOverrideOption.name];
}
if (argv[ProjectConfigOverride.defaultDatabaseOverrideOption.name]) {
projectConfigOverride.defaultDatabase =
argv[ProjectConfigOverride.defaultDatabaseOverrideOption.name];
}
if (argv[ProjectConfigOverride.defaultSchemaOverrideOption.name]) {
projectConfigOverride.defaultSchema =
argv[ProjectConfigOverride.defaultSchemaOverrideOption.name];
}
if (argv[ProjectConfigOverride.defaultLocationOverrideOption.name]) {
projectConfigOverride.defaultLocation =
argv[ProjectConfigOverride.defaultLocationOverrideOption.name];
}
if (argv[ProjectConfigOverride.assertionSchemaOverrideOption.name]) {
projectConfigOverride.assertionSchema =
argv[ProjectConfigOverride.assertionSchemaOverrideOption.name];
}
if (argv[ProjectConfigOverride.varsOverrideOption.name]) {
projectConfigOverride.vars = argv[ProjectConfigOverride.varsOverrideOption.name];
}
if (argv[ProjectConfigOverride.databaseSuffixOverrideOption.name]) {
projectConfigOverride.databaseSuffix =
argv[ProjectConfigOverride.databaseSuffixOverrideOption.name];
}
if (argv[ProjectConfigOverride.schemaSuffixOverrideOption.name]) {
projectConfigOverride.schemaSuffix =
argv[ProjectConfigOverride.schemaSuffixOverrideOption.name];
}
if (argv[ProjectConfigOverride.tablePrefixOverrideOption.name]) {
projectConfigOverride.schemaSuffix =
argv[ProjectConfigOverride.tablePrefixOverrideOption.name];
}
return projectConfigOverride;
}
}
Loading