Giter Club home page Giter Club logo

heroku-cli-util's Introduction

Heroku CLI Utilities

GitHub Actions CI npm version License

Set of helpful CLI utilities

Installation

npm install heroku-cli-util --save

Action

let cli = require('heroku-cli-util');
await cli.action('restarting dynos', async function() {
  let app = await heroku.get(`/apps/${context.app}`);
  await heroku.request({method: 'DELETE', path: `/apps/${app.name}/dynos`});
});

// restarting dynos... done

Prompt

let cli   = require('heroku-cli-util');
let email = await cli.prompt('email', {});
console.log(`your email is: ${email}`);

cli.prompt options

cli.prompt('email', {
  mask: true, // mask input field after submitting
  hide: true // mask characters while entering
});

Confirm App

Supports the same async styles as prompt(). Errors if not confirmed.

Basic

let cli = require('heroku-cli-util');
await cli.confirmApp('appname', context.flags.confirm);

// !     WARNING: Destructive Action
// !     This command will affect the app appname
// !     To proceed, type appname or re-run this command with --confirm appname

> appname

Custom message

let cli = require('heroku-cli-util');
await cli.confirmApp('appname', context.flags.confirm, 'foo');

// !     foo
// !     To proceed, type appname or re-run this command with --confirm appname

> appname

Note that you will still need to define a confirm flag for your command.

Errors

let cli = require('heroku-cli-util');
cli.error("App not found");
// !    App not found

Warnings

let cli = require('heroku-cli-util');
cli.warn("App not found");
// !    App not found

Dates

let cli = require('heroku-cli-util');
let d   = new Date();
console.log(cli.formatDate(d));
// 2001-01-01T08:00:00.000Z

Hush

Use hush for verbose logging when HEROKU_DEBUG=1.

let cli = require('heroku-cli-util');
cli.hush('foo');
// only prints if HEROKU_DEBUG is set

Debug

Pretty print an object.

let cli = require('heroku-cli-util');
cli.debug({foo: [1,2,3]});
// { foo: [ 1, 2, 3 ] }

Stylized output

Pretty print a header, hash, and JSON

let cli = require('heroku-cli-util');
cli.styledHeader("MyApp");
cli.styledHash({name: "myapp", collaborators: ["[email protected]", "[email protected]"]});
cli.styledJSON({name: "myapp"});

Produces

=== MyApp
Collaborators: [email protected]
               [email protected]
Name:          myapp

{
  "name": "myapp"
}

Table

cli.table([
  {app: 'first-app',  language: 'ruby', dyno_count: 3},
  {app: 'second-app', language: 'node', dyno_count: 2},
], {
  columns: [
    {key: 'app'},
    {key: 'dyno_count', label: 'Dyno Count'},
    {key: 'language', format: language => cli.color.red(language)},
  ]
});

Produces:

app         Dyno Count  language
──────────  ──────────  ────────
first-app   3           ruby
second-app  2           node

Linewrap

Used to indent output with wrapping around words:

cli.log(cli.linewrap(2, 10, 'this is text is longer than 10 characters'));
// Outputs:
//
// this
// text is
//  longer
//  than 10
//  characters`);

Useful with process.stdout.columns || 80.

Open Web Browser

await cli.open('https://github.com');

HTTP calls

heroku-cli-util includes an instance of got that will correctly use HTTP proxies.

let cli = require('heroku-cli-util');
let rsp = await cli.got('https://google.com');

Mocking

Mock stdout and stderr by using cli.log() and cli.error().

let cli = require('heroku-cli-util');
cli.log('message 1'); // prints 'message 1'
cli.mockConsole();
cli.log('message 2'); // prints nothing
cli.stdout.should.eq('message 2\n');

Command

Used for initializing a plugin command. give you an auth'ed instance of heroku-client and cleanly handle API exceptions.

It expects you to return a promise chain. This is usually done with co.

let cli = require('heroku-cli-util');
let co  = require('co');
module.exports.commands = [
  {
    topic: 'apps',
    command: 'info',
    needsAuth: true,
    needsApp: true,
    run: cli.command(async function (context, heroku) {
      let app = await heroku.get(`/apps/${context.app}`);
      console.dir(app);
    })
  }
];

With options:

let cli = require('heroku-cli-util');
let co  = require('co');
module.exports.commands = [
  {
    topic: 'apps',
    command: 'info',
    needsAuth: true,
    needsApp: true,
    run: cli.command(
      {preauth: true},
      async function (context, heroku) {
        let app = await heroku.get(`/apps/${context.app}`);
        console.dir(app);
      }
    )
  }
];

If the command has a two_factor API error, it will ask the user for a 2fa code and retry. If you set preauth: true it will preauth against the current app instead of just setting the header on an app. (This is necessary if you need to do more than 1 API call that will require 2fa)

Tests

npm install
npm test

License

ISC

heroku-cli-util's People

Contributors

beanieboi avatar chadian avatar dependabot[bot] avatar elbandito avatar fivetanley avatar hunterloftis avatar jdx avatar jheikes515 avatar jkutner avatar k80bowman avatar karatecowboy avatar msakrejda avatar rasphilco avatar raulb avatar sbosio avatar svc-scm avatar tcrayford avatar tevanoff avatar xavdid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

heroku-cli-util's Issues

Invalid left-hand side in assignment

Hey guys! The when I require the module, I'm getting what looks like a syntax error?

% node
> let cli = require('heroku-cli-util')
/Users/david/projects/heroku-config/node_modules/heroku-cli-util/lib/action.js:16
  if (options.then) [options, promise] = [{}, options]
                    ^^^^^^^^^^^^^^^^^^
ReferenceError: Invalid left-hand side in assignment
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/Users/david/projects/heroku-config/node_modules/heroku-cli-util/index.js:11:18)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

Versions
Node: 5.7.1
heroku-cli-util: 6.0.13

For now I'll just downgrade, but I thought it's something you might be interested in. Thanks!

Edit: The last version without this error is 6.0.3.

Cannot assign to read only property 'has256'

Since this morning i'm getting this error:

2016/04/08 09:38:28 truncating error log...
2016/04/08 09:38:28 cleaning up tmp dirs...
2016/04/08 09:38:28 heroku-cli: Installing core plugins...
2016/04/08 09:38:28 open /var/lib/buildkite-agent/.heroku/config.json: no such file or directory
2016/04/08 09:39:02  done
2016/04/08 09:39:02 open /var/lib/buildkite-agent/.heroku/config.json: no such file or directory
2016/04/08 09:39:02 open /var/lib/buildkite-agent/.heroku/config.json: no such file or directory
2016/04/08 09:39:04 TypeError: Cannot assign to read only property 'has256' of false
    at findBestHerokuColor (/var/lib/buildkite-agent/.heroku/node_modules/heroku-run/node_modules/heroku-cli-util/lib/spinner.js:9:19)
    at new Spinner (/var/lib/buildkite-agent/.heroku/node_modules/heroku-run/node_modules/heroku-cli-util/lib/spinner.js:28:23)
    at Run.updateStatus (/var/lib/buildkite-agent/.heroku/node_modules/heroku-run/commands/run.js:41:22)
    at Run.<anonymous> (/var/lib/buildkite-agent/.heroku/node_modules/heroku-run/commands/run.js:21:12)
    at next (native)
    at onFulfilled (/var/lib/buildkite-agent/.heroku/node_modules/co/index.js:65:19)
    at _fulfilled (/var/lib/buildkite-agent/.heroku/node_modules/q/q.js:834:54)
    at self.promiseDispatch.done (/var/lib/buildkite-agent/.heroku/node_modules/q/q.js:863:30)
    at Promise.promise.promiseDispatch (/var/lib/buildkite-agent/.heroku/node_modules/q/q.js:796:13)
    at /var/lib/buildkite-agent/.heroku/node_modules/q/q.js:604:44

This may be connected to your latest changed @dickeyxxx . Do you have any idea what went wrong?

Undefined property missing or not caught

Need to explore more, but:

$ heroku local:run bin/rails -v
[OKAY] Loaded ENV .env File as KEY=VALUE Format
Could not find i18n-0.7.0 in any of the sources
Run `bundle install` to install missing gems.
TypeError: Cannot read property 'body' of undefined
    at getErrorMessage (/Users/user/src/github.com/heroku/heroku-local/node_modules/heroku-cli-util/lib/errors.js:33:10)
    at error (/Users/user/src/github.com/heroku/heroku-local/node_modules/heroku-cli-util/lib/errors.js:53:30)
    at handleErr (/Users/user/src/github.com/heroku/heroku-local/node_modules/heroku-cli-util/lib/errors.js:100:23)
    at process._tickCallback (internal/process/next_tick.js:109:7)

API Caching

I think it would be useful, especially for slow commands like heroku addons and heroku dashboard if the CLI cached some common requests.

heroku-client already supports caching, we could store them in XDG_CACHE_HOME/heroku, so this change wouldn't be hard.

There may be some security (not serious ones, the .netrc file is already on disk) and disk space implications of caching everything indiscriminately, so a whitelist of only items we want to cache is important here. We probably want to chmod the files down to 0600 as well.

This depends on the result of heroku/node-heroku-client#66 I think. I want to make sure we get the caching right before just slapping this in.

Advanced Errors

Idea for new functionality for errors. Using restify-errors as inspiration, we could have a Heroku error type to generate custom errors.

Property ideas:

  • URL for docs
  • file?
  • example usage
  • error title
  • error description
  • plugin name
  • topic:command

When two factor is requested, would it be possible to preauth globally?

@dickeyxxx @mikehale pointed out that when he was doing heroku config on a 2fa app, it would prompt over an over, until he ran heroku apps:info after which heroku config stopped prompting. He was wondering the difference and it comes down to preAuth being set in apps:info. Besides performance, is there a reason that preAuth behavior is not the default. Having this behavior appear & disappear does seem inconsistent.

return cli.preauth(context.app, heroku(context), secondFactor)

run.js uses nonexistent "shortHelp" property

The command list printing code at run.js line 12 tries to print a shortHelp property from the command object, but neither of the example plugins provides such a string, and heroku-cli's Command struct doesn't have such a field. If I had to guess, I'd say that this project should use the description field instead, but it's possible that I've misunderstood the meaning of the various help-related keys, or that shortHelp is correct and everything else should be updated instead.

Clarify usage of this vs newer(?) modules

Hi there!

I was reading through some docs and noticed they've changed a lot since I wrote my plugin last year. The biggest difference is the introduction of cli-engine-heroku, heavily featured in the intro article. Looks pretty slick!

I'd love to see some documentation on future plans for the modules (is cli-util getting deprecated in favor of cli-engine?). For instance, if I was starting a new module today, which should I use? Is it worth updating an existing plugin with the new modules? If it's not right now (docs not ready, whatever), is there a planned point where that's the recommended action?

Related, the in the code link in this section is 404.

Add pretty object and table print

The ruby CLI has nice functions for printing an object including a header and indenting. e.g.

=== example
Source type: github
Source repo: heroku/example
Staging:     example-staging
Production:  example
             example-admin
Flow:        example-staging --> example, example-admin

It would be great to have that in here so we have consistent output. Likewise for tables.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.