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

lagu: Consolidate command data and configuration #497

Closed
karlhorky opened this issue Jul 11, 2018 · 3 comments
Closed

lagu: Consolidate command data and configuration #497

karlhorky opened this issue Jul 11, 2018 · 3 comments

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Jul 11, 2018

Consolidate the various types of data and configuration we have for the commands into a common format, that should support integration with:

  • yargs
  • environment variables
  • .lagoon.yml config
  • inquirer

Potential strategy

Read in input in the config/index.js (priority: CLI args, env vars, config, inquirer) based on what the command needs, and then simplify getOptions.

Maybe look at some examples of CLI tools that do this (maybe Prettier?) and possibly projects like yargs-interactive. For example, this file may be interesting to take a look at.

@karlhorky karlhorky self-assigned this Jul 11, 2018
@karlhorky
Copy link
Contributor Author

Example of duplicated boilerplate / configuration:

From cli/src/commands/project/create.js:

export const CUSTOMER: 'customer' = 'customer';
export const NAME: 'name' = 'name';
export const GIT_URL: 'git_url' = 'git_url';
export const OPENSHIFT: 'openshift' = 'openshift';
export const BRANCHES: 'branches' = 'branches';
export const PULLREQUESTS: 'pullrequests' = 'pullrequests';
export const PRODUCTION_ENVIRONMENT: 'production_environment' =
  'production_environment';

export const commandOptions = {
  [CUSTOMER]: CUSTOMER,
  [NAME]: NAME,
  [GIT_URL]: GIT_URL,
  [OPENSHIFT]: OPENSHIFT,
  [BRANCHES]: BRANCHES,
  [PULLREQUESTS]: PULLREQUESTS,
  [PRODUCTION_ENVIRONMENT]: PRODUCTION_ENVIRONMENT,
};

type OptionalOptions = {
  customer?: number,
  name?: string,
  git_url?: string,
  openshift?: number,
  branches?: string,
  pullrequests?: string,
  production_environment?: string,
};

type Options = {
  customer: number,
  name: string,
  git_url: string,
  openshift: number,
  branches: string,
  pullrequests: string,
  production_environment: string,
};

yargs
  .usage(`$0 ${command} - ${description}`)
  .options({
    [CUSTOMER]: {
      describe: 'Customer id to use for new project',
      type: 'number',
      alias: 'c',
    },
    [NAME]: {
      describe: 'Name of new project',
      type: 'string',
      alias: 'n',
    },
    [GIT_URL]: {
      describe: 'Git URL of new project',
      type: 'string',
      alias: 'u',
    },
    // ...

inquirer.prompt([
  {
    type: 'list',
    name: CUSTOMER,
    message: 'Customer:',
    choices: allCustomers,
  },
  {
    type: 'input',
    name: NAME,
    message: 'Project name:',
  },
  // ...

From cli/src/config/index.js:

export type LagoonConfig = {|
  api?: string,
  branches?: boolean | RegExp,
  customer?: number,
  git_url?: string,
  openshift?: number,
  production_environment?: string,
  project?: string,
  pullrequests?: boolean | RegExp,
  ssh?: string,
  token?: string,
|};

@twardnw twardnw added the lagu label Jul 23, 2018
@karlhorky
Copy link
Contributor Author

karlhorky commented Jul 24, 2018

Looks like the yargs .ENV([PREFIX]) docs mention something that sounds similar to this:

Program arguments are defined in this order of precedence:

  1. Command line args
  2. Env vars
  3. Config file/objects
  4. Configured defaults

Maybe we could just use yargs for this (except Inquirer.js config), at least at the start.


Edit: We could probably also gather Inquirer.js config too if we exported another function per module (would be nice to split up the commands into multiple files per command if possible at this point) and did something in the handler override in yargs visit function we already have in cli/visit.js.


Edit 2: We will also need a solution in the future for global (non-command-specific) configuration (such as format from #513), with the same precedence order.

@karlhorky
Copy link
Contributor Author

karlhorky commented Aug 3, 2018

Some inspiration may be able to be found in:

Heroku's oclif (Open CLI Framework)

Zeit's now-cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants