Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
fix: unify log output formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 15, 2021
1 parent 401cc9b commit b697050
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 90 deletions.
23 changes: 10 additions & 13 deletions cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as program from 'commander'
import * as chalk from 'chalk'
import * as path from 'path'
import * as fs from 'fs'
import { createCARootCommand } from './commands/create-ca-root'
Expand All @@ -19,6 +18,7 @@ import { flashCommand } from './commands/flash'
import { ioTHubDPSInfo } from './iot/ioTHubDPSInfo'
import { creds } from './creds'
import { functionsSettingsCommand } from './commands/functions-settings'
import { error, help } from './logging'

const version = JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8'),
Expand Down Expand Up @@ -78,6 +78,8 @@ const main = async () => {
createDeviceCertCommand({
certsDir,
resourceGroup,
iotDpsClient: getIotDpsClient,
dpsName,
}),
reactConfigCommand({
websiteClient: getWebsiteClient,
Expand All @@ -95,26 +97,21 @@ const main = async () => {
]

let ran = false
commands.forEach(({ command, action, help, options }) => {
commands.forEach(({ command, action, help: h, options }) => {
const cmd = program.command(command)
cmd
.action(async (...args) => {
try {
ran = true
await action(...args)
} catch (error) {
console.error(
chalk.red.inverse(' ERROR '),
chalk.red(`${command} failed!`),
)
console.error(chalk.red.inverse(' ERROR '), chalk.red(error))
} catch (e) {
error(`${command} failed!`)
error(e)
process.exit(1)
}
})
.on('--help', () => {
console.log('')
console.log(chalk.yellow(help))
console.log('')
help(h)
})
if (options) {
options.forEach(({ flags, description, defaultValue }) =>
Expand All @@ -127,12 +124,12 @@ const main = async () => {
program.version(version)

if (!ran) {
program.outputHelp(chalk.yellow)
program.outputHelp()
throw new Error('No command selected!')
}
}

main().catch((err) => {
console.error(chalk.red(err))
error(err)
process.exit(1)
})
21 changes: 9 additions & 12 deletions cli/commands/create-ca-intermediate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as chalk from 'chalk'
import { CommandDefinition } from './CommandDefinition'
import { generateCAIntermediate } from '../iot/generateCAIntermediate'
import { ProvisioningServiceClient } from 'azure-iot-provisioning-service'
import { add as addToIntermediateRegistry } from '../iot/intermediateRegistry'
import { v4 } from 'uuid'
import { log, debug } from '../logging'
import { log, debug, setting, next, newline } from '../logging'

export const createCAIntermediateCommand = ({
certsDir: certsDirPromise,
Expand All @@ -25,7 +24,7 @@ export const createCAIntermediateCommand = ({
log,
debug,
})
console.log(chalk.magenta(`CA intermediate certificate generated.`))
debug(`CA intermediate certificate generated.`)

await addToIntermediateRegistry({ certsDir, id })

Expand Down Expand Up @@ -71,18 +70,16 @@ export const createCAIntermediateCommand = ({
lastUpdatedDateTimeUtc: undefined as any,
})

console.log(
chalk.magenta(
`Created enrollment group for CA intermediate certificiate`,
),
chalk.yellow(enrollmentGroupId),
setting(
`Created enrollment group for CA intermediate certificiate`,
enrollmentGroupId,
)

console.log()
newline()

console.log(
chalk.green('You can now generate device certificates using'),
chalk.blueBright('node cli create-device-cert'),
next(
'You can now generate device certificates using',
'node cli create-device-cert',
)
},
help:
Expand Down
25 changes: 10 additions & 15 deletions cli/commands/create-ca-root.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as chalk from 'chalk'
import { CommandDefinition } from './CommandDefinition'
import { IotDpsClient } from '@azure/arm-deviceprovisioningservices'
import { generateProofOfPosession } from '../iot/generateProofOfPosession'
import { v4 } from 'uuid'
import { generateCARoot } from '../iot/generateCARoot'
import { log, debug } from '../logging'
import { log, debug, success, setting, newline, next } from '../logging'
import { certificateName as cn } from '../iot/certificateName'

export const createCARootCommand = ({
Expand All @@ -30,7 +29,7 @@ export const createCARootCommand = ({
log,
debug,
})
console.log(chalk.magenta(`CA root certificate generated.`))
success(`CA root certificate generated.`)

// Register root CA certificate on DPS

Expand All @@ -45,10 +44,8 @@ export const createCARootCommand = ({
},
)

console.log(
chalk.magenta(`CA root registered with DPS`),
chalk.yellow(dpsName),
)
success(`CA root registered with DPS`)
setting('DPS name', dpsName)

// Create verification cert

Expand Down Expand Up @@ -77,16 +74,14 @@ export const createCARootCommand = ({
verificationCode: properties.verificationCode,
})

console.log(
chalk.magenta(`Generated verification certificate for verification code`),
chalk.yellow(properties.verificationCode),
)
success(`Generated verification certificate for verification code`)
setting('Verification Code', properties.verificationCode)

console.log()
newline()

console.log(
chalk.green('You can now verify the proof of posession using'),
chalk.blueBright('node cli proof-ca-root-possession'),
next(
'You can now verify the proof of posession using',
'node cli proof-ca-root-possession',
)
},
help:
Expand Down
48 changes: 27 additions & 21 deletions cli/commands/create-device-cert.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import * as chalk from 'chalk'
import { CommandDefinition } from './CommandDefinition'
import { randomWords } from '@nordicsemiconductor/random-words'
import { generateDeviceCertificate } from '../iot/generateDeviceCertificate'
import { log, debug } from '../logging'
import { log, debug, success, newline, next } from '../logging'
import { list as listIntermediateCerts } from '../iot/intermediateRegistry'
import { deviceFileLocations } from '../iot/deviceFileLocations'
import { setting, heading } from '../logging'
import { IotDpsClient } from '@azure/arm-deviceprovisioningservices'

export const createDeviceCertCommand = ({
certsDir: certsDirPromise,
resourceGroup,
iotDpsClient,
dpsName,
}: {
certsDir: () => Promise<string>
iotDpsClient: () => Promise<IotDpsClient>
resourceGroup: string
dpsName: string
}): CommandDefinition => ({
command: 'create-device-cert',
options: [
Expand Down Expand Up @@ -41,10 +46,7 @@ export const createDeviceCertCommand = ({
intermediateCertId = intermediateCerts[0]
}

console.log(
chalk.magenta('Intermediate certificate:'),
chalk.yellow(intermediateCertId),
)
setting('Intermediate certificate', intermediateCertId)

await generateDeviceCertificate({
deviceId: id,
Expand All @@ -54,26 +56,30 @@ export const createDeviceCertCommand = ({
intermediateCertId,
resourceGroup,
})
console.log(
chalk.magenta(`Certificate for device ${chalk.yellow(id)} generated.`),
)
success(`Certificate for device generated.`)
setting('Certificate ID', id)

const certJSON = deviceFileLocations({ certsDir, deviceId: id }).json
console.log()
console.log(
chalk.green('You can now connect to the broker using'),
chalk.greenBright(
'npm exec -- @nordicsemiconductor/asset-tracker-cloud-device-simulator-azure',
),
chalk.blueBright(certJSON),
newline()
next(
'You can now connect to the broker using',
`npm exec -- @nordicsemiconductor/asset-tracker-cloud-device-simulator-azure ${certJSON}`,
)

newline()
next(
'You can now flash the credentials to your device',
`node cli flash ${id}`,
)

console.log()
console.log(
chalk.green('You can now flash the credentials to your device'),
chalk.greenBright(`node cli flash`),
chalk.blueBright(id),
const { properties } = await (await iotDpsClient()).iotDpsResource.get(
dpsName,
resourceGroup,
)

heading('Firmware configuration')
setting('DPS hostname', properties.serviceOperationsHostName as string),
setting('ID scope', properties.idScope as string)
},
help: 'Generate a device certificate and register a device in the registry.',
})
18 changes: 6 additions & 12 deletions cli/commands/flash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
} from '@nordicsemiconductor/firmware-ci-device-helpers'
import { deviceFileLocations } from '../iot/deviceFileLocations'
import { Octokit } from '@octokit/rest'
import * as chalk from 'chalk'
import * as https from 'https'
import { v4 } from 'uuid'
import { progress, success } from '../logging'

const defaultPort = '/dev/ttyACM0'
const defaultSecTag = 42
Expand Down Expand Up @@ -62,7 +62,7 @@ const getLatestFirmware = async ({
if (hexfile === undefined) throw new Error(`Failed to detect latest release.`)

const downloadTarget = path.join(os.tmpdir(), `${v4()}.hex`)
console.log(chalk.magenta(`Downloading`), chalk.blue(hexfile.name))
progress(`Downloading`, hexfile.name)

await new Promise((resolve) => {
const file = fs.createWriteStream(downloadTarget)
Expand Down Expand Up @@ -115,10 +115,7 @@ export const flashCommand = ({
const hexfile =
firmware ?? (await getLatestFirmware({ dk, nbiot, nodebug }))

console.log(
chalk.magenta(`Connecting to device`),
chalk.blue(port ?? defaultPort),
)
progress(`Connecting to device`, port ?? defaultPort)

const connection = await connect({
atHostHexfile:
Expand All @@ -129,10 +126,7 @@ export const flashCommand = ({
progress: console.log,
})

console.log(
chalk.magenta(`Flashing credentials`),
chalk.blue(port ?? defaultPort),
)
progress(`Flashing credentials`, port ?? defaultPort)

const certs = deviceFileLocations({
certsDir: await certsDir(),
Expand All @@ -150,15 +144,15 @@ export const flashCommand = ({
privateKey: fs.readFileSync(certs.privateKey, 'utf-8'),
})

console.log(chalk.magenta(`Flashing firmware`), chalk.blue(hexfile))
progress(`Flashing firmware`, hexfile)

await flash({
hexfile,
})

await connection.connection.end()

console.log(chalk.green(`Done`))
success('Done')
},
help: 'Flash credentials and latest firmware release to a device using JLink',
})
14 changes: 7 additions & 7 deletions cli/commands/proof-ca-possession.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as chalk from 'chalk'
import { CommandDefinition } from './CommandDefinition'
import { IotDpsClient } from '@azure/arm-deviceprovisioningservices'
import { promises as fs } from 'fs'
import { CARootFileLocations } from '../iot/caFileLocations'
import { newline, next, setting, success } from '../logging'

export const proofCARootPossessionCommand = ({
certsDir,
Expand Down Expand Up @@ -36,7 +36,7 @@ export const proofCARootPossessionCommand = ({
'utf-8',
)

console.log(chalk.magenta('Certificate:'), chalk.yellow(certificateName))
setting('Certificate', certificateName)

await armDpsClient.dpsCertificate.verifyCertificate(
certificateName,
Expand All @@ -48,11 +48,11 @@ export const proofCARootPossessionCommand = ({
dpsName,
)

console.log(chalk.magenta('Verified root CA certificate.'))
console.log()
console.log(
chalk.green('You can now create a CA intermediate certificate using'),
chalk.blueBright('node cli create-ca-intermediate'),
success('Verified root CA certificate.')
newline()
next(
'You can now create a CA intermediate certificate using',
'node cli create-ca-intermediate',
)
},
help:
Expand Down
9 changes: 3 additions & 6 deletions cli/creds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as chalk from 'chalk'
import { AzureCliCredentials } from '@azure/ms-rest-nodeauth'
import { resourceGroupName } from '../arm/resources'
import { setting } from './logging'

export const creds = async (): Promise<AzureCliCredentials> => {
const creds = await AzureCliCredentials.create()
Expand All @@ -9,11 +9,8 @@ export const creds = async (): Promise<AzureCliCredentials> => {
tokenInfo: { subscription },
} = creds

console.error(chalk.magenta('Subscription:'), chalk.yellow(subscription))
console.error(
chalk.magenta('Resource Group:'),
chalk.yellow(resourceGroupName()),
)
setting('Subscription', subscription)
setting('Resource Group', resourceGroupName())

return creds
}
Loading

0 comments on commit b697050

Please sign in to comment.