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

Adds Ability to Chain Cobra RunE Commands #1771

Merged
merged 1 commit into from
Dec 20, 2024
Merged

Conversation

schnie
Copy link
Member

@schnie schnie commented Dec 20, 2024

Description

This PR just adds a new utility to chain multiple cobra RunE type functions. This PR actually changes nothing functionally, but I'll be building upon this in a future PR to fix the logging of the astro dev commands.

Hat tip to @jeremybeard for the original concept here.

Motivation

At the moment, the logging setup in our root PersistentPreRunE never actually runs for the astro dev commands 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 a RunE command 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

Add screenshots to illustrate the validity of these changes.

📋 Checklist

  • Rebased from the main (or release if patching) branch (before testing)
  • Ran make test before taking out of draft
  • Ran make lint before taking out of draft
  • Added/updated applicable tests
  • Tested against Astro-API (if necessary).
  • Tested against Houston-API and Astronomer (if necessary).
  • Communicated to/tagged owners of respective clients potentially impacted by these changes.
  • Updated any related documentation

@schnie schnie marked this pull request as ready for review December 20, 2024 17:38
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
},
Copy link
Member Author

Choose a reason for hiding this comment

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

This logic has been lifted and shifted to the root_hooks.go file.

Comment on lines +68 to +71
PersistentPreRunE: utils.ChainRunEs(
SetupLoggingPersistentPreRunE,
CreateRootPersistentPreRunE(astroCoreClient, platformCoreClient),
),
Copy link
Member Author

Choose a reason for hiding this comment

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

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.

@schnie schnie force-pushed the feature/chain-cobra-cmds branch from 0e09f1b to e1d6a0f Compare December 20, 2024 17:42
@schnie schnie force-pushed the feature/chain-cobra-cmds branch from e1d6a0f to d3f0f23 Compare December 20, 2024 17:50
@schnie schnie merged commit 76ea818 into main Dec 20, 2024
3 checks passed
@schnie schnie deleted the feature/chain-cobra-cmds branch December 20, 2024 18:36
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.

3 participants