Skip to content

Commit

Permalink
Add a small hint on filter databases listed from tsh
Browse files Browse the repository at this point in the history
  • Loading branch information
greedy52 committed Jun 5, 2024
1 parent fb0645e commit f3cc49e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tool/tsh/common/db_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,28 @@ func formatDatabaseRolesForDB(database types.Database, accessChecker services.Ac
}
return ""
}

func shouldShowListDatabasesHint(cf *CLIConf, numRows int) bool {
selector := newDatabaseResourceSelectors(cf)

return numRows >= minNumRowsToShowListDatabasesHint &&
cf.command == "db ls" &&
cf.SearchKeywords == "" &&
selector.IsEmpty()
}

func maybeShowListDatabasesHint(cf *CLIConf, w io.Writer, numRows int) {
if !shouldShowListDatabasesHint(cf, numRows) {
return
}

fmt.Fprint(w, listDatabaseHint)
}

// minNumRowsToShowListDatabasesHint is an arbitrary number selected to show
// filtering hint for `tsh db ls` command when too many databases are listed.
const minNumRowsToShowListDatabasesHint = 20

const listDatabaseHint = "" +
"hint: use 'tsh db ls --search foo,bar' to search keywords\n" +
" use 'tsh db ls key1=value1,key2=value2' to filter databases by labels\n\n"
78 changes: 78 additions & 0 deletions tool/tsh/common/db_print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package common

import (
"bytes"
"strings"
"testing"

Expand Down Expand Up @@ -196,3 +197,80 @@ func Test_formatDatabaseRolesForDB(t *testing.T) {
})
}
}

func Test_maybeShowListDatabaseHint(t *testing.T) {
t.Parallel()

tests := []struct {
name string
cf *CLIConf
numRows int
wantHint bool
}{
{
name: "show hint when number is big",
cf: &CLIConf{
command: "db ls",
},
numRows: 25,
wantHint: true,
},
{
name: "no hint for tsh db connect",
cf: &CLIConf{
command: "db connect",
},
numRows: 25,
wantHint: false,
},
{
name: "no hint when number is small",
cf: &CLIConf{
command: "db ls",
},
numRows: 15,
wantHint: false,
},
{
name: "no hint when search flag exists",
cf: &CLIConf{
command: "db ls",
SearchKeywords: "foo",
},
numRows: 25,
wantHint: false,
},
{
name: "no hint when query flag exists",
cf: &CLIConf{
command: "db ls",
PredicateExpression: "labels[\"key\"] == \"value\"",
},
numRows: 25,
wantHint: false,
},
{
name: "no hint when labels exist",
cf: &CLIConf{
command: "db ls",
Labels: "key=value",
},
numRows: 25,
wantHint: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var buf bytes.Buffer

maybeShowListDatabasesHint(test.cf, &buf, test.numRows)

if test.wantHint {
require.Contains(t, buf.String(), "hint")
} else {
require.Empty(t, buf.String())
}
})
}
}
4 changes: 4 additions & 0 deletions tool/tsh/common/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2987,6 +2987,8 @@ func showDatabasesAsText(cf *CLIConf, w io.Writer, databases []types.Database, a
rows: rows,
verbose: verbose,
})

maybeShowListDatabasesHint(cf, w, len(rows))
}

func printDatabasesWithClusters(cf *CLIConf, dbListings []databaseListing, active []tlsca.RouteToDatabase) {
Expand All @@ -3007,6 +3009,8 @@ func printDatabasesWithClusters(cf *CLIConf, dbListings []databaseListing, activ
showProxyAndCluster: true,
verbose: cf.Verbose,
})

maybeShowListDatabasesHint(cf, cf.Stdout(), len(rows))
}

func formatActiveDB(active tlsca.RouteToDatabase, displayName string) string {
Expand Down

0 comments on commit f3cc49e

Please sign in to comment.