/*
 *  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');

module.exports = {
    command: 'download [directory]',
    desc: 'Downloads an app from a Cloudflow installation to directory',
    builder: function(yargs) {
        yargs
            .example('$0 app download', 'downloads the app in the current directory')
            .example('$0 app download /app_path/', 'downloads the app described in /app_path/project.cfapp')
            .option('overwrite', {
                describe: 'force overwriting files',
                default: false
            })
            .option('host', {
                describe: 'overrides the host address of the project.cfapp file'
            })
            .option('login', {
                alias: 'user',
                describe: 'overrides the login of the project.cfapp file'
            })
            .option('password', {
                describe: 'overrides the passowrd of the project.cfapp file'
            })
            .option('session', {
                describe: 'the session key that is used for the cloudflow api calls, when passed it overrides login and password'
            });
    },
    handler: function(argv) {
        const options = {
            overwrite: argv.overwrite,
            host: argv.host,
            login: argv.login,
            password: argv.password,
            session: argv.session
        };

        const directory = argv.directory || '.';

        // parse and stringify to get rid of 'undefined' values
        apps.download(directory, JSON.parse(JSON.stringify(options))).catch(function(error) {
            console.log(error.message);
        });
    }
};

@@ -325,16 +325,523 @@
 cfapp');
+%0Aconst ConsoleOutputStream = require('../../../lib/util/ConsoleOutputStream');%0Aconst JSONOutputStream = require('../../../lib/util/JSONOutputStream');%0A%0Afunction catchError(error, outputStream, jsonFormat) %7B%0A    if (jsonFormat === true) %7B%0A        console.log(JSON.stringify(%7B%0A            lines: outputStream.outputLines,%0A            error: %7B%0A                message: error.toString(),%0A                code: error.errorCode%0A            %7D%0A        %7D));%0A    %7D%0A    else %7B%0A        console.log(error.stack);%0A    %7D%0A%7D
 %0A%0Amodule
@@ -2226,195 +2226,575 @@
     
-// parse and stringify to get rid of 'undefined' values%0A        apps.download(directory, JSON.parse(JSON.stringify(options))).catch(function(error) %7B%0A            console.log(error.message
+let outputStream = new ConsoleOutputStream();%0A        if (argv.json === true) %7B%0A            outputStream = new JSONOutputStream();%0A        %7D%0A%0A        // parse and stringify to get rid of 'undefined' values%0A        apps.download(directory, JSON.parse(JSON.stringify(options)), outputStream).then(function () %7B%0A            if (argv.json === true) %7B%0A                console.log(JSON.stringify(%7B%0A                    lines: outputStream.outputLines%0A                %7D));%0A            %7D%0A        %7D).catch(function(error) %7B%0A            catchError(error, outputStream, argv.json
 );%0A 

Add JSON output for the download command