Skip to content
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

Feature/cobra cli #79

Merged
merged 5 commits into from
Jun 24, 2024
Merged

Feature/cobra cli #79

merged 5 commits into from
Jun 24, 2024

Conversation

consolethinks
Copy link
Collaborator

@consolethinks consolethinks commented Jun 5, 2024

Issue

Resolves #75

PR Description

It copies the pre-existing code in each executable's main function, adapts them to Cobra's way of getting flags and parameters.

Each command supported by this one executable should behave the same way, for a given set of flags and parameters, as the corresponding tool that it was based on.

Notes

This PR should be tested against PSI usecases still.

@sbliven
Copy link
Member

sbliven commented Jun 11, 2024

This introduces as cmd/cmd folder. This is a bit confusing. Should we rename the module?

@consolethinks
Copy link
Collaborator Author

This last commit should address the double cmd folder structure by renaming the inner cmd folder to "commands" (as it contains command implementations of the CLI tool). If need be, the outer cmd folder could be renamed to cli as well, but I think the former is the "standard" in Go.

Copy link
Collaborator

@minottic minottic left a comment

Choose a reason for hiding this comment

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

I feel tests are missing and, once ready, I will be worried to merge all the main changes as a single commit. Also the CI breaks.

This is a very useful PR, thanks!

Long: `This library comprises a few subcommands for managing SciCat
and datasets on it, as well as interacting with the archival system connected
to it.`,
// uncomment the next line if there's a default action
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe remove the comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I might add a default action since the version parameter is common between all commands


flag.Parse()

if datasetUtils.TestFlags != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

was this block removed by mistake?

Copy link
Collaborator

Choose a reason for hiding this comment

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

having this will allow to check that passing variables by reference did not introduce any error

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it was deleted because the code was original only copied from before I added tests, this is readded in the latest commit.


import cmd "github.com/paulscherrerinstitute/scicat/cmd/commands"

func main() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we test here that when we call this main with an arg (e.g. datasetIngestor) the datasetIngestor from commands is executed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have done something similar to that in the latest commit (calling Execute() instead of main, but it does effectively the same thing)

// from the array of strings
commonPrefix := func(arr []string) string {
// return shortest string, length given in bytes
findMinLength := func(arr []string) int {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do these need to become internal functions? is this because the command is now a pointer? I am not a fun of internal functions, as they will be very hard to test later

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

they are internal because the functions are only used once and only exist to make the code more readable. If they weren't internal, the functions' definitions would be shared among all the commands. This wasn't an issue when we had lots of little "main" packages, but now there's a bigger "cmd" package that ties everything together.

Timeout: 10 * time.Second}

// internal functions
assembleRsyncCommands := func(username string, datasetDetails []datasetUtils.Dataset, destinationPath string) ([]string, []string) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

see comment above

@sbliven
Copy link
Member

sbliven commented Jun 21, 2024

Not to hold up this merge even more, but are we ok with removing the old tools? I had imagined keeping the old executable around for backwards compatibility. After removing them we will have to maintain two branches until we drop support for the old cli names.

@consolethinks consolethinks changed the base branch from main to v3 June 24, 2024 09:09
@minottic minottic self-requested a review June 24, 2024 10:02
@minottic
Copy link
Collaborator

I think we can double check the CI and the version comparison and use this eventually.
image
This will allow us to get rid of the v3 branch and merge everything into main, even before officially releasing.

From memory, the release workflow in the CI overrides the "prerelease" and "latest" flags, so we should check that

@consolethinks consolethinks merged commit 1e77318 into v3 Jun 24, 2024
3 checks passed
@consolethinks consolethinks deleted the feature/cobra-cli branch June 24, 2024 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

One executable for all CLI commands by using Cobra
3 participants