'use strict';

var Command = require('ronin').Command;
var utils = require('../../utils');
var bs58 = require('bs58');
var debug = require('debug');
var log = debug('cli:object');
log.error = debug('cli:object:error');

module.exports = Command.extend({
  desc: 'Create new ipfs objects',

  options: {},

  run: function run(template) {
    utils.getIPFS(function (err, ipfs) {
      if (err) {
        throw err;
      }
      ipfs.object.new(template, function (err, obj) {
        if (err) {
          log.error(err);
          throw err;
        }

        if (typeof obj.Hash === 'string') {
          // js-ipfs-api output
          console.log(obj.Hash);
          return;
        }

        console.log(bs58.encode(obj.Hash).toString());
      });
    });
  }
});