-
Notifications
You must be signed in to change notification settings - Fork 2k
tsh request search, add support for --format flag #62015
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
Merged
tangyatsu
merged 2 commits into
master
from
tangyatsu/tsh-request-search-add-support-for-format-flag
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package asciitable | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| "regexp" | ||
| "slices" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| ) | ||
|
|
||
| const asciitableTag = "asciitable" | ||
|
|
||
| // Regular expression to convert from "DatabaseRoles" to "Database Roles" etc. | ||
| var headerSplitRe = regexp.MustCompile(`([a-z])([A-Z])`) | ||
|
|
||
| // MakeColumnsAndRows converts a slice of structs into column headers and | ||
| // row data suitable for use with asciitable.MakeTable. | ||
| // T must be a struct type. If T is not a struct, the function returns an error. | ||
| // | ||
| // Column headers are determined by the `asciitable` struct tag. If the tag is | ||
| // empty, the header is derived from the field name (e.g., "DatabaseRoles" | ||
| // becomes "Database Roles"). | ||
| // | ||
| // includeColumns optionally restricts which columns are returned. Each value | ||
| // must match the final header name (tag value is used if present, otherwise the | ||
| // derived name). If includeColumns is empty or nil, all fields are included. | ||
| func MakeColumnsAndRows[T any](rows []T, includeColumns []string) ([]string, [][]string, error) { | ||
|
tangyatsu marked this conversation as resolved.
|
||
| t := reflect.TypeOf((*T)(nil)).Elem() | ||
| if t.Kind() != reflect.Struct { | ||
| return nil, nil, trace.Errorf("only slices of struct are supported: got slice of %s", t.Kind()) | ||
| } | ||
|
|
||
| type fieldInfo struct { | ||
| index int | ||
| name string | ||
| } | ||
|
|
||
| var fields []fieldInfo | ||
| var columns []string | ||
|
|
||
| for i := 0; i < t.NumField(); i++ { | ||
| f := t.Field(i) | ||
|
|
||
| header := f.Tag.Get(asciitableTag) | ||
| if header == "-" { | ||
| continue | ||
| } | ||
| if header == "" { | ||
| header = headerSplitRe.ReplaceAllString(f.Name, "${1} ${2}") | ||
| } | ||
|
|
||
| if len(includeColumns) > 0 && !slices.Contains(includeColumns, header) { | ||
| continue | ||
| } | ||
|
|
||
| fields = append(fields, fieldInfo{ | ||
| index: i, | ||
| name: header, | ||
| }) | ||
| columns = append(columns, header) | ||
| } | ||
|
|
||
| outRows := make([][]string, 0, len(rows)) | ||
| for _, row := range rows { | ||
| v := reflect.ValueOf(row) | ||
| rowValues := make([]string, 0, len(fields)) | ||
| for _, fi := range fields { | ||
| rowValues = append(rowValues, fmt.Sprintf("%v", v.Field(fi.index))) | ||
| } | ||
| outRows = append(outRows, rowValues) | ||
| } | ||
|
|
||
| return columns, outRows, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package asciitable | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMakeColumnsAndRows(t *testing.T) { | ||
| type row struct { | ||
| Name string | ||
| ResourceID string | ||
| } | ||
|
|
||
| rows := []row{ | ||
| {Name: "n1", ResourceID: "id1"}, | ||
| {Name: "n2", ResourceID: "id2"}, | ||
| } | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, []string{"Name", "Resource ID"}, cols) | ||
| require.Equal(t, [][]string{ | ||
| {"n1", "id1"}, | ||
| {"n2", "id2"}, | ||
| }, data) | ||
| } | ||
|
|
||
| func TestMakeColumnsAndRowsWithTagsAndSkip(t *testing.T) { | ||
| type row struct { | ||
| Name string `asciitable:"Custom Name"` | ||
| Skip string `asciitable:"-"` | ||
| ResourceID string `asciitable:"Resource ID"` | ||
| } | ||
|
|
||
| rows := []row{ | ||
| {Name: "n1", Skip: "skip1", ResourceID: "id1"}, | ||
| {Name: "n2", Skip: "skip2", ResourceID: "id2"}, | ||
| } | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, []string{"Custom Name", "Resource ID"}, cols) | ||
| require.Equal(t, [][]string{ | ||
| {"n1", "id1"}, | ||
| {"n2", "id2"}, | ||
| }, data) | ||
| } | ||
|
|
||
| func TestMakeColumnsAndRowsIncludeColumns(t *testing.T) { | ||
| type row struct { | ||
| Name string | ||
| Hostname string | ||
| Labels string | ||
| ResourceID string | ||
| } | ||
|
|
||
| rows := []row{ | ||
| {Name: "n1", Hostname: "h1", Labels: "a=1", ResourceID: "id1"}, | ||
| {Name: "n2", Hostname: "h2", Labels: "b=2", ResourceID: "id2"}, | ||
| } | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, []string{"Name", "Labels"}) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, []string{"Name", "Labels"}, cols) | ||
| require.Equal(t, [][]string{ | ||
| {"n1", "a=1"}, | ||
| {"n2", "b=2"}, | ||
| }, data) | ||
| } | ||
|
|
||
| func TestMakeColumnsAndRowsIncludeColumnsWithTags(t *testing.T) { | ||
| type row struct { | ||
| Name string `asciitable:"Custom Name"` | ||
| ResourceID string `asciitable:"Resource ID"` | ||
| } | ||
|
|
||
| rows := []row{ | ||
| {Name: "n1", ResourceID: "id1"}, | ||
| {Name: "n2", ResourceID: "id2"}, | ||
| } | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, []string{"Custom Name"}) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, []string{"Custom Name"}, cols) | ||
| require.Equal(t, [][]string{ | ||
| {"n1"}, | ||
| {"n2"}, | ||
| }, data) | ||
| } | ||
|
|
||
| func TestMakeColumnsAndRowsCamelCaseLongName(t *testing.T) { | ||
| type row struct { | ||
| VeryLongFieldName string | ||
| } | ||
|
|
||
| rows := []row{ | ||
| {VeryLongFieldName: "value1"}, | ||
| } | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, cols, 1) | ||
| require.Equal(t, "Very Long Field Name", cols[0]) | ||
| require.Equal(t, [][]string{{"value1"}}, data) | ||
| } | ||
|
|
||
| func TestMakeColumnsAndRowsEmptySlice(t *testing.T) { | ||
| type row struct { | ||
| Name string | ||
| ResourceID string | ||
| } | ||
|
|
||
| var rows []row | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, []string{"Name", "Resource ID"}, cols) | ||
| require.Empty(t, data) | ||
| } | ||
|
|
||
| func TestMakeColumnsAndRowsNonStructType(t *testing.T) { | ||
| rows := []int{1, 2, 3} | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, nil) | ||
| require.Error(t, err) | ||
| require.Nil(t, cols) | ||
| require.Nil(t, data) | ||
|
|
||
| var traceErr trace.Error | ||
| ok := errors.As(err, &traceErr) | ||
| require.True(t, ok) | ||
|
|
||
| require.Contains(t, err.Error(), "only slices of struct are supported") | ||
| } | ||
|
|
||
| func TestMakeColumnsAndRowsIncludeColumnsUnknown(t *testing.T) { | ||
| type row struct { | ||
| Name string | ||
| } | ||
|
|
||
| rows := []row{ | ||
| {Name: "n1"}, | ||
| } | ||
|
|
||
| cols, data, err := MakeColumnsAndRows(rows, []string{"Unknown"}) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Empty(t, cols) | ||
| require.Equal(t, [][]string{{}}, data) | ||
| require.Len(t, data, 1) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.