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

fix #4: Removes 'function' prefix from CLI args #16

Merged
merged 6 commits into from
Apr 17, 2019

Conversation

grant
Copy link
Contributor

@grant grant commented Apr 11, 2019

Fixes #4.

  • Removes redundant function from CLI/env vars for target and signature-type
  • Updates docs
  • Moves string literals into constants to reduce renaming multiple pieces of code and clearly define the library interface in 1 place.

src/index.ts Outdated
enum NodeEnv {
PRODUCTION = 'production',
}

const argv = minimist(process.argv, {
string: ['port', 'function-target', 'function-signature-type'],
string: [ENV_VARIABLE.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE],
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be FLAG.PORT.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

src/index.ts Outdated
});

const CODE_LOCATION = process.cwd();
const PORT = argv['port'] || process.env.PORT || '8080';
const PORT =
argv[ENV_VARIABLE.PORT] || process.env[ENV_VARIABLE.PORT] || '8080';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be argv[FLAG.PORT].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

src/index.ts Outdated
argv['function-signature-type'] ||
process.env.FUNCTION_SIGNATURE_TYPE ||
'http';
argv[FLAG.SIGNATURE_TYPE] || process.env.FUNCTION_SIGNATURE_TYPE || 'http';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be process.env[ENV_VARIABLE.SIGNATURE_TYPE], for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

src/index.ts Outdated

// Supported environment variables
const ENV_VARIABLE = {
PORT: 'FUNCTION_PORT',
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be kept as PORT. It is not a functions-specific environment variable, and it comes from Knative runtime contract.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

const ENV_VARIABLE = {
PORT: 'FUNCTION_PORT',
TARGET: 'FUNCTION_TARGET',
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
Copy link
Contributor

Choose a reason for hiding this comment

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

While we are refactoring this, please replace FUNCTION_SIGNATURE_TYPE in FUNCTION_SIGNATURE_TYPE must be one of 'http' or 'event'. message below with Function signature type, as it can refer to either flag or env var.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

src/index.ts Outdated
});

const CODE_LOCATION = process.cwd();
const PORT = argv['port'] || process.env.PORT || '8080';
const PORT =
argv[ENV_VARIABLE.PORT] || process.env[ENV_VARIABLE.PORT] || '8080';
const FUNCTION_TARGET =
Copy link
Contributor

Choose a reason for hiding this comment

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

While I can see how the refactoring you are proposing makes sense, it also makes the code a bit less readable, in my opinion, because of the extra mappings. For example, we are mapping FUNCTION_TARGET string to ENV_VARIABLE.TARGET and then resolving it to FUNCTION_TARGET const.

If the names without 'function' prefix are to be the canonical ones, then let's drop FUNCTION_ prefix from FUNCTION_TARGET, FUNCTION_SIGNATURE_TYPE_STRING, FUNCTION_SIGNATURE_TYPE consts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped the FUNCTION_ prefix.

Copy link
Contributor

@swalkowski swalkowski left a comment

Choose a reason for hiding this comment

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

I like this change now, please address the remaining comments. Thanks!

src/index.ts Outdated
const FUNCTION_TARGET =
argv['function-target'] || process.env.FUNCTION_TARGET || 'function';
const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080';
const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function';

const FUNCTION_SIGNATURE_TYPE_STRING =
Copy link
Contributor

Choose a reason for hiding this comment

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

We could drop FUNCTION_ prefix in this const as well, for consistency and brevity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This request is getting out of scope, but I've changed it as I agree.

src/index.ts Outdated
argv['function-signature-type'] ||
process.env.FUNCTION_SIGNATURE_TYPE ||
'http';
argv[FLAG.SIGNATURE_TYPE] || process.env[ENV.SIGNATURE_TYPE] || 'http';
const FUNCTION_SIGNATURE_TYPE =
Copy link
Contributor

Choose a reason for hiding this comment

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

We could drop FUNCTION_ prefix in this const as well, for consistency and brevity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

src/index.ts Outdated
// Supported environment variables
const ENV = {
PORT: 'PORT',
TARGET: 'TARGET',
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps I wasn't clear in my previous comments: we should keep the environment variable names unchanged, just like in the table in README. Please keep PORT, FUNCTION_TARGET, FUNCTION_SIGNATURE_TYPE.

Short names are fine for flag names and const names in the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean:

const ENV = {
  PORT: 'PORT',
  TARGET: 'FUNCTION_TARGET',
  SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Agreed.
Not sure why I changed this. Reverted the FUNCTION_ prefix for these env vars.

Copy link
Contributor Author

@grant grant left a comment

Choose a reason for hiding this comment

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

Addressed comments. PTAL.

import * as http from 'http';
import * as onFinished from 'on-finished';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IDE auto-sorted.

import * as invoker from '../src/invoker';
import * as supertest from 'supertest';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IDE auto-sorted.

Copy link
Contributor

@swalkowski swalkowski left a comment

Choose a reason for hiding this comment

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

Thank you! LGTM.

@swalkowski swalkowski changed the title fix #4: Removes 'function' prefix from CLI arg and env vars fix #4: Removes 'function' prefix from CLI args Apr 17, 2019
@swalkowski swalkowski merged commit 76f134b into master Apr 17, 2019
@grant grant deleted the grant_remove_function_prefix branch April 17, 2019 15:24
@grant grant restored the grant_remove_function_prefix branch May 31, 2019 17:21
@grant grant deleted the grant_remove_function_prefix branch May 6, 2020 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flags use an unnecessary function- prefix
3 participants