Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output mode option to logs command #1131

Merged
merged 2 commits into from Feb 26, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -252,7 +252,7 @@ All feedback and suggestions are welcome!
- 📣 Stay up to date on new features and announcments on [@zeithq](https://twitter.com/zeithq).
- 🔐 Subscribe to our [security](http://zeit.us12.list-manage1.com/subscribe?u=3c9e9e13d7e6dae8faf375bed&id=110e586914) mailing list to stay up-to-date on urgent security disclosures.

Please note: we adhere to the [contributor coventant](http://contributor-covenant.org/) for
Please note: we adhere to the [contributor covenant](http://contributor-covenant.org/) for
all interactions in our community.

#### Contributions
Expand Down
25 changes: 22 additions & 3 deletions src/providers/sh/commands/logs.js
Expand Up @@ -47,6 +47,9 @@ const help = () => {
'UNTIL'
)} Only return logs before date (ISO 8601), ignored for ${'`-f`'}
-T, --team Set a custom team scope
-o ${chalk.bold.underline('MODE')}, --output=${chalk.bold.underline(
'MODE'
)} Specify the output format (${Object.keys(logPrinters).join('|')}) [short]

${chalk.dim('Examples:')}

Expand All @@ -67,21 +70,23 @@ let limit
let query
let follow
let types
let outputMode

let since
let until
let instanceId

const main = async ctx => {
argv = mri(ctx.argv.slice(2), {
string: ['query', 'since', 'until'],
string: ['query', 'since', 'until', 'output'],
boolean: ['help', 'all', 'debug', 'follow'],
alias: {
help: 'h',
all: 'a',
debug: 'd',
query: 'q',
follow: 'f'
follow: 'f',
output: 'o'
}
})

Expand Down Expand Up @@ -124,6 +129,7 @@ const main = async ctx => {
query = argv.query || ''
follow = argv.f
types = argv.all ? [] : ['command', 'stdout', 'stderr', 'exit']
outputMode = argv.output in logPrinters ? argv.output : 'short'

const {authConfig: { credentials }, config: { sh }} = ctx
const {token} = credentials.find(item => item.provider === 'sh')
Expand Down Expand Up @@ -238,7 +244,7 @@ function printLogs({ token, sh: { currentTeam } }) {
})
}

function printLog(log) {
function printLogShort(log) {
let data
const obj = log.object
if (log.type === 'request') {
Expand Down Expand Up @@ -267,6 +273,19 @@ function printLog(log) {
})
}

function printLogRaw(log) {
console.log(log.object ? JSON.stringify(log.object) : log.text)
}

const logPrinters = {
short: printLogShort,
raw: printLogRaw
}

function printLog(log) {
logPrinters[outputMode](log)
}

async function fetchLogs({ token, currentTeam, since, until } = {}) {
const now = new Now({ apiUrl, token, debug, currentTeam })

Expand Down