Adds Ability to Chain Cobra RunE Commands#1771
Merged
Conversation
schnie
commented
Dec 20, 2024
Comment on lines
-76
to
-106
| PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
| // Check for latest version | ||
| if config.CFG.UpgradeMessage.GetBool() { | ||
| // create github client with 3 second timeout, setting an aggressive timeout since its not mandatory to get a response in each command execution | ||
| githubClient := github.NewClient(&http.Client{Timeout: 3 * time.Second}) | ||
| // compare current version to latest | ||
| err = version.CompareVersions(githubClient, "astronomer", "astro-cli") | ||
| if err != nil { | ||
| softwareCmd.InitDebugLogs = append(softwareCmd.InitDebugLogs, "Error comparing CLI versions: "+err.Error()) | ||
| } | ||
| } | ||
| if isCloudCtx { | ||
| err = cloudCmd.Setup(cmd, platformCoreClient, astroCoreClient) | ||
| if err != nil { | ||
| if strings.Contains(err.Error(), "token is invalid or malformed") { | ||
| return errors.New("API Token is invalid or malformed") //nolint | ||
| } | ||
| if strings.Contains(err.Error(), "the API token given has expired") { | ||
| return errors.New("API Token is expired") //nolint | ||
| } | ||
| softwareCmd.InitDebugLogs = append(softwareCmd.InitDebugLogs, "Error during cmd setup: "+err.Error()) | ||
| } | ||
| } | ||
| // common PersistentPreRunE component between software & cloud | ||
| // setting up log verbosity and dumping debug logs collected during CLI-initialization | ||
| if err := softwareCmd.SetUpLogs(os.Stdout, verboseLevel); err != nil { | ||
| return err | ||
| } | ||
| softwareCmd.PrintDebugLogs() | ||
| return nil | ||
| }, |
Member
Author
There was a problem hiding this comment.
This logic has been lifted and shifted to the root_hooks.go file.
schnie
commented
Dec 20, 2024
Comment on lines
+68
to
+71
| PersistentPreRunE: utils.ChainRunEs( | ||
| SetupLoggingPersistentPreRunE, | ||
| CreateRootPersistentPreRunE(astroCoreClient, platformCoreClient), | ||
| ), |
Member
Author
There was a problem hiding this comment.
We've broken apart the core of the function and the piece that sets up the logger into 2 separate hook functions, which we chain together here. Functionally, nothing changes, but we've built a pattern we can expand on in a future PR.
0e09f1b to
e1d6a0f
Compare
e1d6a0f to
d3f0f23
Compare
neel-astro
approved these changes
Dec 20, 2024
pritt20
approved these changes
Dec 20, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR just adds a new utility to chain multiple cobra
RunEtype functions. This PR actually changes nothing functionally, but I'll be building upon this in a future PR to fix the logging of theastro devcommands.Hat tip to @jeremybeard for the original concept here.
Motivation
At the moment, the logging setup in our root
PersistentPreRunEnever actually runs for theastro devcommands because we clobber this function property with a separate one. This setup allows us to separate out logic and share composable RunE functions when clobbering aRunEcommand is necessary.🎟 Issue(s)
Related to https://github.com/astronomer/astro/issues/26133
🧪 Functional Testing
Commands still run as expected and all tests pass. New tests added for chain functionality.
📸 Screenshots
📋 Checklist
make testbefore taking out of draftmake lintbefore taking out of draft