<commit_before>/*
 *  Copyright (c) 2017 NiXPS, All rights reserved.
 *
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this
 *  file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

'use strict';

const { apps } = require('../../../lib/cfapp');
const columnify = require('columnify');
const cloudflowAPI = require('cloudflow-api');

module.exports = {
    command: 'list',
    desc: 'Lists all the installed Cloudflow Application on the remote server',
    builder: function(yargs) {
        yargs.example('$0 app list --login admin --password <PASSWORD>', 'lists all of installed Cloudflow Applications on http://localhost:9090')
            .example('$0 app list --host http://my.server.com:9090 --login admin --password <PASSWORD>', 'lists all installed Cloudflow Applications on http://my.server.com:9090')
            .option('login', {
                describe: 'overrides the login of the project.cfapp file'
            })
            .option('password', {
                describe: 'overrides the password of the project.cfapp file'
            });
    },
    handler: function(argv) {
        const options = {
            host: argv.host,
            login: argv.login,
            password: argv.password
        };

        const serverURL = argv.host || 'http://localhost:9090';

        // Check if we can list applications
        const api = cloudflowAPI.getSyncAPI(serverURL);
        var session = api.auth.create_session(options.login, options.password).session;
        api.m_session = session;

        if (apps.canRegisterApps(api) === false) {
            console.log(`no support for application listing this Cloudflow build b${api.portal.version().build}`);
            return;
        }

        // parse and stringify to get rid of 'undefined' values
        try {
            const appList = apps.list(serverURL, JSON.parse(JSON.stringify(options)));
            const appListTable = appList.map((app) => {
                return {
                    name: app.name,
                    version: app.version || 'no version',
                    description: app.description
                };
            });
            console.log(columnify(appListTable, {
                columnSplitter: ' | ',
                minWidth: 30,
                config: {
                    version: { minWidth: 15 },
                    description: { maxWidth: 50 }
                }
            }));
        }
        catch(error) {
            console.log(error);
        }
    }
};
<commit_msg>Add a --json parameter to the list command<commit_after>/*
 *  Copyright (c) 2017 NiXPS, All rights reserved.
 *
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this
 *  file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

'use strict';

const { apps } = require('../../../lib/cfapp');
const columnify = require('columnify');
const cloudflowAPI = require('cloudflow-api');

module.exports = {
    command: 'list',
    desc: 'Lists all the installed Cloudflow Application on the remote server',
    builder: function(yargs) {
        yargs.example('$0 app list --login admin --password <PASSWORD>', 'lists all of installed Cloudflow Applications on http://localhost:9090')
            .example('$0 app list --host http://my.server.com:9090 --login admin --password <PASSWORD>', 'lists all installed Cloudflow Applications on http://my.server.com:9090')
            .option('login', {
                describe: 'overrides the login of the project.cfapp file'
            })
            .option('password', {
                describe: 'overrides the password of the project.cfapp file'
            })
            .option('json', {
                describe: 'returns a json representation of the list of cfapps',
                default: false
            });
    },
    handler: function(argv) {
        const options = {
            host: argv.host,
            login: argv.login,
            password: argv.password,
            json: argv.json
        };

        const serverURL = argv.host || 'http://localhost:9090';

        // Check if we can list applications
        const api = cloudflowAPI.getSyncAPI(serverURL);
        var session = api.auth.create_session(options.login, options.password).session;
        api.m_session = session;

        if (apps.canRegisterApps(api) === false) {
            console.log(`no support for application listing this Cloudflow build b${api.portal.version().build}`);
            return;
        }

        // parse and stringify to get rid of 'undefined' values
        try {
            const appList = apps.list(serverURL, JSON.parse(JSON.stringify(options)));

            if (options.json === true) {
                console.log(appList);
                return;
            }

            const appListTable = appList.map((app) => {
                return {
                    name: app.name,
                    version: app.version || 'no version',
                    description: app.description
                };
            });
            console.log(columnify(appListTable, {
                columnSplitter: ' | ',
                minWidth: 30,
                config: {
                    version: { minWidth: 15 },
                    description: { maxWidth: 50 }
                }
            }));
        }
        catch(error) {
            console.log(error);
        }
    }
};

{'fraction_non_alphanumeric': 0.11704422869471413, 'fraction_numerical': 0.011506652283351312, 'mean_word_length': 1.7662854301342616, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1517133', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1433, 'n_words': 425}