-
Notifications
You must be signed in to change notification settings - Fork 73
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
Conversation
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 | ||
}, |
There was a problem hiding this comment.
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.
PersistentPreRunE: utils.ChainRunEs( | ||
SetupLoggingPersistentPreRunE, | ||
CreateRootPersistentPreRunE(astroCoreClient, platformCoreClient), | ||
), |
There was a problem hiding this comment.
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.
0e09f1b
to
e1d6a0f
Compare
e1d6a0f
to
d3f0f23
Compare
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 theastro 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 theastro 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 aRunE
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
📋 Checklist
make test
before taking out of draftmake lint
before taking out of draft