Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const categoriesOrder = {
compositions : 110 ,
'helm repos' : 111 ,
'predefined pipelines': 120,
'cli config': 121,
teams: 130,
more : 150,
};
Expand Down
9 changes: 9 additions & 0 deletions lib/constants.js
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,
};
34 changes: 34 additions & 0 deletions lib/interface/cli/commands/cli-config/cli-config.cmd.js
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;
46 changes: 46 additions & 0 deletions lib/interface/cli/commands/cli-config/ops/get.cmd.js
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;
44 changes: 44 additions & 0 deletions lib/interface/cli/commands/cli-config/ops/profile.cmd.js
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`);
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;
55 changes: 55 additions & 0 deletions lib/interface/cli/commands/cli-config/ops/set.cmd.js
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;
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ const debug = require('debug')('codefresh:cli:create:context');
const Command = require('../../Command');
const fs = require('fs');
const { spawn } = require('child_process');
const { homedir } = require('os');
const rp = require('request-promise');
const createRoot = require('../root/create.cmd');
const authManager = require('../../../../logic/auth').manager; // eslint-disable-line

const { codefreshPath } = require('../../../../constants');
const scriptUrl = 'https://raw.githubusercontent.com/codefresh-io/k8s-dind-config/master/codefresh-k8s-configure.sh';
let filePath = `${homedir()}/.Codefresh/runtime/codefresh-k8s-configure.sh`;
const dirPath = `${homedir()}/.Codefresh/runtime`;
const codefreshPath = `${homedir()}/.Codefresh`;
let filePath = `${codefreshPath}/runtime/codefresh-k8s-configure.sh`;
const dirPath = `${codefreshPath}/runtime`;


const callToScript = (k8sScript) =>{
Expand Down
76 changes: 76 additions & 0 deletions lib/interface/cli/helpers/cli-config.js
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) => {
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,
};
11 changes: 6 additions & 5 deletions lib/logic/api/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const decompress = require('decompress');
const decompressTargz = require('decompress-targz');
const compareVersions = require('compare-versions');

const { codefreshPath } = require('../../constants');

const _createClusterScript = (info, filePath) => {
const { name, context } = info;
fs.chmodSync(filePath, '755');
Expand Down Expand Up @@ -46,12 +48,11 @@ const getAllClusters = async () => {
};

const createCluster = async (info) => {
const dirPath = `${homedir()}/.Codefresh/cluster`;
const filePath = `${homedir()}/.Codefresh/cluster/stevedore`;
const versionPath = `${homedir()}/.Codefresh/cluster/version.txt`;
const dirPath = `${codefreshPath}/cluster`;
const filePath = `${codefreshPath}/cluster/stevedore`;
const versionPath = `${codefreshPath}/cluster/version.txt`;
const versionUrl = 'https://raw.githubusercontent.com/codefresh-io/Stevedore/master/VERSION';
const codefreshPath = `${homedir()}/.Codefresh`;
let zipPath = `${homedir()}/.Codefresh/cluster/data`;
let zipPath = `${codefreshPath}/cluster/data`;
let shouldUpdate = true;
const options = {
url: versionUrl,
Expand Down
40 changes: 40 additions & 0 deletions lib/logic/cli-config/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class NoPropertyError extends Error {
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,
};
Loading