Skip to content

common/commands: add a command builder package#1241

Merged
phlogistonjohn merged 10 commits intoceph:masterfrom
phlogistonjohn:jjm-command-builder
Apr 14, 2026
Merged

common/commands: add a command builder package#1241
phlogistonjohn merged 10 commits intoceph:masterfrom
phlogistonjohn:jjm-command-builder

Conversation

@phlogistonjohn
Copy link
Copy Markdown
Collaborator

@phlogistonjohn phlogistonjohn commented Mar 21, 2026

A generic command builder API that lets one construct commands dynamically in a manner similar to what the ceph command line tool does.

This is actually something I had planned on doing since at least 5 years now. While we did have some documenation on how to accomplish this in the hints.md file, I don't think many people were reading it and it was a bit "thin". So after the umpteenth time I said or wrote something like, "yes you can replicate nearly all ceph commands using go-ceph" or "if you make your own JSON and call the right api you can do that". I finally sat down and implemented a good chunk of it... including a fairly extensive doc.go explaining the background of the package.

So now we have both code that one can use directly and it can serve as an example for folks who just might want to get started doing their own "admin" package for some topic.

Checklist

  • Added tests for features and functional changes
  • Public functions and types are documented
  • Standard formatting is applied to Go code
  • Is this a new API? Added a new file that begins with //go:build ceph_preview
  • Ran make api-update to record new APIs

New or infrequent contributors may want to review the go-ceph Developer's Guide including the section on how we track API Status and the API Stability Plan.

The go-ceph project uses mergify. View the mergify command guide for information on how to interact with mergify. Add a comment with @Mergifyio rebase to rebase your PR when github indicates that the PR is out of date with the base branch.

@phlogistonjohn phlogistonjohn force-pushed the jjm-command-builder branch 4 times, most recently from 6b97cfb to d199a5d Compare March 22, 2026 16:26
@phlogistonjohn phlogistonjohn marked this pull request as ready for review March 24, 2026 16:41
@phlogistonjohn
Copy link
Copy Markdown
Collaborator Author

🥣

@phlogistonjohn phlogistonjohn added the API This PR includes a change to the public API of a go-ceph package label Mar 27, 2026
Copy link
Copy Markdown
Collaborator

@anoopcs9 anoopcs9 left a comment

Choose a reason for hiding this comment

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

I only have few minor comments.

Should we go for a Show() method on Description to display details for a command once a match is found using Match() or Find()?

Comment thread common/commands/builder/descriptions.go Outdated
Comment thread common/commands/builder/ceph_types_test.go Outdated
Comment thread common/commands/builder/ceph_types_test.go Outdated
Comment thread common/commands/builder/ceph_types_test.go Outdated
Comment thread common/commands/builder/ceph_types_test.go Outdated
Comment thread common/commands/builder/ceph_types.go Outdated
Comment thread common/commands/builder/ceph_types.go Outdated
Comment thread common/commands/builder/doc.go Outdated
Comment thread common/commands/builder/query.go Outdated
@phlogistonjohn
Copy link
Copy Markdown
Collaborator Author

Should we go for a Show() method on Description to display details for a command once a match is found using Match() or Find()?

What would it return?

Copy link
Copy Markdown
Collaborator

@anoopcs9 anoopcs9 left a comment

Choose a reason for hiding this comment

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

Should we go for a Show() method on Description to display details for a command once a match is found using Match() or Find()?

What would it return?

Either a slice of strings or a map[string]string. The basic idea is to provide a way to query the expected format and values for the command in question while trying to use Apply() on an already created Builder type.

Comment thread common/commands/builder/ceph_types.go
@phlogistonjohn
Copy link
Copy Markdown
Collaborator Author

Either a slice of strings or a map[string]string. The basic idea is to provide a way to query the expected format and values for the command in question while trying to use Apply() on an already created Builder type.

So like Variables but returns a slice of the SignatureVar.name instead of the whole SignatureVar?

@anoopcs9
Copy link
Copy Markdown
Collaborator

Either a slice of strings or a map[string]string. The basic idea is to provide a way to query the expected format and values for the command in question while trying to use Apply() on an already created Builder type.

So like Variables but returns a slice of the SignatureVar.name instead of the whole SignatureVar?

May include the prefix as well, because the string or slice of strings used to find the match might not contain the entire prefix. Nevertheless, if it is just SignateVar.Name we don’t convey the full meaning as these variables are not always required. Moreover, if the type happens to be CephChoices we must list the choices again. Looks like we need the entire SignatureElement 😉.

@phlogistonjohn
Copy link
Copy Markdown
Collaborator Author

phlogistonjohn commented Apr 10, 2026

I'm still a bit fuzzy on the request. I've attempted to add a convenient way to get an at-a-glance summary of a description by giving it a String method that produces a summary string containing the prefix and string representations of the variables. The SignatureVar type also gained a String method to help make this happen.
(I left it as separate commits for now to help things stand out)

I hope this is sufficient. As for API use I would generally expect the PrefixString/Prefix method and Variables methods to cover the remaining cases of having to walk over the contents of a command Description.

If you still think something is missing please share a (pseudocode) use case demonstrating what you think is missing becasue I'm struggling a bit with some of the prose comments.

@anoopcs9
Copy link
Copy Markdown
Collaborator

As discussed offline, we've decided to pursue the request later so that the core infrastructure can be merged first.

Copy link
Copy Markdown
Collaborator

@anoopcs9 anoopcs9 left a comment

Choose a reason for hiding this comment

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

lgtm.

@phlogistonjohn
Copy link
Copy Markdown
Collaborator Author

@ansiwen PTAL. If you can not approve by 04/13 my plan is to merge anyway but if changes are requested I'm happy to extend the preview period for this by an additional release. That's my plan since many of my prs tend to run long lately.

@phlogistonjohn
Copy link
Copy Markdown
Collaborator Author

@Mergifyio rebase

Add types for working with Ceph's command description JSON.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
These types encapsulate a bit of basic knowledge about what
ceph command JSON expects. Note that it doesn't really do
any restrictions on specialized types like OsdName or whatnot.
That can be expanded on later.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
Add a Builder that can map the command description into the
corresponding Ceph argument types and generate JSON commands based on
them.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Signed-off-by: John Mulligan <jmulligan@redhat.com>
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 13, 2026

Deprecation notice: This pull request comes from a fork and was rebased using bot_account impersonation. This capability will be removed on July 1, 2026. After this date, the rebase action will no longer be able to rebase fork pull requests with this configuration. Please switch to the update action/command to ensure compatibility going forward.

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 13, 2026

rebase

✅ Branch has been successfully rebased

@phlogistonjohn phlogistonjohn merged commit 20b97b9 into ceph:master Apr 14, 2026
14 checks passed
@phlogistonjohn phlogistonjohn deleted the jjm-command-builder branch April 14, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API This PR includes a change to the public API of a go-ceph package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants