Skip to content
Merged
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bin": "./bin/run.js",
"bugs": "https://github.com/heroku/cli/issues",
"dependencies": {
"@heroku-cli/command": "13.0.0-beta.1",
"@heroku-cli/command": "13.0.0-beta.3",
"@heroku-cli/notifications": "^1.2.6",
"@heroku-cli/schema": "^1.0.25",
"@heroku/buildpack-registry": "^1.0.1",
Expand Down Expand Up @@ -378,7 +378,7 @@
"license": "ISC",
"main": "dist/index.js",
"overrides": {
"@heroku-cli/command": "13.0.0-beta.1",
"@heroku-cli/command": "13.0.0-beta.3",
"serialize-javascript": "^7.0.4"
},
"repository": "heroku/cli",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/accounts/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class Add extends Command {
const {name} = args
const logInMessage = 'You must be logged in to run this command.'

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

Expand Down
2 changes: 1 addition & 1 deletion src/commands/accounts/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Current extends Command {
static promptFlagActive = false

async run() {
const accountName = await AccountsModule.current()
const accountName = await AccountsModule.current(this.heroku)
if (accountName) {
hux.styledHeader(`Current account is ${color.user(accountName)}`)
} else {
Expand Down
9 changes: 5 additions & 4 deletions src/commands/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ export default class AccountsIndex extends Command {
static promptFlagActive = false

async run() {
const accounts = accountsModule.list()
const accounts = await accountsModule.list()
if (accounts.length === 0) {
ux.error('You don\'t have any accounts in your cache.')
}

const current = await accountsModule.current(this.heroku)
for (const account of accounts) {
if (account.name === await accountsModule.current()) {
ux.stdout(`* ${account.name}`)
if (account.name === current || account.username === current) {
ux.stdout(`* ${account.name ?? account.username}`)
} else {
ux.stdout(` ${account.name}`)
ux.stdout(` ${account.name ?? account.username}`)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/accounts/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export default class Remove extends Command {
const {args} = await this.parse(Remove)
const {name} = args

if (!AccountsModule.list().some(a => a.name === name)) {
if (!(await AccountsModule.list()).some(account => account.name === name)) {
ux.error(`${name} doesn't exist in your accounts cache.`)
}

if (await AccountsModule.current() === name) {
if (await AccountsModule.current(this.heroku) === name) {
ux.error(`${name} is the current account.`)
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/accounts/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class Set extends Command {
const {args} = await this.parse(Set)
const {name} = args

if (!AccountsModule.list().some(a => a.name === name)) {
if (!(await AccountsModule.list()).some(account => account.name === name)) {
ux.error(`${name} does not exist in your accounts cache.`)
}

Expand Down
3 changes: 1 addition & 2 deletions src/commands/git/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ export class GitCredentials extends Command {
return new Promise(resolve => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
})

const input: Record<string, string> = {}

rl.on('line', (line: string) => {
if (!line.trim()) {
rl.close()
setImmediate(() => rl.close())
return
}

Expand Down
50 changes: 41 additions & 9 deletions src/lib/accounts/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import {parse, stringify} from 'yaml'
import {APIClient, listKeychainAccounts, getStorageConfig} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import * as Heroku from '@heroku-cli/schema'
import {parse, stringify} from 'yaml'

export interface AccountEntry {
name?: string
username: string
}

export interface IAccountsWrapper {
list(): Heroku.Account[] | []
current(): Promise<string | null>
list(): Promise<AccountEntry[]>
current(heroku: APIClient): Promise<string | null>
add(name: string, username: string, password: string): void
remove(name: string): void
set(name: string): Promise<void>
Expand Down Expand Up @@ -50,21 +56,47 @@ export class AccountsWrapper implements IAccountsWrapper {
return account
}

list(): Heroku.Account[] | [] {
async getKeychainAccounts(): Promise<(string | null | undefined)[]> {
return listKeychainAccounts()
}

getStorageConfig() {
return getStorageConfig()
}

async list(): Promise<AccountEntry[]> {
const config = this.getStorageConfig()
if (config.credentialStore) {
const accounts = await this.getKeychainAccounts()
return accounts
.filter((account): account is string => account !== null && account !== undefined)
.map(account => ({username: account}))
}

return this.listNetrc()
}

listNetrc(): AccountEntry[] {
const basedir = path.join(this.configDir(), 'accounts')
try {
return fs.readdirSync(basedir)
.map(name => Object.assign(this.account(name), {name}))
.map(name => ({name, username: this.account(name).username ?? ''}))
} catch {
return []
}
}

async current(): Promise<string | null> {
async current(heroku: APIClient): Promise<string | null> {
const config = getStorageConfig()
if (config.credentialStore) {
const authEntry = await heroku.getAuthEntry()
return authEntry?.account ?? null
}

const netrcInstance = await this.initNetrc()
if (netrcInstance.machines['api.heroku.com']) {
const current = this.list().find(a => a.username === netrcInstance.machines['api.heroku.com'].login)
return current && current.name ? current.name : null
const current = this.listNetrc().find(a => a.username === netrcInstance.machines['api.heroku.com'].login)
return current?.name ?? null
}

return null
Expand Down
2 changes: 1 addition & 1 deletion test/unit/commands/accounts/add.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('accounts:add', function () {
let addStub: sinon.SinonStub

beforeEach(function () {
sinon.stub(AccountsModule, 'list').returns([])
sinon.stub(AccountsModule, 'list').resolves([])
addStub = sinon.stub(AccountsModule, 'add')
api = nock('https://api.heroku.com')
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/commands/accounts/current.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ describe('accounts:current', function () {
})

it('should print the name of the current account if an account is found', async function () {
currentStub.returns('test-account')
currentStub.resolves('test-account')
await runCommand(Cmd, [])
expect(stdout.output).to.contain('test-account')
})

it('should print an error message if no account is found', async function () {
currentStub.returns(null)
currentStub.resolves(null)
await runCommand(Cmd, [])
.catch((error: Error) => {
expect(ansis.strip(error.message)).to.equal('You haven\'t set an account. Run heroku accounts:add <account-name> to add an account to your cache or heroku accounts:set <account-name> to set the current account.')
Expand Down
4 changes: 2 additions & 2 deletions test/unit/commands/accounts/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe('accounts', function () {

it('should print a list of added accounts with the current account highlighted if accounts are found', async function () {
currentStub.resolves('test-account')
listStub.returns([{name: 'test-account'}, {name: 'test-account-2'}])
listStub.resolves([{name: 'test-account', username: 'user1'}, {name: 'test-account-2', username: 'user2'}])
await runCommand(Cmd, [])
expect(stdout.output).to.equal('* test-account\n test-account-2\n')
})

it('should print an error message if no accounts are found', async function () {
listStub.returns([])
listStub.resolves([])
await runCommand(Cmd, [])
.catch((error: Error) => {
expect(error.message).to.contain('You don\'t have any accounts in your cache.')
Expand Down
12 changes: 6 additions & 6 deletions test/unit/commands/accounts/remove.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ describe('accounts:remove', function () {
})

it('calls the remove function with the account name when the account exists and it is not the current account', async function () {
currentStub.returns('test-account')
listStub.returns([{name: 'test-account'}, {name: 'test-account-2'}])
currentStub.resolves('test-account')
listStub.resolves([{name: 'test-account', username: 'user1'}, {name: 'test-account-2', username: 'user2'}])
await runCommand(Cmd, ['test-account-2'])
expect(removeStub.calledWith('test-account-2'))
})

it('should return an error if the selected account name is not included in the account list', async function () {
currentStub.returns('test-account')
listStub.returns([{name: 'test-account'}, {name: 'test-account-2'}])
currentStub.resolves('test-account')
listStub.resolves([{name: 'test-account', username: 'user1'}, {name: 'test-account-2', username: 'user2'}])
await runCommand(Cmd, ['test-account-3'])
.catch((error: Error) => {
expect(error.message).to.contain('test-account-3 doesn\'t exist in your accounts cache.')
})
})

it('should return an error if the selected account name is the current account', async function () {
currentStub.returns('test-account')
listStub.returns([{name: 'test-account'}, {name: 'test-account-2'}])
currentStub.resolves('test-account')
listStub.resolves([{name: 'test-account', username: 'user1'}, {name: 'test-account-2', username: 'user2'}])
await runCommand(Cmd, ['test-account'])
.catch((error: Error) => {
expect(error.message).to.contain('test-account is the current account.')
Expand Down
4 changes: 2 additions & 2 deletions test/unit/commands/accounts/set.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ describe('accounts:set', function () {
})

it('calls the set function with the account name when the account exists', async function () {
listStub.returns([{name: 'test-account'}, {name: 'test-account-2'}])
listStub.resolves([{name: 'test-account', username: 'user1'}, {name: 'test-account-2', username: 'user2'}])
await runCommand(Cmd, ['test-account-2'])
expect(setStub.calledWith('test-account-2'))
})

it('should return an error if the selected account name is not included in the account list', async function () {
listStub.returns([{name: 'test-account'}, {name: 'test-account-2'}])
listStub.resolves([{name: 'test-account', username: 'user1'}, {name: 'test-account-2', username: 'user2'}])
await runCommand(Cmd, ['test-account-3'])
.catch((error: Error) => {
expect(error.message).to.contain('test-account-3 does not exist in your accounts cache.')
Expand Down
Loading
Loading