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

Plain text/JSON output for commands #2242

Open
tanepiper opened this issue Aug 29, 2023 · 0 comments
Open

Plain text/JSON output for commands #2242

tanepiper opened this issue Aug 29, 2023 · 0 comments

Comments

@tanepiper
Copy link

tanepiper commented Aug 29, 2023

Expected Behavior

I'd like to automate using the contentful cli, for example get a list of spaces the cli user has access too that I can pass to a shell script or tool like jq

Actual Behavior

When I run the command I get a table output, currently most list command use the table from cli-table3, and don't support other outputs, limiting the CLI use.

Possible Solution

Provide a --plain or --json like argument to commands like contentful space list that provides a plain output, for example:

module.exports.builder = yargs => {
  return yargs
    .usage('Usage: contentful space list')
    .option('management-token', {
      alias: 'mt',
      describe: 'Contentful management API token',
      type: 'string'
    })
    .option('header', {
      alias: 'H',
      type: 'string',
      describe: 'Pass an additional HTTP Header'
    })
    .option('json', {
      alias: 'j',
      describe: 'Enables JSON output',
      type: 'boolean'
    })
    .epilog(
      [
        'See more at:',
        'https://github.com/contentful/contentful-cli/tree/master/docs/space/list',
        copyright
      ].join('\n')
    )
}

module.exports.aliases = ['ls']

async function spaceList({ context, header }) {
  const { managementToken, activeSpaceId, json } = context

  const client = await createManagementClient({
    accessToken: managementToken,
    feature: 'space-list',
    headers: getHeadersFromOption(header)
  })

  const result = await paginate({ client, method: 'getSpaces' })

  const spaces = result.items.sort((a, b) => a.name.localeCompare(b.name))

  if (json) {
    log(JSON.stringify({ spaces })) // This will output an object with a key spaces, and the value the array of spaces
    return
  }

  const table = new Table({
    head: ['Space name', 'Space id']
  })

Context

With GitHub Actions for example, with a JSON output of a command such as contentful space list, it can be passed to a matrix of an array of spaces, this can allow for automation of backup to an assigned user or application with permission over several spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant