passbolt / go-passbolt-cli

A CLI tool to interact with Passbolt, a Open source Password Manager for Teams
https://passbolt.com
MIT License
61 stars 14 forks source link

JSON format #4

Closed AnatomicJC closed 1 year ago

AnatomicJC commented 2 years ago

Hi !

thanks for this awesome tool ! Do you plan to propose json format as output ? It will be easier to parse.

Thanks 😽

speatzle commented 2 years ago

Hi, i have investigated JSON output in the past and there are a these considerations: 1) Errors won't be in JSON due to cobra. 2) For what sub-commands would JSON output be useful? I can only see JSON output being useful for list, get and maybe create what about the rest of the sub-commands? To 'properly' support JSON output all sub-commands 'should' be able to output JSON, but that would be allot of work that will probably be unused.

I'm not sure if i want to implement the JSON output with these 'compromises'. What do you think?

AnatomicJC commented 2 years ago

Hi and sorry for this late reply.

Usually, I use jq to parse API calls outputs to filter results. That's why I wondered if it should easy to implement, but it seems it is not :/

As an example, I created a docker image for go-passbolt-cli, and added some wrappers to easily fetch what I want.

As an example, to get a password per resource name and username, I use this wrapper:

#!/bin/sh

if [ "$#" -ne 2 ]; then
    echo "Illegal number of parameters"
    echo "Usage: get-password name username"
    exit 1
fi

NAME="${1}"
USERNAME="${2}"

ID="$(passbolt list resource --column ID --column Name --column Username \
  | grep "${NAME}" | grep "${USERNAME}" | head -n1 | awk '{print $1}')"

passbolt get resource --id ${ID} | grep Password | sed 's/^Password: //g'

I totally understand it can be a non-trivial task and you can close this issue. I already played a bit with golang, and I quickly switched back to python to manipulate json. It was easier for me 😆

speatzle commented 2 years ago

Hi,

i think JSON output is useful in allot of cases as using jq is much easier than combining grep, awk, sed, head and friends to parse Output.

For the Issues With adding JSON output: 1) The Errors can't be in JSON due to cobra but errors always set the Exitcode to 1 so if this is properly documented i think that is ok since Scripts can very easily check the Exitcode.

2) After thinking about it, i believe it would be fine to only support JSON output on list, get and create sub-commands since the other commands are usually only used manually and not by Scripts.

As such i think the few hours of effort implementing this would be well spent since it makes such a big difference with ease of parsing and the Issues can be properly worked around.

nourcy commented 2 years ago

Hi, I would be also interested for list and get! Are you planning to work on it in the coming weeks?

Cheers

speatzle commented 2 years ago

Yes, i am planning to get around to it SoonTM.

AlexandreMarcq commented 2 years ago

@speatzle I can try to help you on that, is there any setup I should do before trying anything ?

speatzle commented 2 years ago

@AlexandreMarcq you just need to clone the repository and have a Passbolt Instance. When implementing this you basically just add a flag to cmd/list.go, cmd/get.go and cmd/create.go called --json which then marshals the existing go-passbolt structs to JSON (those structs already have the correct JSON tags)

schustersv commented 1 year ago

Any news on the PR? Being able to produce easily parsable json output would be really welcome.

speatzle commented 1 year ago

Since there hasn't been any activity on the PR in a long time by @AlexandreMarcq ill see if I can fix it up and merge it soon

speatzle commented 1 year ago

This got Implemented by #20. will make a New Release soon.

speatzle commented 1 year ago

Released in v0.2.0