<commit_before>/*
 * Copyright 2013-2015, All Rights Reserved.
 *
 * Code licensed under the BSD License:
 * https://github.com/node-gh/gh/blob/master/LICENSE.md
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

'use strict';

// -- Requires -------------------------------------------------------------------------------------

var async = require('async'),
    base = require('../base'),
    hooks = require('../hooks'),
    inquirer = require('inquirer'),
    logger = require('../logger'),
    openUrl = require('open');

// -- Constructor ----------------------------------------------------------------------------------

function Gists(options) {
    this.options = options;
}

// -- Constants ------------------------------------------------------------------------------------

Gists.DETAILS = {
    alias: 'gi',
    description: 'Provides a set of util commands to work with Gists.',
    iterative: 'delete',
    commands: [
        'browser',
        'delete',
        'fork',
        'list',
        'new'
    ],
    options: {
        'browser': Boolean,
        'content': String,
        'delete': [String, Array],
        'description': String,
        'fork': String,
        'id': String,
        'list': Boolean,
        'new': String,
        'private': Boolean,
        'user': String
    },
    shorthands: {
        'B': ['--browser'],
        'c': ['--content'],
        'D': ['--delete'],
        'd': ['--description'],
        'f': ['--fork'],
        'i': ['--id'],
        'l': ['--list'],
        'N': ['--new'],
        'p': ['--private'],
        'u': ['--user']
    },
    payload: function(payload, options) {
        options.list = true;
    }
};

// -- Commands -------------------------------------------------------------------------------------

Gists.prototype.run = function() {
    var instance = this,
        operations,
        options = instance.options;

    if (options.paste) {
        logger.error('Sorry, this functionality was removed.');
        return;
    }

    if (options.browser) {
        instance.browser(options.id || options.loggedUser);
    }

    if (options.delete) {
        hooks.invoke('gists.delete', instance, function(afterHooksCallback) {
            logger.logTemplate(
                '[info] Deleting gist {{greenBright options.loggedUser "/" options.delete}}', {
                    options: options
                });

            inquirer.prompt(
                [
                    {
                        type: 'input',
                        message: 'Are you sure? This action CANNOT be undone. [y/N]',
                        name: 'confirmation'
                    }
                ], function(answers) {
                    if (answers.confirmation.toLowerCase() === 'y') {
                        instance.delete(options.delete, function(err) {
                            if (err) {
                                logger.error('Can\'t delete gist.');
                                return;
                            }
                        });

                        afterHooksCallback();
                    }
                    else {
                        logger.info('Not deleted.');
                    }
                });
        });
    }

    if (options.fork) {
        hooks.invoke('gists.fork', instance, function(afterHooksCallback) {
            logger.logTemplate(
                '[info] Forking gist on {{greenBright options.loggedUser}}', {
                    options: options
                });

            instance.fork(options.fork, function(err) {
                if (err) {
                    logger.error('Can\'t fork gist.');
                    return;
                }

                logger.compileTemplate(
                    'https://gist.github.com/{{options.id}}', {
                    options: options
                });

                afterHooksCallback();
            });
        });
    }

    if (options.list) {
        logger.logTemplate(
            '[info] Listing gists for {{greenBright options.user}}', {
                options: options
            });

        instance.list(options.user, function(err) {
            if (err) {
                logger.error('Can\'t list gists for ' + options.user + '.');
                return;
            }
        });
    }

    if (options.new) {
        hooks.invoke('gists.new', instance, function(afterHooksCallback) {
            operations = [];
            options.new = options.new;

            logger.logTemplateFile('gi-new.handlebars', {
                options: options,
                privacy: options.private ? 'private' : 'public'
            });

            instance.new(options.new, options.content, function(err, gist) {
                if (gist) {
                    options.id = gist.id;
                }

                if (err) {
                    logger.error('Can\'t create gist.');
                    return;
                }

                logger.compileTemplate('{{gistLink}}', {
                    options: options
                });

                afterHooksCallback();
            });
        });
    }
};

Gists.prototype.browser = function(gist) {
    openUrl('https://gist.github.com/' + gist);
};

Gists.prototype.delete = function(id, opt_callback) {
    var payload = {
        id: id
    };

    base.github.gists.delete(payload, opt_callback);
};

Gists.prototype.fork = function(id, opt_callback) {
    var payload = {
        id: id
    };

    base.github.gists.fork(payload, opt_callback);
};

Gists.prototype.list = function(user, opt_callback) {
    var instance = this,
        payload;

    payload = {
        user: user
    };

    base.github.gists.getFromUser(payload, function(err, gists) {
        instance.listCallback_(err, gists, opt_callback);
    });
};

Gists.prototype.listCallback_ = function(err, gists, opt_callback) {
    var instance = this,
        options = instance.options;

    if (err && !options.all) {
        logger.error(logger.getErrorMessage(err));
    }

    if (gists && gists.length > 0) {
        logger.logTemplateFile('gi-list.handlebars', {
            gists: gists,
            user: options.user
        });

        opt_callback && opt_callback(err);
    }
};

Gists.prototype.new = function(name, content, opt_callback) {
    var instance = this,
        file = {},
        options = instance.options,
        payload;

    options.description = options.description || '';

    file[name] = {
        content: content
    };

    payload = {
        description: options.description,
        files: file,
        public: !options.private
    };

    base.github.gists.create(payload, opt_callback);
};

exports.Impl = Gists;
<commit_msg>Improving gists error messages.<commit_after>/*
 * Copyright 2013-2015, All Rights Reserved.
 *
 * Code licensed under the BSD License:
 * https://github.com/node-gh/gh/blob/master/LICENSE.md
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

'use strict';

// -- Requires -------------------------------------------------------------------------------------

var async = require('async'),
    base = require('../base'),
    hooks = require('../hooks'),
    inquirer = require('inquirer'),
    logger = require('../logger'),
    openUrl = require('open');

// -- Constructor ----------------------------------------------------------------------------------

function Gists(options) {
    this.options = options;
}

// -- Constants ------------------------------------------------------------------------------------

Gists.DETAILS = {
    alias: 'gi',
    description: 'Provides a set of util commands to work with Gists.',
    iterative: 'delete',
    commands: [
        'browser',
        'delete',
        'fork',
        'list',
        'new'
    ],
    options: {
        'browser': Boolean,
        'content': String,
        'delete': [String, Array],
        'description': String,
        'fork': String,
        'id': String,
        'list': Boolean,
        'new': String,
        'private': Boolean,
        'user': String
    },
    shorthands: {
        'B': ['--browser'],
        'c': ['--content'],
        'D': ['--delete'],
        'd': ['--description'],
        'f': ['--fork'],
        'i': ['--id'],
        'l': ['--list'],
        'N': ['--new'],
        'p': ['--private'],
        'u': ['--user']
    },
    payload: function(payload, options) {
        options.list = true;
    }
};

// -- Commands -------------------------------------------------------------------------------------

Gists.prototype.run = function() {
    var instance = this,
        operations,
        options = instance.options;

    if (options.paste) {
        logger.error('Sorry, this functionality was removed.');
        return;
    }

    if (options.browser) {
        instance.browser(options.id || options.loggedUser);
    }

    if (options.delete) {
        hooks.invoke('gists.delete', instance, function(afterHooksCallback) {
            logger.logTemplate(
                '[info] Deleting gist {{greenBright options.loggedUser "/" options.delete}}', {
                    options: options
                });

            inquirer.prompt(
                [
                    {
                        type: 'input',
                        message: 'Are you sure? This action CANNOT be undone. [y/N]',
                        name: 'confirmation'
                    }
                ], function(answers) {
                    if (answers.confirmation.toLowerCase() === 'y') {
                        instance.delete(options.delete, function(err) {
                            if (err) {
                                logger.error('Can\'t delete gist.');
                                return;
                            }
                        });

                        afterHooksCallback();
                    }
                    else {
                        logger.info('Not deleted.');
                    }
                });
        });
    }

    if (options.fork) {
        hooks.invoke('gists.fork', instance, function(afterHooksCallback) {
            logger.logTemplate(
                '[info] Forking gist on {{greenBright options.loggedUser}}', {
                    options: options
                });

            instance.fork(options.fork, function(err) {
                if (err) {
                    logger.error(JSON.parse(err.message).message);
                    return;
                }

                logger.compileTemplate(
                    'https://gist.github.com/{{options.id}}', {
                    options: options
                });

                afterHooksCallback();
            });
        });
    }

    if (options.list) {
        logger.logTemplate(
            '[info] Listing gists for {{greenBright options.user}}', {
                options: options
            });

        instance.list(options.user, function(err) {
            if (err) {
                logger.error('Can\'t list gists for ' + options.user + '.');
                return;
            }
        });
    }

    if (options.new) {
        hooks.invoke('gists.new', instance, function(afterHooksCallback) {
            operations = [];
            options.new = options.new;

            logger.logTemplateFile('gi-new.handlebars', {
                options: options,
                privacy: options.private ? 'private' : 'public'
            });

            instance.new(options.new, options.content, function(err, gist) {
                if (gist) {
                    options.id = gist.id;
                }

                if (err) {
                    logger.error('Can\'t create gist. ' + JSON.parse(err.message).message);
                    return;
                }

                logger.compileTemplate('{{gistLink}}', {
                    options: options
                });

                afterHooksCallback();
            });
        });
    }
};

Gists.prototype.browser = function(gist) {
    openUrl('https://gist.github.com/' + gist);
};

Gists.prototype.delete = function(id, opt_callback) {
    var payload = {
        id: id
    };

    base.github.gists.delete(payload, opt_callback);
};

Gists.prototype.fork = function(id, opt_callback) {
    var payload = {
        id: id
    };

    base.github.gists.fork(payload, opt_callback);
};

Gists.prototype.list = function(user, opt_callback) {
    var instance = this,
        payload;

    payload = {
        user: user
    };

    base.github.gists.getFromUser(payload, function(err, gists) {
        instance.listCallback_(err, gists, opt_callback);
    });
};

Gists.prototype.listCallback_ = function(err, gists, opt_callback) {
    var instance = this,
        options = instance.options;

    if (err && !options.all) {
        logger.error(logger.getErrorMessage(err));
    }

    if (gists && gists.length > 0) {
        logger.logTemplateFile('gi-list.handlebars', {
            gists: gists,
            user: options.user
        });

        opt_callback && opt_callback(err);
    }
};

Gists.prototype.new = function(name, content, opt_callback) {
    var instance = this,
        file = {},
        options = instance.options,
        payload;

    options.description = options.description || '';

    file[name] = {
        content: content
    };

    payload = {
        description: options.description,
        files: file,
        public: !options.private
    };

    base.github.gists.create(payload, opt_callback);
};

exports.Impl = Gists;

{'fraction_non_alphanumeric': 0.1846255409667718, 'fraction_numerical': 0.0013203256803344826, 'mean_word_length': 1.2968328840970351, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 6, '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': '63930', 'n_tokens_mistral': 3712, 'n_tokens_neox': 3548, 'n_words': 704}