Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/cli/src/commands/accounts/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {Command} from '@heroku-cli/command'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
import {add, list} from '../../lib/accounts/accounts'
import {FlagInput} from '@oclif/core/lib/interfaces/parser'
import {confirm} from '@inquirer/prompts'
import * as open from 'open'


export default class Add extends Command {
static description = 'add a Heroku account to your cache'
Expand All @@ -10,17 +14,34 @@ export default class Add extends Command {
name: Args.string({description: 'name of Heroku account to add', required: true}),
}

static flags: FlagInput = {
sso: flags.boolean({char: 's', description: 'login for enterprise users under SSO'}),
}

static example = 'heroku accounts:add my-account'

async run() {
const {args} = await this.parse(Add)
const {args, flags} = await this.parse(Add)
const {name} = args
const {sso} = flags
const logInMessage = 'You must be logged in to run this command.'
const dashboardUrl = 'https://dashboard.heroku.com'
let redirectToDashboard = false

if (list().some(a => a.name === name)) {
ux.error(`${name} already exists`)
}

if (!sso) {
ux.warn('You may be signed into a different Heroku account from the browser.')
redirectToDashboard = await confirm({default: false, message: `Redirect to Dashboard for sign out?`, theme: {prefix: '', style: {defaultAnswer: () => '(Y/N)'}}})

if (redirectToDashboard) {
return await open(dashboardUrl)
}
}

await this.heroku.login({method: sso ? 'sso' : 'browser'})
const {body: account} = await this.heroku.get<Heroku.Account>('/account')
const email = account.email || ''

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/test/unit/commands/accounts/add.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import * as nock from 'nock'
import * as sinon from 'sinon'
import Cmd from '../../../../src/commands/accounts/add'
import * as accounts from '../../../../src/lib/accounts/accounts'
import {APIClient} from '@heroku-cli/command'

describe('accounts:add', function () {
let api: nock.Scope
let addStub: sinon.SinonStub
let loginStub: sinon.SinonStub

beforeEach(function () {
sinon.stub(accounts, 'list').returns([])
addStub = sinon.stub(accounts, 'add')
loginStub = sinon.stub(APIClient.prototype, 'login').resolves()
api = nock('https://api.heroku.com')
})

Expand Down
Loading