-
Notifications
You must be signed in to change notification settings - Fork 23
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
✨ make components.Select()
generic
#4811
Conversation
This change is adding a new `Selector` interface that the function `components.Select()` accepts so that we can select any type of slice. Additionally, I improved the `components.List()` function to use the `tabwriter` when running things without a TTY. Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
3fb89cb
to
bc63e5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not have this added to a readme or some other development doc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea! I can add some docs.
This example is useful to test this interactive code, you can run it with
$ go run cli/components/_examples/selector/main.go
I'll add it to the docs. 👍🏽
cli/components/list_raw_object.go
Outdated
) | ||
|
||
// Object is the interface that a list need to implement so we can display its objects. | ||
type Object interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have a more specific name for this interface? I find this one too generic making it unclear what it's purpose is. Perhaps call it CliListableItem
or something similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change. 👍🏽
cli/components/list_raw_object.go
Outdated
b := &strings.Builder{} | ||
w := tabwriter.NewWriter(b, 1, 1, 1, ' ', tabwriter.TabIndent) | ||
|
||
log.Debug().Msgf("discovered %d %s(s)", len(list), listType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this message makes no sense here. The discovery mechanism is related to asset discovery when starting a scan. You are building a generic function that can be used for any type of object (not necessarily an asset).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch!
cli/components/selector.go
Outdated
|
||
// Selector is the interface that items need to implement so that we can select them. | ||
type Selector interface { | ||
HumanName() string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: weird name. Perhaps call it FriendlyName
or similar.
Would be nice to add documentation what this function is used for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to re-implement the function for the assets, since it already exists and, it is the only place we use it (for now).
If we are ok with a new function, then I'll go with Display()
cli/components/selector.go
Outdated
) | ||
|
||
// Selector is the interface that items need to implement so that we can select them. | ||
type Selector interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one also seems to generic. CliSelectableItem
maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing.
cli/components/list_raw_object.go
Outdated
for i := range list { | ||
assetObj := list[i] | ||
|
||
for i, key := range assetObj.PrintableKeys() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I am in favour of this construction. We are building a generic way of displaying lists of items and selecting an item from that list. I think the way an item from the list is displayed, should be defined outside of this function. The current implementation seems like it provides some "customization" options but it is actually very limited.
If you don't need the keys and values individually, why not just make the interface for Object
have a function Print() string
or something similar, and just call this here. Then it is up to the Object
to specify how it is printed on the CLI. We also remove the PrintableKeys
and PrintableValue
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree.
Leaving the formatting to the implementation (caller) is not an option, if we want that, we should not have this function at all.
The whole point of these helpers is to help and standardize how we print/interact with "stuff".
This comment has been minimized.
This comment has been minimized.
85cfd01
to
87732d9
Compare
Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
87732d9
to
b311ed1
Compare
This change is adding a new
SelectableItem
interface that the functioncomponents.Select()
accepts so that we can select any type of slice.
Additionally, I improved the
components.List()
function to use thetabwriter
whenrunning things without a TTY.
Finally, I added the following
README.md
components
packageThis Go package has interactive helpers used by
cnquery
andcnspec
.We use a powerful little TUI framework called bubbletea.
Select
componentSelect is an interactive prompt that displays the provided message and displays a
list of items to be selected.
e.g.
To execute this example:
List
componentList is a non-interactive function that lists items to the user.
e.g.
To execute this example: