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 JSON and YAML to several tsh commands #11681

Merged
merged 40 commits into from
Apr 18, 2022
Merged

Conversation

atburke
Copy link
Contributor

@atburke atburke commented Apr 1, 2022

This PR adds support for JSON and YAML output to several tsh commands via the --format flag.

Commands with JSON/YAML

Command JSON/YAML Content
version Same info as text
app ls Directly serialize apps
app config Same info as text, plus curl command
db ls Directly serialize databases
db env key/value map of env vars
db config Same info as text
play Directly serialize events (preexisting behavior)
ls Directly serialize server struct (preexisting behavior)
clusters Same info as text
status Same info as text
env key/value map of env vars
request ls Directly serialize request struct (preexisting behavior)
request show Directly serialize request struct
kube ls Same info as text
kube sessions Directly serialize sessions struct
mfa ls Directly serialize device struct

Commands without JSON/YAML

  • help
  • ssh
  • aws
  • app login
  • app logout
  • proxy ssh
  • proxy db
  • proxy app
  • db login
  • db logout
  • db connect
  • join
  • scp
  • login
  • logout
  • request create
  • request review
  • kube login
  • kube exec
  • kube join
  • mfa add
  • mfa rm

Resolves #6742.

@github-actions github-actions bot added the tsh tsh - Teleport's command line tool for logging into nodes running Teleport. label Apr 1, 2022
@github-actions github-actions bot requested review from nklaassen and Tener April 1, 2022 23:36
Copy link
Contributor

@Tener Tener left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good and the issues are minor. Approving, as nothing critical stands out.

lib/client/api.go Outdated Show resolved Hide resolved
lib/client/api.go Outdated Show resolved Hide resolved
tool/tsh/app.go Outdated Show resolved Hide resolved
@@ -252,13 +271,42 @@ func onDatabaseConfig(cf *CLIConf) error {
default:
return trace.BadParameter("unknown database protocol: %q", database)
}
switch cf.Format {

format := strings.ToLower(cf.Format)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for ignoring the case of the format. Do we have consistency with our other CLI tools in this regard? We may want to apply the same treatment e.g. for tctl, although preferably with a separate PR.

tool/tsh/kube.go Outdated Show resolved Hide resolved
tool/tsh/mfa.go Outdated Show resolved Hide resolved
lib/client/api.go Outdated Show resolved Hide resolved
lib/tlsca/ca.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@r0mant r0mant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atburke Implementation looks good now apart from a few nitpicks, but this PR also needs test coverage. There are a couple of ways to test this I think, you can either setup auth/teleport process in tests, populate them with respective objects (apps, dbs, etc. - we should already be doing this somewhere here) and call respective onListXXX methods, or factor out json/yaml rendering logic into separate functions (like you already have showApps, showDatabases) and test them to make sure they return correct output based on the provided inputs.

tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated
Comment on lines 1577 to 1579
if apps == nil {
apps = []types.Application{}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will Marshal panic is apps are nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it won't panic, but it will serialize nil as null instead of [].

tool/tsh/tsh.go Outdated
Comment on lines 1645 to 1647
if databases == nil {
databases = []types.Database{}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will Marshal panic is databases are nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@atburke
Copy link
Contributor Author

atburke commented Apr 18, 2022

@r0mant Can you take another look at this?

tool/tsh/access_request.go Outdated Show resolved Hide resolved
tool/tsh/access_request.go Outdated Show resolved Hide resolved
tool/tsh/db.go Outdated Show resolved Hide resolved
@atburke atburke enabled auto-merge (squash) April 18, 2022 19:13
@atburke atburke merged commit 7c81fc9 into master Apr 18, 2022
@atburke atburke deleted the atburke/tsh-json-output branch April 18, 2022 21:40
atburke added a commit that referenced this pull request Apr 18, 2022
This PR adds support for JSON and YAML output to several tsh commands via the --format flag.
atburke added a commit that referenced this pull request Apr 19, 2022
This change adds support for JSON and YAML output to several tsh commands via the --format flag.
@webvictim webvictim mentioned this pull request Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tsh tsh - Teleport's command line tool for logging into nodes running Teleport.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add additional JSON output flags to other TSH commands
3 participants