kitops icon indicating copy to clipboard operation
kitops copied to clipboard

Add Node Selection Capability to kit info Command for Enhanced Kitfile Parsing

Open gorkem opened this issue 1 year ago • 2 comments

Describe the problem you're trying to solve When using the Kit CLI on CI/CD platforms, there is often a need to extract specific information from selected nodes within a Kitfile. Currently, users must rely on additional tools like yq to parse and filter this data, adding unnecessary complexity and dependencies to CI/CD pipelines.

Describe the solution you'd like Enhance the kit info command to include a node selection feature like yq selectors. This would allow users to directly specify paths to nodes within a Kitfile, simplifying the process of retrieving targeted information without needing external tools.

Describe alternatives you've considered While yq can be used as a workaround to pipe and filter the output of the Kit CLI, this approach requires an additional tool, which complicates CI/CD configurations.

gorkem avatar Sep 02 '24 16:09 gorkem

I would like to take this up and work on it.

srikary12 avatar Oct 08 '24 07:10 srikary12

Sounds good. Assigned.

gorkem avatar Oct 08 '24 13:10 gorkem

The expecttation is to add optional node after kit info command to display specific node. If nothing is mentioned full info needs to be displayed. Works?

srikary12 avatar Oct 13 '24 13:10 srikary12

We can introduce a filter flag --filter so kit info displays the full info kit info --filter .model.path prints only the model path.

gorkem avatar Oct 14 '24 12:10 gorkem

Quick question. Can I use yqlib package to acheive the same or has to be developed in house?

srikary12 avatar Oct 18 '24 09:10 srikary12

I would be open to a PR that used yqlib, though I suspect it may be a bit of using a sledgehammer to drive a nail situation. The library is capable of a lot of manipulation, and we're really only concerned with simple access at the moment.

amisevsk avatar Oct 18 '24 14:10 amisevsk

I'll be building something custom then. Any approach or suggestions?

srikary12 avatar Oct 18 '24 16:10 srikary12

@amisevsk Using reflect package I've tried adding the functionality. Need guidance with some of the pending items.

  • What to do if the filter if a struct, e.g., -f model. I'm able to fetch the struct should I display in YAML or show an error?
  • Suggest if I'm missing something or refractoring is needed.

srikary12 avatar Oct 20 '24 12:10 srikary12

@srikary12 I haven't looked into it too much, but I figure that since we have the unmarshalled struct (i.e. the Kitfile) we should just use the filter to walk that struct and marshal the resulting field.

Suppose the user passes -f ".model" (c.f. kit info <model> | yq '.model'). Then the Go code would look something like

// config := <resolved Kitfile from ResolveManifestAndConfig(...)>
filteredField := config.model

buf := new(bytes.Buffer)
enc := yaml.NewEncoder(buf)
enc.SetIndent(2)
if err := enc.Encode(filteredField); err != nil {
  // handle err
}
fmt.Println(string(buf.Bytes()))

(This assumes that using reflect to walk the struct is... simple enough. I've been working in TypeScript lately so I forget how marshalling reflected structs works :) )

Again, though, I haven't tried it out myself so I may be missing something. If this is looking ugly or difficult, feel free to submit a PR using yqlib and I will review. The reason for my gut take on yqlib is that it's designed to handle arbitrary yaml and parse it, whereas in our case we have a defined structure to work with -- I'm very ready to be wrong here :)

amisevsk avatar Oct 21 '24 15:10 amisevsk

I'll the required changes and mark it for review. I've expected the same inputs from you. I'll have to marshall them and display.

Thanks for the inputs.

srikary12 avatar Oct 21 '24 15:10 srikary12

Fixed it :) and raised a PR.

srikary12 avatar Oct 23 '24 18:10 srikary12