-
Notifications
You must be signed in to change notification settings - Fork 40
Saas 965 configuring cli #239
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
62a71bb
cli-config output formatting
c7d3213
constants extracted
0123620
cli config management
0edcb01
profiles + schema validation + output changes + get command changes +…
38e34b9
packages + docs
0e7a672
fix docs order
be5fe45
removed constants
b651b31
errors refactored
75f17f1
set null support
536bdb7
Merge branch 'master' into SAAS-965-configuring-cli
7ec8a09
help for properties + null is now displayed
86d00de
config validation
10e8543
model defaults for objects in schema
c04f8a3
manager config "lazy load"
7cc7984
advanced formatting + tests
36060eb
output formatting config on get command + fallback mode
5a61c1a
version updated
55e5563
add current config to fallback mode
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const { homedir } = require('os'); | ||
| const path = require('path'); | ||
|
|
||
| const codefreshPath = path.resolve(homedir(), '.Codefresh'); | ||
|
|
||
|
|
||
| module.exports = { | ||
| codefreshPath, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| const Command = require('../../Command'); | ||
|
|
||
| const manager = require('../../../../logic/cli-config/manager'); | ||
| const {outputCliConfig} = require('../../helpers/cli-config'); | ||
|
|
||
| const cliConfig = new Command({ | ||
| root: true, | ||
| command: 'cli-config', | ||
| description: 'Codefresh CLI configuration. Uses profiles', | ||
| usage: 'CLI configuration is used for user-specific properties, for example pretty-print', | ||
| category: 'cli config', | ||
| webDocs: { | ||
| description: 'Codefresh CLI configuration. Uses profiles', | ||
| category: 'CLI Config', | ||
| title: 'Show Config', | ||
| weight: 100, | ||
| }, | ||
| builder: (yargs) => { | ||
| return yargs | ||
| .option('output', { | ||
| alias: 'o', | ||
| describe: 'Output format', | ||
| options: ['json', 'yaml'], | ||
| }) | ||
| .example('codefresh cli-config', 'Print configuration for current profile'); | ||
| }, | ||
| handler: async (argv) => { | ||
| const config = manager.config(); | ||
| console.log(`Current profile: | ${manager.currentProfile()} |\n`); | ||
| outputCliConfig(argv.output, config); | ||
| }, | ||
| }); | ||
|
|
||
| module.exports = cliConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| const Command = require('../../../Command'); | ||
| const cliCommand = require('../cli-config.cmd'); | ||
|
|
||
| const manager = require('../../../../../logic/cli-config/manager'); | ||
| const { printProperties, outputCliConfig, propertyErrorHandler } = require('../../../helpers/cli-config'); | ||
|
|
||
| const getCommand = new Command({ | ||
| command: 'get [name]', | ||
| parent: cliCommand, | ||
| description: 'For current profile get all properties containing provided "name"', | ||
| usage: 'Used when you may need to know some exact properties values', | ||
| webDocs: { | ||
| description: 'For current profile get all properties containing provided "name"', | ||
| category: 'CLI Config', | ||
| title: 'Get Config Properties', | ||
| weight: 110, | ||
| }, | ||
| builder: (yargs) => { | ||
| return yargs | ||
| .positional('name', { | ||
| describe: 'Property name', | ||
| }) | ||
| .example('codefresh cli-config get', 'Print available property names') | ||
| .example('codefresh cli-config get output', 'Print properties, containing "output" word') | ||
| .example('codefresh cli-config get output.pretty', 'Print properties, containing "output.pretty" path'); | ||
| }, | ||
| handler: async (argv) => { | ||
| const propertyName = argv.name; | ||
| if (!propertyName) { | ||
| console.log('Available properties:\n'); | ||
| printProperties(manager.availableProperties()); | ||
| return; | ||
| } | ||
|
|
||
| let properties; | ||
| try { | ||
| properties = manager.get(propertyName); | ||
| } catch (e) { | ||
| propertyErrorHandler(e); | ||
| } | ||
| console.log(`Current profile: | ${manager.currentProfile()} |\n`); | ||
| outputCliConfig(argv.output, properties); | ||
| }, | ||
| }); | ||
|
|
||
| module.exports = getCommand; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| const Command = require('../../../Command'); | ||
| const cliCommand = require('../cli-config.cmd'); | ||
|
|
||
| const CliConfigManager = require('../../../../../logic/cli-config/manager'); | ||
|
|
||
| const setCommand = new Command({ | ||
| command: 'profile [name]', | ||
| parent: cliCommand, | ||
| description: 'Change cli-config profile', | ||
| usage: 'Using profiles gives an ability to switch fast between the different configuration sets', | ||
| webDocs: { | ||
| description: 'Change cli-config profile', | ||
| category: 'CLI Config', | ||
| title: 'Profiles', | ||
| weight: 120, | ||
| }, | ||
| builder: (yargs) => { | ||
| return yargs | ||
| .positional('name', { | ||
| describe: 'Profile name', | ||
| }) | ||
| .example('codefresh cli-config profile myProfile', 'Use or create profile with name "myProfile"'); | ||
| }, | ||
| handler: async (argv) => { | ||
| const { name } = argv; | ||
|
|
||
| if (!name) { | ||
| console.log(`Current profile: | ${CliConfigManager.currentProfile()} |\n`); | ||
yaroslav-codefresh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log('Available profiles:'); | ||
| CliConfigManager.profiles().forEach((profile) => { | ||
| console.log(profile); | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const created = CliConfigManager.useProfile(name); | ||
| CliConfigManager.persistConfig(); | ||
|
|
||
| const message = created ? `Profile "${name}" was created` : `Using profile "${name}"`; | ||
| console.log(message); | ||
| }, | ||
| }); | ||
|
|
||
| module.exports = setCommand; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| const Command = require('../../../Command'); | ||
| const cliCommand = require('../cli-config.cmd'); | ||
|
|
||
| const Manager = require('../../../../../logic/cli-config/manager'); | ||
| const {outputCliConfig, propertyErrorHandler, printProperties} = require('../../../helpers/cli-config'); | ||
|
|
||
| // todo : fix descriptions for docs | ||
| const setCommand = new Command({ | ||
| command: 'set [name] [value]', | ||
| parent: cliCommand, | ||
| description: 'For current profile set a property with "name"', | ||
| webDocs: { | ||
| description: 'For current profile set an available property', | ||
| category: 'CLI Config', | ||
| title: 'Set Config Property', | ||
| weight: 110, | ||
| }, | ||
| builder: (yargs) => { | ||
| return yargs | ||
| .positional('name', { | ||
| describe: 'Property name', | ||
| }) | ||
| .positional('value', { | ||
| describe: 'Property value', | ||
| }) | ||
| .example('codefresh cli-config set', 'Print available property names') | ||
| .example('codefresh cli-config set output.pretty false', 'Set "output.pretty" property'); | ||
| }, | ||
| handler: async (argv) => { | ||
| const { name, value } = argv; | ||
| if (!name) { | ||
| console.log('Available properties:\n'); | ||
| printProperties(Manager.availableProperties()); | ||
| return; | ||
| } | ||
| if (!value) { | ||
| console.log(`No value provided for property "${name}"`); | ||
| return; | ||
| } | ||
|
|
||
| let props; | ||
| try { | ||
| Manager.set(name, value); | ||
| Manager.persistConfig(); | ||
| props = Manager.get(name); | ||
| } catch (e) { | ||
| propertyErrorHandler(e); | ||
| return; | ||
| } | ||
| console.log(`Property set on profile: | ${Manager.currentProfile()} |\n`); | ||
| outputCliConfig(argv.output, props); | ||
| }, | ||
| }); | ||
|
|
||
| module.exports = setCommand; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| const _ = require('lodash'); | ||
| const flattern = require('flat'); | ||
| const columnify = require('columnify'); | ||
| const yaml = require('js-yaml'); | ||
| const { NoPropertyError, MultiplePropertiesError, NotFullPropertyError, SchemaValidationError } = require('../../../logic/cli-config/errors'); | ||
|
|
||
| const _jsonFormatter = data => JSON.stringify(data, null, 4); | ||
|
|
||
| const propertyComparator = (a, b) => { | ||
yaroslav-codefresh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const aDots = a.split('.').length; | ||
| const bDots = b.split('.').length; | ||
| if (aDots > bDots) return 1; | ||
| if (aDots < bDots) return -1; | ||
|
|
||
| if (a > b) return 1; | ||
| if (a < b) return -1; | ||
| return 0; | ||
| }; | ||
|
|
||
|
|
||
| function _defaultOutput(data) { | ||
| const flat = flattern(data); | ||
| const columns = _.keys(flat) | ||
| .sort(propertyComparator) | ||
| .map((key) => { | ||
| return { key, value: _.get(data, key) }; | ||
| }); | ||
| return columnify(columns, { columnSplitter: ' ' }); | ||
| } | ||
|
|
||
| function _formatter(format) { | ||
| switch (format) { | ||
| case 'yaml': | ||
| return yaml.safeDump; | ||
| case 'json': | ||
| return _jsonFormatter; | ||
| default: | ||
| return _defaultOutput; | ||
| } | ||
| } | ||
|
|
||
| function outputCliConfig(format, data) { | ||
| const formatter = _formatter(format); | ||
| console.log(formatter(data)); | ||
| } | ||
|
|
||
| function printProperties(properties) { | ||
| properties.sort(propertyComparator).forEach(prop => console.log(prop)); | ||
| } | ||
|
|
||
| function propertyErrorHandler(e) { | ||
| if (e instanceof NoPropertyError) { | ||
| console.log(`Property "${e.property}" is not supported`); | ||
| return; | ||
| } | ||
| if (e instanceof MultiplePropertiesError) { | ||
| console.log('Choose one of the following properties:\n'); | ||
| printProperties(e.properties); | ||
| return; | ||
| } | ||
| if (e instanceof NotFullPropertyError) { | ||
| console.log(`Did you mean property: ${e.property}?`); | ||
| return; | ||
| } | ||
| if (e instanceof SchemaValidationError) { | ||
| e.printErrors(); | ||
| return; | ||
| } | ||
| throw e; | ||
| } | ||
|
|
||
| module.exports = { | ||
| outputCliConfig, | ||
| printProperties, | ||
| propertyErrorHandler, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| class NoPropertyError extends Error { | ||
yaroslav-codefresh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| constructor(property) { | ||
| super(); | ||
| this.property = property; | ||
| } | ||
| } | ||
|
|
||
| class MultiplePropertiesError extends Error { | ||
| constructor(properties) { | ||
| super(); | ||
| this.properties = properties; | ||
| } | ||
| } | ||
|
|
||
| class NotFullPropertyError extends Error { | ||
| constructor(property) { | ||
| super(); | ||
| this.property = property; | ||
| } | ||
| } | ||
|
|
||
| class SchemaValidationError extends Error { | ||
| constructor(errors) { | ||
| super(); | ||
| this.errors = errors; | ||
| } | ||
|
|
||
| printErrors() { | ||
| this.errors.forEach((e) => { | ||
| console.log(`Validation error: property "${e.dataPath.replace('.', '')}" ${e.message}`); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| NoPropertyError, | ||
| MultiplePropertiesError, | ||
| NotFullPropertyError, | ||
| SchemaValidationError, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.